mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-05-06 05:10:22 +07:00
lambda -> anonym: disable in unexpected context where cast won't help (IDEA-120165)
This commit is contained in:
@@ -83,20 +83,23 @@ public class AnonymousCanBeLambdaInspection extends BaseJavaBatchLocalInspection
|
||||
if (PsiUtil.getLanguageLevel(aClass).isAtLeast(LanguageLevel.JDK_1_8)) {
|
||||
final PsiClassType baseClassType = aClass.getBaseClassType();
|
||||
if (LambdaUtil.isFunctionalType(baseClassType)) {
|
||||
final PsiMethod[] methods = aClass.getMethods();
|
||||
if (methods.length == 1 && aClass.getFields().length == 0) {
|
||||
final PsiCodeBlock body = methods[0].getBody();
|
||||
if (body != null) {
|
||||
final ForbiddenRefsChecker checker = new ForbiddenRefsChecker(methods[0], aClass);
|
||||
body.accept(checker);
|
||||
if (!checker.hasForbiddenRefs()) {
|
||||
PsiResolveHelper helper = PsiResolveHelper.SERVICE.getInstance(body.getProject());
|
||||
for (PsiLocalVariable local : checker.getLocals()) {
|
||||
final String localName = local.getName();
|
||||
if (localName != null && helper.resolveReferencedVariable(localName, aClass) != null) return;
|
||||
final PsiElement lambdaContext = aClass.getParent().getParent();
|
||||
if (LambdaUtil.isValidLambdaContext(lambdaContext) || !(lambdaContext instanceof PsiExpressionStatement)) {
|
||||
final PsiMethod[] methods = aClass.getMethods();
|
||||
if (methods.length == 1 && aClass.getFields().length == 0) {
|
||||
final PsiCodeBlock body = methods[0].getBody();
|
||||
if (body != null) {
|
||||
final ForbiddenRefsChecker checker = new ForbiddenRefsChecker(methods[0], aClass);
|
||||
body.accept(checker);
|
||||
if (!checker.hasForbiddenRefs()) {
|
||||
PsiResolveHelper helper = PsiResolveHelper.SERVICE.getInstance(body.getProject());
|
||||
for (PsiLocalVariable local : checker.getLocals()) {
|
||||
final String localName = local.getName();
|
||||
if (localName != null && helper.resolveReferencedVariable(localName, aClass) != null) return;
|
||||
}
|
||||
holder.registerProblem(aClass.getBaseClassReference(), "Anonymous #ref #loc can be replaced with lambda",
|
||||
ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new ReplaceWithLambdaFix());
|
||||
}
|
||||
holder.registerProblem(aClass.getBaseClassReference(), "Anonymous #ref #loc can be replaced with lambda",
|
||||
ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new ReplaceWithLambdaFix());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
// "Replace with lambda" "false"
|
||||
class Test {
|
||||
interface I {
|
||||
|
||||
}
|
||||
interface Bar extends I {
|
||||
int compare(String o1, String o2);
|
||||
}
|
||||
{
|
||||
new Ba<caret>r() {
|
||||
@Override
|
||||
public int compare(String o1, String o2) {
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user