JavaCodeStyleManagerImpl#hasConflictingVariableAfterwards: check variable children as well

It could be a declaration which initializer contains a lambda. In this case lambda parameter name wasn't checked
Fixes IDEA-206763 Iteration over keySet quickfix does not take into consideration that the variable name "entry" is already used by a lambda value
This commit is contained in:
Tagir Valeev
2019-02-07 09:57:57 +07:00
parent fd941abc8b
commit 2cfd1ece23
4 changed files with 30 additions and 0 deletions
@@ -1069,6 +1069,7 @@ public class JavaCodeStyleManagerImpl extends JavaCodeStyleManager {
if (name.equals(variable.getName()) && !canBeReused.test(variable)) {
throw new CancelException();
}
super.visitVariable(variable);
}
});
}
@@ -0,0 +1,14 @@
import java.util.*;
public class NestedLambdaNameConflict {
public void test(Map<String, String> m){
for(Map.Entry<String, String> e : m.entrySet()) {
System.out.println(e.getKey());
Foo foo = entry -> entry.getKey().equals(e.getValue());
}
}
interface Foo {
boolean doSmth(Map.Entry<String, String> e);
}
}
@@ -0,0 +1,14 @@
import java.util.*;
public class NestedLambdaNameConflict {
public void test(Map<String, String> m){
for(String k : m.<caret>keySet()) {
System.out.println(k);
Foo foo = entry -> entry.getKey().equals(m.get(k));
}
}
interface Foo {
boolean doSmth(Map.Entry<String, String> e);
}
}
@@ -52,5 +52,6 @@ public class KeySetIterationMayUseEntrySetFixTest extends IGQuickFixesTestCase {
public void testLambda() { doTest(CommonQuickFixBundle.message("fix.replace.with.x", "Map.forEach()")); }
public void testLambdaNoVar() { doTest(CommonQuickFixBundle.message("fix.replace.with.x", "Map.forEach()")); }
public void testWildcardType() { doTest(CommonQuickFixBundle.message("fix.replace.with.x", "values()")); }
public void testNestedLambdaNameConflict() { doTest(CommonQuickFixBundle.message("fix.replace.with.x", "entrySet()")); }
}