From 2cfd1ece23b12326d4eb0bab61d3535d2136357d Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Thu, 7 Feb 2019 09:56:12 +0700 Subject: [PATCH] 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 --- .../source/codeStyle/JavaCodeStyleManagerImpl.java | 1 + .../NestedLambdaNameConflict.after.java | 14 ++++++++++++++ .../NestedLambdaNameConflict.java | 14 ++++++++++++++ .../KeySetIterationMayUseEntrySetFixTest.java | 1 + 4 files changed, 30 insertions(+) create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/performance/key_set_with_entry_set/NestedLambdaNameConflict.after.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/performance/key_set_with_entry_set/NestedLambdaNameConflict.java diff --git a/java/java-impl/src/com/intellij/psi/impl/source/codeStyle/JavaCodeStyleManagerImpl.java b/java/java-impl/src/com/intellij/psi/impl/source/codeStyle/JavaCodeStyleManagerImpl.java index 2b72096a5a14..916475d4b5ba 100644 --- a/java/java-impl/src/com/intellij/psi/impl/source/codeStyle/JavaCodeStyleManagerImpl.java +++ b/java/java-impl/src/com/intellij/psi/impl/source/codeStyle/JavaCodeStyleManagerImpl.java @@ -1069,6 +1069,7 @@ public class JavaCodeStyleManagerImpl extends JavaCodeStyleManager { if (name.equals(variable.getName()) && !canBeReused.test(variable)) { throw new CancelException(); } + super.visitVariable(variable); } }); } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/performance/key_set_with_entry_set/NestedLambdaNameConflict.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/performance/key_set_with_entry_set/NestedLambdaNameConflict.after.java new file mode 100644 index 000000000000..fd8b15b3105a --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/performance/key_set_with_entry_set/NestedLambdaNameConflict.after.java @@ -0,0 +1,14 @@ +import java.util.*; + +public class NestedLambdaNameConflict { + public void test(Map m){ + for(Map.Entry e : m.entrySet()) { + System.out.println(e.getKey()); + Foo foo = entry -> entry.getKey().equals(e.getValue()); + } + } + + interface Foo { + boolean doSmth(Map.Entry e); + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/performance/key_set_with_entry_set/NestedLambdaNameConflict.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/performance/key_set_with_entry_set/NestedLambdaNameConflict.java new file mode 100644 index 000000000000..aaddc8c23113 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/performance/key_set_with_entry_set/NestedLambdaNameConflict.java @@ -0,0 +1,14 @@ +import java.util.*; + +public class NestedLambdaNameConflict { + public void test(Map m){ + for(String k : m.keySet()) { + System.out.println(k); + Foo foo = entry -> entry.getKey().equals(m.get(k)); + } + } + + interface Foo { + boolean doSmth(Map.Entry e); + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/performance/KeySetIterationMayUseEntrySetFixTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/performance/KeySetIterationMayUseEntrySetFixTest.java index 9ffd256aabf5..39bdef7868d9 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/performance/KeySetIterationMayUseEntrySetFixTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/performance/KeySetIterationMayUseEntrySetFixTest.java @@ -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()")); } } \ No newline at end of file