diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/UseCompareMethodInspection.java b/java/java-analysis-impl/src/com/intellij/codeInspection/UseCompareMethodInspection.java index 7587eb7ebb73..968645d539f6 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/UseCompareMethodInspection.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/UseCompareMethodInspection.java @@ -124,7 +124,9 @@ public class UseCompareMethodInspection extends AbstractBaseJavaLocalInspectionT if (elseExpression instanceof PsiConditionalExpression) { Map m = extractConditions((PsiConditionalExpression)elseExpression); if (m == null) return null; - result.putAll(m); + for (var entry : m.entrySet()) { + if (result.put(entry.getKey(), entry.getValue()) != null) return null; + } return result; } return storeCondition(result, null, elseExpression) ? result : null; diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/useCompareMethod/afterCompareTwoFields2.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/useCompareMethod/afterCompareTwoFields2.java new file mode 100644 index 000000000000..2169346e1ec8 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/useCompareMethod/afterCompareTwoFields2.java @@ -0,0 +1,21 @@ +// "Fix all ''compare()' method can be used to compare numbers' problems in file" "true" +import java.util.ArrayList; +import java.util.List; +public class Sort { + static class Point { + int x, y; + public Point(int x, int y) { + this.x = x; + this.y = y; + } + @Override + public String toString() { + return "(" + x + ", " + y + ")"; + } + } + public static void main(String[] args) { + List l = Arrays.asList(new Point(1, 0), new Point(0, 1), new Point(0, 0))); + l.sort((o1, o2) -> o1.x < o2.x ? -1 : o1.x > o2.x ? 1 : Integer.compare(o1.y, o2.y)); + System.out.println(l); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/useCompareMethod/beforeCompareTwoFields.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/useCompareMethod/beforeCompareTwoFields.java new file mode 100644 index 000000000000..026f4f90ff20 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/useCompareMethod/beforeCompareTwoFields.java @@ -0,0 +1,21 @@ +// "Fix all ''compare()' method can be used to compare numbers' problems in file" "false" +import java.util.ArrayList; +import java.util.List; +public class Sort { + static class Point { + int x, y; + public Point(int x, int y) { + this.x = x; + this.y = y; + } + @Override + public String toString() { + return "(" + x + ", " + y + ")"; + } + } + public static void main(String[] args) { + List l = Arrays.asList(new Point(1, 0), new Point(0, 1), new Point(0, 0))); + l.sort((o1, o2) -> o1.x < o2.x ? -1 : o1.x > o2.x ? 1 : o1.y < o2.y ? -1 : o1.y > o2.y ? 1 : 0); + System.out.println(l); + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/useCompareMethod/beforeCompareTwoFields2.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/useCompareMethod/beforeCompareTwoFields2.java new file mode 100644 index 000000000000..b161ba5564b6 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/useCompareMethod/beforeCompareTwoFields2.java @@ -0,0 +1,21 @@ +// "Fix all ''compare()' method can be used to compare numbers' problems in file" "true" +import java.util.ArrayList; +import java.util.List; +public class Sort { + static class Point { + int x, y; + public Point(int x, int y) { + this.x = x; + this.y = y; + } + @Override + public String toString() { + return "(" + x + ", " + y + ")"; + } + } + public static void main(String[] args) { + List l = Arrays.asList(new Point(1, 0), new Point(0, 1), new Point(0, 0))); + l.sort((o1, o2) -> o1.x < o2.x ? -1 : o1.x > o2.x ? 1 : o1.y < o2.y ? -1 : o1.y > o2.y ? 1 : 0); + System.out.println(l); + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/quickFix/UseCompareMethodInspectionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/quickFix/UseCompareMethodInspectionTest.java index 776e62d68a9b..a31ba8149c63 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/quickFix/UseCompareMethodInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInsight/daemon/quickFix/UseCompareMethodInspectionTest.java @@ -18,8 +18,11 @@ package com.intellij.java.codeInsight.daemon.quickFix; import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase; import com.intellij.codeInspection.LocalInspectionTool; import com.intellij.codeInspection.UseCompareMethodInspection; +import com.intellij.testFramework.LightProjectDescriptor; import org.jetbrains.annotations.NotNull; +import static com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase.JAVA_8; + public class UseCompareMethodInspectionTest extends LightQuickFixParameterizedTestCase { @Override @@ -29,6 +32,11 @@ public class UseCompareMethodInspectionTest extends LightQuickFixParameterizedTe }; } + @Override + protected @NotNull LightProjectDescriptor getProjectDescriptor() { + return JAVA_8; + } + @Override protected String getBasePath() { return "/codeInsight/daemonCodeAnalyzer/quickFix/useCompareMethod";