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 51ec536534ef..30f2a55254cb 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/UnaryPlusInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/UnaryPlusInspection.java @@ -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) { diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ExpectedTypeUtils.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ExpectedTypeUtils.java index 2b1bde4528d0..c9d04db31647 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ExpectedTypeUtils.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ExpectedTypeUtils.java @@ -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(); diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/TypeUtils.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/TypeUtils.java index 734bde77deab..4cf028de2c7b 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/TypeUtils.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/TypeUtils.java @@ -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; + } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/unary_plus/UnaryPlus.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/unary_plus/UnaryPlus.java index 35b553618260..d2fb8c882071 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/unary_plus/UnaryPlus.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/unary_plus/UnaryPlus.java @@ -8,4 +8,11 @@ class UnaryPlus { m(+b); // calls different method when unary plus removed. System.out.println(+1); } + + public void n(Byte i) {} + public void n(int i) {} + void n() { + Byte b = 1; + m(+b); + } } \ No newline at end of file