IG: consider boxing in "Unary plus" inspection

This commit is contained in:
Bas Leijdekkers
2015-08-20 15:44:37 +02:00
parent 79a4106994
commit c9081d2c7b
4 changed files with 37 additions and 26 deletions
@@ -28,6 +28,7 @@ import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.psiutils.MethodCallUtils;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import com.siyeh.ig.psiutils.TypeUtils;
import org.jdom.Element;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
@@ -124,6 +125,9 @@ public class UnaryPlusInspection extends BaseInspection {
return;
}
final PsiType type = operand.getType();
if (type == null) {
return;
}
if (onlyReportInsideBinaryExpression) {
final PsiElement parent = ParenthesesUtils.getParentSkipParentheses(prefixExpression);
if (!(operand instanceof PsiParenthesizedExpression) && !(operand instanceof PsiPrefixExpression) &&
@@ -131,7 +135,7 @@ public class UnaryPlusInspection extends BaseInspection {
return;
}
}
else if (PsiType.BYTE.equals(type) || PsiType.SHORT.equals(type)) {
else if (TypeUtils.unaryNumericPromotion(type) != type) {
PsiExpression expression = prefixExpression;
PsiElement parent = expression.getParent();
while (parent instanceof PsiParenthesizedExpression) {
@@ -173,7 +173,7 @@ public class ExpectedTypeUtils {
expectedType = type;
}
else if (isShiftOperation(tokenType)) {
expectedType = unaryNumericPromotion(wrappedExpressionType);
expectedType = TypeUtils.unaryNumericPromotion(wrappedExpressionType);
}
else if (ComparisonUtils.isEqualityComparison(polyadicExpression)) {
final PsiExpression operand1 = operands[0];
@@ -229,30 +229,6 @@ public class ExpectedTypeUtils {
return null;
}
/**
* JLS 5.6.1 Unary Numeric Promotion
*/
private static PsiType unaryNumericPromotion(PsiType type) {
if (type == null) {
return null;
}
if (type.equalsToText("java.lang.Byte") || type.equalsToText("java.lang.Short") ||
type.equalsToText("java.lang.Character") || type.equalsToText("java.lang.Integer") ||
type.equals(PsiType.BYTE) || type.equals(PsiType.SHORT) || type.equals(PsiType.CHAR)) {
return PsiType.INT;
}
else if (type.equalsToText("java.lang.Long")) {
return PsiType.LONG;
}
else if (type.equalsToText("java.lang.Float")) {
return PsiType.FLOAT;
}
else if (type.equalsToText("java.lang.Double")) {
return PsiType.DOUBLE;
}
return type;
}
@Override
public void visitPrefixExpression(@NotNull PsiPrefixExpression expression) {
final PsiType type = expression.getType();
@@ -213,4 +213,28 @@ public class TypeUtils {
final PsiClass aClass = classType.resolve();
return aClass != null && aClass instanceof PsiTypeParameter;
}
/**
* JLS 5.6.1 Unary Numeric Promotion
*/
public static PsiType unaryNumericPromotion(PsiType type) {
if (type == null) {
return null;
}
if (type.equalsToText("java.lang.Byte") || type.equalsToText("java.lang.Short") ||
type.equalsToText("java.lang.Character") || type.equalsToText("java.lang.Integer") ||
type.equals(PsiType.BYTE) || type.equals(PsiType.SHORT) || type.equals(PsiType.CHAR)) {
return PsiType.INT;
}
else if (type.equalsToText("java.lang.Long")) {
return PsiType.LONG;
}
else if (type.equalsToText("java.lang.Float")) {
return PsiType.FLOAT;
}
else if (type.equalsToText("java.lang.Double")) {
return PsiType.DOUBLE;
}
return type;
}
}
@@ -8,4 +8,11 @@ class UnaryPlus {
m(+b); // calls different method when unary plus removed.
System.out.println(<warning descr="Unary '+' operator">+</warning>1);
}
public void n(Byte i) {}
public void n(int i) {}
void n() {
Byte b = 1;
m(+b);
}
}