[java-inspections] IDEA-273527 Incorrect generated code with "Can be replaced with 'Integer.compare'"

GitOrigin-RevId: 4918eebd8b1d6996a8330bf4cbfcc66a367ded18
This commit is contained in:
Tagir Valeev
2021-07-14 13:42:07 +07:00
committed by intellij-monorepo-bot
parent 987e0ec5e5
commit 05309a5d4e
5 changed files with 74 additions and 1 deletions

View File

@@ -124,7 +124,9 @@ public class UseCompareMethodInspection extends AbstractBaseJavaLocalInspectionT
if (elseExpression instanceof PsiConditionalExpression) {
Map<Integer, PsiExpression> 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;

View File

@@ -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<Point> 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);
}
}

View File

@@ -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<Point> l = Arrays.asList(new Point(1, 0), new Point(0, 1), new Point(0, 0)));
l.sort((o1, o2) -> <caret>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);
}
}

View File

@@ -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<Point> 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 : <caret>o1.y < o2.y ? -1 : o1.y > o2.y ? 1 : 0);
System.out.println(l);
}
}

View File

@@ -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";