make "Integer multiplication or shift implicitly cast to long" inspection work correctly in the presence of polyadic and boxed expressions

This commit is contained in:
Bas Leijdekkers
2014-11-05 16:26:37 +01:00
parent 1e2b1a981c
commit 2c7dabe3bd
5 changed files with 74 additions and 33 deletions
@@ -1486,7 +1486,8 @@ wait.without.corresponding.notify.problem.descriptor=Call to <code>#ref</code> w
notify.without.corresponding.wait.display.name='notify()' without corresponding 'wait()'
notify.without.corresponding.wait.problem.descriptor=Call to <code>#ref</code> without corresponding <code>wait()</code> #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=<html>Ignore compile time constant expressions which do not overflow</html>
wait.or.await.without.timeout.display.name='wait()' or 'await()' without timeout
wait.or.await.without.timeout.problem.descriptor=<code>#ref</code> without timeout #loc
@@ -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(
@@ -1,8 +0,0 @@
package com.siyeh.igtest.numeric;
public class IntegerMultiplicationCastToLong {
public void foo() {
int x = 65336;
final long val = 65336 * x;
}
}
@@ -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 = <warning descr="65336 * x: integer multiplication implicitly cast to long">65336 * x</warning>;
long other = <warning descr="Integer.valueOf(65336) * Integer.valueOf(x): integer multiplication implicitly cast to long">Integer.valueOf(65336) * Integer.valueOf(x)</warning>;
long third = <warning descr="x << 24: integer shift implicitly cast to long">x << 24</warning>;
long polyadic = <warning descr="x * 1024 * 1024: integer multiplication implicitly cast to long">x * 1024 * 1024</warning>;
long incomplete = x * x *<error descr="Expression expected">;</error>
}
}
@@ -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();
}
}