Merge pull request #2454 from aschackmull/java/explicit-mul-zero

Java: Allow explicit zero multiplication in java/evaluation-to-constant.
This commit is contained in:
yo-h
2019-12-06 18:13:43 -05:00
committed by GitHub
4 changed files with 28 additions and 9 deletions

View File

@@ -74,6 +74,8 @@ where
not child instanceof Annotation
) and
not e instanceof CompileTimeConstantExpr and
// Exclude explicit zero multiplication.
not e.(MulExpr).getAnOperand().(IntegerLiteral).getIntValue() = 0 and
// Exclude expressions that appear to be disabled deliberately (e.g. `false && ...`).
not e.(AndLogicalExpr).getAnOperand().(BooleanLiteral).getBooleanValue() = false
select e, "Expression always evaluates to the same value."

View File

@@ -1,7 +1,7 @@
| Test.java:18:33:18:49 | ... * ... | Expression always evaluates to the same value. |
| Test.java:19:34:19:50 | ... * ... | Expression always evaluates to the same value. |
| Test.java:20:29:20:48 | ... * ... | Expression always evaluates to the same value. |
| Test.java:21:32:21:50 | ... * ... | Expression always evaluates to the same value. |
| Test.java:18:33:18:53 | ... * ... | Expression always evaluates to the same value. |
| Test.java:19:34:19:54 | ... * ... | Expression always evaluates to the same value. |
| Test.java:20:29:20:58 | ... * ... | Expression always evaluates to the same value. |
| Test.java:21:32:21:69 | ... * ... | Expression always evaluates to the same value. |
| Test.java:27:28:27:44 | ... % ... | Expression always evaluates to the same value. |
| Test.java:29:29:29:48 | ... % ... | Expression always evaluates to the same value. |
| Test.java:30:32:30:50 | ... % ... | Expression always evaluates to the same value. |

View File

@@ -15,11 +15,11 @@ class Test{
int mul_constant_left = 0 * 60 * 60 * 24; //OK
int mul_constant_right = 60 * 60 * 24 * 0; //OK
int mul_is_not_constant = rnd.nextInt() * 1; //OK
int mul_is_constant_int_left = 0 * rnd.nextInt(); //NOT OK
int mul_is_constant_int_right = rnd.nextInt() * 0; //NOT OK
long mul_is_constant_hex = rnd.nextLong() * 0x0; //NOT OK
long mul_is_constant_binary = rnd.nextLong() * 00; //NOT OK
int mul_is_constant_int_left = (0+0) * rnd.nextInt(); //NOT OK
int mul_is_constant_int_right = rnd.nextInt() * (1-1); //NOT OK
long mul_is_constant_hex = rnd.nextLong() * (0x0F & 0xF0); //NOT OK
long mul_is_constant_binary = rnd.nextLong() * (0b010101 & 0b101010); //NOT OK
int mul_explicit_zero = rnd.nextInt() * 0; //OK (deliberate zero multiplication)
//Remainder by 1
int rem_not_constant = 42 % 6; //OK
int rem_constant = 60 % 1; //OK