From 72689b4e17ba47d9abd1e4876c095895f2bcddca Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Sat, 9 Dec 2017 13:01:54 +0100 Subject: [PATCH] IG: consider more unary plus usages confusing (IDEA-182164) --- .../com/siyeh/ig/numeric/UnaryPlusInspection.java | 11 ++++++++--- .../src/inspectionDescriptions/UnaryPlus.html | 7 ++++--- .../numeric/unary_plus/UnaryPlusConfusing.java | 15 +++++++++++++++ .../siyeh/ig/numeric/UnaryPlusInspectionTest.java | 5 +++++ 4 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/unary_plus/UnaryPlusConfusing.java diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/UnaryPlusInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/UnaryPlusInspection.java index eeae65da6c94..3847d0e0866f 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/UnaryPlusInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/UnaryPlusInspection.java @@ -123,12 +123,17 @@ public class UnaryPlusInspection extends BaseInspection { } if (onlyReportInsideBinaryExpression) { final PsiElement parent = ParenthesesUtils.getParentSkipParentheses(prefixExpression); - if (!(operand instanceof PsiParenthesizedExpression) && !(operand instanceof PsiPrefixExpression) && - !(parent instanceof PsiPolyadicExpression) && !(parent instanceof PsiPrefixExpression)) { + if (!(operand instanceof PsiParenthesizedExpression || + operand instanceof PsiPrefixExpression || + parent instanceof PsiPolyadicExpression || + parent instanceof PsiPrefixExpression || + parent instanceof PsiAssignmentExpression || + parent instanceof PsiVariable)) { return; } } - else if (TypeUtils.unaryNumericPromotion(type) != type && MethodCallUtils.isNecessaryForSurroundingMethodCall(prefixExpression, operand)) { + else if (TypeUtils.unaryNumericPromotion(type) != type && + MethodCallUtils.isNecessaryForSurroundingMethodCall(prefixExpression, operand)) { // unary plus might have been used as cast to int return; } diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/UnaryPlus.html b/plugins/InspectionGadgets/src/inspectionDescriptions/UnaryPlus.html index 45b136aefda1..d305e2e7bd11 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/UnaryPlus.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/UnaryPlus.html @@ -1,12 +1,13 @@ Reports any uses of the unary + operator. Unary plus is usually a null operation, and -its presence may represent a coding error, particularly in combination with the increment operator, ++. +its presence may represent a coding error. +Particularly in combination with the increment operator, +++, or the equals operator, =+

Use the checkbox below to only report when an unary plus is used together with a binary or another unary expression. -This means the inspection won't warn when an unary plus expression is for example used as a variable initializer, as an argument to a method -or as the right-hand side of an assignment. In such cases it is much less confusing. +This means the inspection won't warn when an unary plus expression is used for example as a method argument, or in an array +initializer expression. In such cases it is much less confusing.

diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/unary_plus/UnaryPlusConfusing.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/unary_plus/UnaryPlusConfusing.java new file mode 100644 index 000000000000..41166a3a7508 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/unary_plus/UnaryPlusConfusing.java @@ -0,0 +1,15 @@ +class UnaryPlusConfusing { + + void confusing(int x) { + x =+ 1; + x = +(1); + x = + ++x; + x = 1 + + 1; + int y =+ x; + } + + void notConfusing(int x) { + System.out.println(+x); + int[] xs = {+1, -3, +2}; + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/numeric/UnaryPlusInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/numeric/UnaryPlusInspectionTest.java index 802951ed23de..78da26c90444 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/numeric/UnaryPlusInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/numeric/UnaryPlusInspectionTest.java @@ -28,6 +28,11 @@ public class UnaryPlusInspectionTest extends LightInspectionTestCase { doTest(); } + public void testUnaryPlusConfusing() { + myFixture.enableInspections(new UnaryPlusInspection()); + doTest(); + } + @Nullable @Override protected InspectionProfileEntry getInspection() {