anonymous -> lambda: allow field shadowing when suggesting new local names (IDEA-157288)

This commit is contained in:
Anna Kozlova
2016-07-08 10:46:34 +02:00
parent 6ee2ce046e
commit 4e813690f7
3 changed files with 29 additions and 1 deletions
@@ -344,7 +344,7 @@ public class AnonymousCanBeLambdaInspection extends BaseJavaBatchLocalInspection
PsiVariable local = iterator.next();
final String localName = local.getName();
if (localName == null ||
helper.resolveReferencedVariable(localName, lambdaExpression) == null ||
shadowingResolve(localName, lambdaExpression, helper) ||
!PsiTreeUtil.isAncestor(lambdaExpression, local, false)) {
iterator.remove();
namesOfVariablesInTheBlock.add(localName);
@@ -352,6 +352,11 @@ public class AnonymousCanBeLambdaInspection extends BaseJavaBatchLocalInspection
}
}
private static boolean shadowingResolve(String localName, PsiLambdaExpression lambdaExpression, PsiResolveHelper helper) {
final PsiVariable variable = helper.resolveReferencedVariable(localName, lambdaExpression);
return variable == null || variable instanceof PsiField;
}
private static class ReplaceWithLambdaFix implements LocalQuickFix, HighPriorityAction {
@NotNull
@Override
@@ -0,0 +1,10 @@
// "Replace with lambda" "true"
class Test {
Integer s;
private void m() {
Runnable r = () -> {
System.out.println(s);
Integer s = Test.this.s;
};
}
}
@@ -0,0 +1,13 @@
// "Replace with lambda" "true"
class Test {
Integer s;
private void m() {
Runnable r = new Runna<caret>ble() {
@Override
public void run() {
System.out.println(s);
Integer s = Test.this.s;
}
};
}
}