IG: add option to only report in binary expressions to "Unary plus" inspection

This commit is contained in:
Bas Leijdekkers
2015-08-19 20:18:29 +02:00
parent 3ff0ba6be4
commit 99bdddb361
3 changed files with 38 additions and 5 deletions
@@ -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) {
@@ -1,9 +1,11 @@
<html>
<body>
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 <b>'+'</b> operator. Unary plus is usually a null operation, and
its presence may represent a coding error, particularly in combination with the increment operator, <b>'++'</b>.
<!-- tooltip end -->
<p>
Use the checkbox below to only report when an unary plus is used together with a binary or another unary expression.
<p>
</body>
</html>
@@ -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;
}
}