diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties index 46226c74862d..cfd69bd17f1b 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/InspectionGadgetsBundle.properties @@ -1486,7 +1486,8 @@ wait.without.corresponding.notify.problem.descriptor=Call to #ref w notify.without.corresponding.wait.display.name='notify()' without corresponding 'wait()' notify.without.corresponding.wait.problem.descriptor=Call to #ref without corresponding wait() #loc integer.multiplication.implicit.cast.to.long.display.name=Integer multiplication or shift implicitly cast to long -integer.multiplication.implicit.cast.to.long.problem.descriptor=#ref: integer multiplication or shift implicitly cast to long #loc +integer.multiplication.implicit.cast.to.long.problem.descriptor=#ref: integer multiplication implicitly cast to long #loc +integer.shift.implicit.cast.to.long.problem.descriptor=#ref: integer shift implicitly cast to long #loc integer.multiplication.implicit.cast.to.long.option=Ignore compile time constant expressions which do not overflow wait.or.await.without.timeout.display.name='wait()' or 'await()' without timeout wait.or.await.without.timeout.problem.descriptor=#ref without timeout #loc diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/IntegerMultiplicationImplicitCastToLongInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/IntegerMultiplicationImplicitCastToLongInspection.java index 53fedfcaa8d5..2991e8f783f2 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/IntegerMultiplicationImplicitCastToLongInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/IntegerMultiplicationImplicitCastToLongInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2011 Dave Griffith, Bas Leijdekkers + * Copyright 2006-2014 Dave Griffith, Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -31,8 +31,7 @@ import javax.swing.*; import java.util.HashSet; import java.util.Set; -public class IntegerMultiplicationImplicitCastToLongInspection extends - BaseInspection { +public class IntegerMultiplicationImplicitCastToLongInspection extends BaseInspection { /** * @noinspection StaticCollection @@ -45,6 +44,10 @@ public class IntegerMultiplicationImplicitCastToLongInspection extends s_typesToCheck.add("short"); s_typesToCheck.add("byte"); s_typesToCheck.add("char"); + s_typesToCheck.add(CommonClassNames.JAVA_LANG_INTEGER); + s_typesToCheck.add(CommonClassNames.JAVA_LANG_SHORT); + s_typesToCheck.add(CommonClassNames.JAVA_LANG_BYTE); + s_typesToCheck.add(CommonClassNames.JAVA_LANG_CHARACTER); } @SuppressWarnings({"PublicField"}) @@ -60,8 +63,13 @@ public class IntegerMultiplicationImplicitCastToLongInspection extends @Override @NotNull protected String buildErrorString(Object... infos) { - return InspectionGadgetsBundle.message( - "integer.multiplication.implicit.cast.to.long.problem.descriptor"); + final IElementType tokenType = (IElementType)infos[0]; + if (JavaTokenType.ASTERISK.equals(tokenType)) { + return InspectionGadgetsBundle.message("integer.multiplication.implicit.cast.to.long.problem.descriptor"); + } + else { + return InspectionGadgetsBundle.message("integer.shift.implicit.cast.to.long.problem.descriptor"); + } } @Override @@ -80,9 +88,8 @@ public class IntegerMultiplicationImplicitCastToLongInspection extends extends BaseInspectionVisitor { @Override - public void visitBinaryExpression( - @NotNull PsiBinaryExpression expression) { - super.visitBinaryExpression(expression); + public void visitPolyadicExpression(@NotNull PsiPolyadicExpression expression) { + super.visitPolyadicExpression(expression); final IElementType tokenType = expression.getOperationTokenType(); if (!tokenType.equals(JavaTokenType.ASTERISK) && !tokenType.equals(JavaTokenType.LTLT)) { @@ -92,12 +99,7 @@ public class IntegerMultiplicationImplicitCastToLongInspection extends if (!isNonLongInteger(type)) { return; } - final PsiExpression rhs = expression.getROperand(); - if (rhs == null) { - return; - } - final PsiType rhsType = rhs.getType(); - if (!isNonLongInteger(rhsType)) { + if (expression.getOperands().length < 2 || expression.getLastChild() instanceof PsiErrorElement) { return; } final PsiExpression context = getContainingExpression(expression); @@ -112,18 +114,16 @@ public class IntegerMultiplicationImplicitCastToLongInspection extends if (!contextType.equals(PsiType.LONG)) { return; } - try { - final Object result = - ExpressionUtils.computeConstantExpression(expression, - true); - if (ignoreNonOverflowingCompileTimeConstants && - result != null) { - return; + if (ignoreNonOverflowingCompileTimeConstants) { + try { + if (ExpressionUtils.computeConstantExpression(expression, true) != null) { + return; + } + } + catch (ConstantEvaluationOverflowException ignore) { } } - catch (ConstantEvaluationOverflowException ignore) { - } - registerError(expression); + registerError(expression, tokenType); } private PsiExpression getContainingExpression( diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/IntegerMultiplicationCastToLong.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/IntegerMultiplicationCastToLong.java deleted file mode 100644 index 6825419b4721..000000000000 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/IntegerMultiplicationCastToLong.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.siyeh.igtest.numeric; - -public class IntegerMultiplicationCastToLong { - public void foo() { - int x = 65336; - final long val = 65336 * x; - } -} diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/integer_multiplication_implicit_cast_to_long/IntegerMultiplicationImplicitCastToLong.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/integer_multiplication_implicit_cast_to_long/IntegerMultiplicationImplicitCastToLong.java new file mode 100644 index 000000000000..c161bf260930 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/integer_multiplication_implicit_cast_to_long/IntegerMultiplicationImplicitCastToLong.java @@ -0,0 +1,12 @@ +package com.siyeh.igtest.numeric.integer_multiplication_implicit_cast_to_long; + +public class IntegerMultiplicationImplicitCastToLong { + public void foo() { + int x = 65336; + final long val = 65336 * x; + long other = Integer.valueOf(65336) * Integer.valueOf(x); + long third = x << 24; + long polyadic = x * 1024 * 1024; + long incomplete = x * x *; + } +} diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/numeric/IntegerMultiplicationImplicitCastToLongInspectionTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/numeric/IntegerMultiplicationImplicitCastToLongInspectionTest.java new file mode 100644 index 000000000000..9de5a8e61e78 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/numeric/IntegerMultiplicationImplicitCastToLongInspectionTest.java @@ -0,0 +1,36 @@ +/* + * Copyright 2000-2014 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.siyeh.ig.numeric; + +import com.intellij.codeInspection.InspectionProfileEntry; +import com.siyeh.ig.LightInspectionTestCase; +import org.jetbrains.annotations.Nullable; + +/** + * @author Bas Leijdekkers + */ +public class IntegerMultiplicationImplicitCastToLongInspectionTest extends LightInspectionTestCase { + + public void testIntegerMultiplicationImplicitCastToLong() { + doTest(); + } + + @Nullable + @Override + protected InspectionProfileEntry getInspection() { + return new IntegerMultiplicationImplicitCastToLongInspection(); + } +}