IG: consider more unary plus usages confusing (IDEA-182164)

This commit is contained in:
Bas Leijdekkers
2017-12-09 13:25:28 +01:00
parent 4fcfc76365
commit 72689b4e17
4 changed files with 32 additions and 6 deletions
@@ -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;
}
@@ -1,12 +1,13 @@
<html>
<body>
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>.
its presence may represent a coding error.
Particularly in combination with the increment operator, <b>+++</b>, or the equals 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.
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.
<p>
</body>
@@ -0,0 +1,15 @@
class UnaryPlusConfusing {
void confusing(int x) {
x =<warning descr="Unary '+' operator">+</warning> 1;
x = <warning descr="Unary '+' operator">+</warning>(1);
x = <warning descr="Unary '+' operator">+</warning> ++x;
x = 1 + <warning descr="Unary '+' operator">+</warning> 1;
int y =<warning descr="Unary '+' operator">+</warning> x;
}
void notConfusing(int x) {
System.out.println(+x);
int[] xs = {+1, -3, +2};
}
}
@@ -28,6 +28,11 @@ public class UnaryPlusInspectionTest extends LightInspectionTestCase {
doTest();
}
public void testUnaryPlusConfusing() {
myFixture.enableInspections(new UnaryPlusInspection());
doTest();
}
@Nullable
@Override
protected InspectionProfileEntry getInspection() {