Merge pull request #2175 from aschackmull/java/continue-in-false-loop

Java: Port C++ query cpp/continue-in-false-loop to Java.
This commit is contained in:
yh-semmle
2019-10-24 20:47:59 -04:00
committed by GitHub
6 changed files with 140 additions and 0 deletions

View File

@@ -0,0 +1,84 @@
public class A {
public interface Cond {
boolean cond();
}
void test1(int x, Cond c) {
int i;
// --- do loops ---
do {
if (c.cond())
continue; // BAD
if (c.cond())
break;
} while (false);
do {
if (c.cond())
continue;
if (c.cond())
break;
} while (true);
do {
if (c.cond())
continue;
if (c.cond())
break;
} while (c.cond());
// --- while, for loops ---
while (false) {
if (c.cond())
continue; // GOOD [never reached, if the condition changed so it was then the result would no longer apply]
if (c.cond())
break;
}
for (i = 0; false; i++) {
if (c.cond())
continue; // GOOD [never reached, if the condition changed so it was then the result would no longer apply]
if (c.cond())
break;
}
// --- nested loops ---
do {
do {
if (c.cond())
continue; // BAD
if (c.cond())
break;
} while (false);
if (c.cond())
break;
} while (true);
do {
do {
if (c.cond())
continue; // GOOD
if (c.cond())
break;
} while (true);
} while (false);
do {
switch (x) {
case 1:
// do [1]
break; // break out of the switch
default:
// do [2]
// break out of the loop entirely, skipping [3]
continue; // BAD; labelled break is better
};
// do [3]
} while (false);
}
}

View File

@@ -0,0 +1,3 @@
| A.java:14:9:14:17 | stmt | This 'continue' never re-runs the loop - the $@ is always false. | A.java:17:14:17:18 | false | loop condition |
| A.java:54:11:54:19 | stmt | This 'continue' never re-runs the loop - the $@ is always false. | A.java:57:16:57:20 | false | loop condition |
| A.java:79:9:79:17 | stmt | This 'continue' never re-runs the loop - the $@ is always false. | A.java:82:14:82:18 | false | loop condition |

View File

@@ -0,0 +1 @@
Likely Bugs/Statements/ContinueInFalseLoop.ql