mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-19 21:11:28 +07:00
forbid anonymous -> lambda when conflicting local vars exist (IDEA-93589)
This commit is contained in:
@@ -26,9 +26,12 @@ import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.Function;
|
||||
import com.intellij.util.containers.HashSet;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* User: anna
|
||||
*/
|
||||
@@ -76,6 +79,7 @@ public class AnonymousCanBeLambdaInspection extends BaseJavaLocalInspectionTool
|
||||
final PsiCodeBlock body = methods[0].getBody();
|
||||
if (body != null) {
|
||||
final boolean [] bodyContainsForbiddenRefs = new boolean[1];
|
||||
final Set<PsiLocalVariable> locals = new HashSet<PsiLocalVariable>();
|
||||
body.accept(new JavaRecursiveElementWalkingVisitor() {
|
||||
@Override
|
||||
public void visitMethodCallExpression(PsiMethodCallExpression methodCallExpression) {
|
||||
@@ -103,8 +107,19 @@ public class AnonymousCanBeLambdaInspection extends BaseJavaLocalInspectionTool
|
||||
bodyContainsForbiddenRefs[0] = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitLocalVariable(PsiLocalVariable variable) {
|
||||
super.visitLocalVariable(variable);
|
||||
locals.add(variable);
|
||||
}
|
||||
});
|
||||
if (!bodyContainsForbiddenRefs[0]) {
|
||||
PsiResolveHelper helper = PsiResolveHelper.SERVICE.getInstance(body.getProject());
|
||||
for (PsiLocalVariable local : locals) {
|
||||
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());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
// "Replace with lambda" "false"
|
||||
class Foo11 {
|
||||
Runnable runnable = new Runnable() {
|
||||
public void run() {
|
||||
int x = 5;
|
||||
new Runn<caret>able() {
|
||||
public void run () {
|
||||
int x = 10;
|
||||
}
|
||||
} ;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user