[java] numeric overflow inspection on left shift (IDEA-154370)

This commit is contained in:
Roman Shevchenko
2016-04-08 20:33:33 +02:00
parent 0dc69cd4fe
commit 6eefa1badb
2 changed files with 10 additions and 4 deletions

View File

@@ -351,10 +351,16 @@ class ConstantExpressionVisitor extends JavaElementVisitor implements PsiConstan
if (rOperandValue instanceof Character) rOperandValue = Integer.valueOf(((Character)rOperandValue).charValue());
if (isIntegral(lOperandValue) && isIntegral(rOperandValue)) {
if (lOperandValue instanceof Long) {
value = Long.valueOf(((Number)lOperandValue).longValue() << ((Number)rOperandValue).longValue());
long l = ((Number)lOperandValue).longValue();
long r = ((Number)rOperandValue).longValue();
value = Long.valueOf(l << r);
checkMultiplicationOverflow(((Long)value).longValue(), l, (long)Math.pow(2, r & 0x3F), expression);
}
else {
value = Integer.valueOf(((Number)lOperandValue).intValue() << ((Number)rOperandValue).intValue());
int l = ((Number)lOperandValue).intValue();
int r = ((Number)rOperandValue).intValue();
value = Integer.valueOf(l << r);
checkMultiplicationOverflow(((Integer)value).intValue(), l, (long)Math.pow(2, r & 0x1F), expression);
}
}
}

View File

@@ -43,6 +43,7 @@ class c {
i1 = INTEGER_MAX_VALUE * -1;
<warning descr="Numeric overflow in expression">i1 = <warning descr="Numeric overflow in expression">2 / 0</warning></warning>;
<warning descr="Numeric overflow in expression">i1 = <warning descr="Numeric overflow in expression">INTEGER_MIN_VALUE / -1</warning></warning>;
<warning descr="Numeric overflow in expression">i1 = <warning descr="Numeric overflow in expression">1000 << 30</warning></warning>;
System.out.println(i1);
long l1 = <warning descr="Numeric overflow in expression">LONG_MAX_VALUE + 1</warning>;
@@ -64,10 +65,9 @@ class c {
<warning descr="Numeric overflow in expression">l1 = <warning descr="Numeric overflow in expression">2 / 0L</warning></warning>;
<warning descr="Numeric overflow in expression">l1 = <warning descr="Numeric overflow in expression">2 % 0L</warning></warning>;
<warning descr="Numeric overflow in expression">l1 = <warning descr="Numeric overflow in expression">LONG_MIN_VALUE / -1</warning></warning>;
<warning descr="Numeric overflow in expression">l1 = <warning descr="Numeric overflow in expression">30 * 24 * 60 * 60 * 1000</warning></warning>;
<warning descr="Numeric overflow in expression">l1 = <warning descr="Numeric overflow in expression"><warning descr="Numeric overflow in expression">30000000 * 243232323 * <warning descr="Numeric overflow in expression">(<warning descr="Numeric overflow in expression">LONG_MAX_VALUE +3</warning>)</warning></warning> / 5</warning></warning>;
<warning descr="Numeric overflow in expression">l1 = <warning descr="Numeric overflow in expression">1000 << 62</warning></warning>;
System.out.println(l1);
}