From 99bdddb361eadf65f9461e4db6e0e98b2ff34c9b Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Wed, 19 Aug 2015 16:05:02 +0200 Subject: [PATCH] IG: add option to only report in binary expressions to "Unary plus" inspection --- .../siyeh/ig/numeric/UnaryPlusInspection.java | 33 +++++++++++++++++-- .../src/inspectionDescriptions/UnaryPlus.html | 6 ++-- .../ig/numeric/UnaryPlusInspectionTest.java | 4 ++- 3 files changed, 38 insertions(+), 5 deletions(-) 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 56a09e875c16..51ec536534ef 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/UnaryPlusInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/UnaryPlusInspection.java @@ -17,7 +17,9 @@ package com.siyeh.ig.numeric; import com.intellij.codeInspection.ProblemDescriptor; import com.intellij.codeInspection.ProblemHighlightType; +import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel; import com.intellij.openapi.project.Project; +import com.intellij.openapi.util.WriteExternalException; import com.intellij.psi.*; import com.intellij.psi.tree.IElementType; import com.siyeh.InspectionGadgetsBundle; @@ -25,12 +27,18 @@ import com.siyeh.ig.BaseInspection; import com.siyeh.ig.BaseInspectionVisitor; import com.siyeh.ig.InspectionGadgetsFix; import com.siyeh.ig.psiutils.MethodCallUtils; +import com.siyeh.ig.psiutils.ParenthesesUtils; +import org.jdom.Element; import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import javax.swing.*; + public class UnaryPlusInspection extends BaseInspection { + public boolean onlyReportInsideBinaryExpression = true; + @Override @NotNull public String getDisplayName() { @@ -44,6 +52,20 @@ public class UnaryPlusInspection extends BaseInspection { "unary.plus.problem.descriptor"); } + @Nullable + @Override + public JComponent createOptionsPanel() { + return new SingleCheckboxOptionsPanel("Only report in confusing binary or unary expression context", this, + "onlyReportInsideBinaryExpression"); + } + + @Override + public void writeSettings(@NotNull Element node) throws WriteExternalException { + if (!onlyReportInsideBinaryExpression) { + node.addContent(new Element("option").setAttribute("name", "onlyReportInsideBinaryExpression").setAttribute("value", "false")); + } + } + @Nullable @Override protected InspectionGadgetsFix buildFix(Object... infos) { @@ -87,7 +109,7 @@ public class UnaryPlusInspection extends BaseInspection { return new UnaryPlusVisitor(); } - private static class UnaryPlusVisitor extends BaseInspectionVisitor { + private class UnaryPlusVisitor extends BaseInspectionVisitor { @Override public void visitPrefixExpression(PsiPrefixExpression prefixExpression) { @@ -102,7 +124,14 @@ public class UnaryPlusInspection extends BaseInspection { return; } final PsiType type = operand.getType(); - if (PsiType.BYTE.equals(type) || PsiType.SHORT.equals(type)) { + if (onlyReportInsideBinaryExpression) { + final PsiElement parent = ParenthesesUtils.getParentSkipParentheses(prefixExpression); + if (!(operand instanceof PsiParenthesizedExpression) && !(operand instanceof PsiPrefixExpression) && + !(parent instanceof PsiPolyadicExpression) && !(parent instanceof PsiPrefixExpression)) { + return; + } + } + else if (PsiType.BYTE.equals(type) || PsiType.SHORT.equals(type)) { PsiExpression expression = prefixExpression; PsiElement parent = expression.getParent(); while (parent instanceof PsiParenthesizedExpression) { diff --git a/plugins/InspectionGadgets/src/inspectionDescriptions/UnaryPlus.html b/plugins/InspectionGadgets/src/inspectionDescriptions/UnaryPlus.html index 698a8be5858e..e67f9b5387bb 100644 --- a/plugins/InspectionGadgets/src/inspectionDescriptions/UnaryPlus.html +++ b/plugins/InspectionGadgets/src/inspectionDescriptions/UnaryPlus.html @@ -1,9 +1,11 @@ -Reports any uses of the unary '+' operator. Unary plus is a null operation, and -its presence may represent a coding error, particularly in combination with the increment operator, '++'. +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, '++'.

+Use the checkbox below to only report when an unary plus is used together with a binary or another unary expression. +

\ 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 a51023ec15f0..802951ed23de 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/numeric/UnaryPlusInspectionTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/numeric/UnaryPlusInspectionTest.java @@ -31,6 +31,8 @@ public class UnaryPlusInspectionTest extends LightInspectionTestCase { @Nullable @Override protected InspectionProfileEntry getInspection() { - return new UnaryPlusInspection(); + final UnaryPlusInspection inspection = new UnaryPlusInspection(); + inspection.onlyReportInsideBinaryExpression = false; + return inspection; } } \ No newline at end of file