From 35711da1d7f1709004d9f2bb0b3ba55c866eed60 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Fri, 22 Apr 2022 12:05:46 +0200 Subject: [PATCH] [java-inspections] RedundantCompareCall: insert cast when both operands are boxed Fixes IDEA-291826 IntelliJ Suggest simplify Integer.compare method to `==` GitOrigin-RevId: 71882cec49e2846f0b30ae626da6d07381e516e2 --- .../quickFix/redundantCompareCall/afterBoxing.java | 10 ++++++++++ .../quickFix/redundantCompareCall/beforeBoxing.java | 10 ++++++++++ .../RedundantCompareCallInspection.java | 13 ++++++++++++- 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantCompareCall/afterBoxing.java create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/redundantCompareCall/beforeBoxing.java 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)); }