diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantCompareCall/afterBoxing.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantCompareCall/afterBoxing.java new file mode 100644 index 000000000000..66ff51aed8a5 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantCompareCall/afterBoxing.java @@ -0,0 +1,10 @@ +// "Fix all 'Redundant 'compare()' method call' problems in file" "true" +class Boxing { + void test(Integer x, Integer y, int z) { + boolean same = (int) x == y; + boolean same1 = x > y; + boolean same2 = (int) x != y; + boolean same3 = x == z; + boolean same4 = z == x; + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantCompareCall/beforeBoxing.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantCompareCall/beforeBoxing.java new file mode 100644 index 000000000000..cde20e1af9af --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantCompareCall/beforeBoxing.java @@ -0,0 +1,10 @@ +// "Fix all 'Redundant 'compare()' method call' problems in file" "true" +class Boxing { + void test(Integer x, Integer y, int z) { + boolean same = Integer.compare(x, y) == 0; + boolean same1 = Integer.compare(x, y) > 0; + boolean same2 = Integer.compare(x, y) != 0; + boolean same3 = Integer.compare(x, z) == 0; + boolean same4 = Integer.compare(z, x) == 0; + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/src/com/intellij/codeInspection/RedundantCompareCallInspection.java b/plugins/InspectionGadgets/src/com/intellij/codeInspection/RedundantCompareCallInspection.java index 54c3435ad1bb..9b130e8f5b39 100644 --- a/plugins/InspectionGadgets/src/com/intellij/codeInspection/RedundantCompareCallInspection.java +++ b/plugins/InspectionGadgets/src/com/intellij/codeInspection/RedundantCompareCallInspection.java @@ -70,10 +70,21 @@ public class RedundantCompareCallInspection extends AbstractBaseJavaLocalInspect if (call == null) return; PsiExpression[] args = call.getArgumentList().getExpressions(); if(args.length != 2) return; + String maybeCast = ""; + if (myRelationType == RelationType.EQ || myRelationType == RelationType.NE) { + PsiType leftType = args[0].getType(); + PsiType rightType = args[1].getType(); + if (leftType instanceof PsiClassType && rightType instanceof PsiClassType) { + PsiPrimitiveType type = PsiPrimitiveType.getOptionallyUnboxedType(leftType); + if (type != null) { + maybeCast = "(" + type.getCanonicalText() + ")"; + } + } + } PsiBinaryExpression parent = PsiTreeUtil.getParentOfType(call, PsiBinaryExpression.class); if (parent == null) return; CommentTracker ct = new CommentTracker(); - ct.replaceAndRestoreComments(parent, ct.text(args[0], ParenthesesUtils.EQUALITY_PRECEDENCE) + + ct.replaceAndRestoreComments(parent, maybeCast + ct.text(args[0], ParenthesesUtils.EQUALITY_PRECEDENCE) + myRelationType + ct.text(args[1], ParenthesesUtils.EQUALITY_PRECEDENCE)); }