Skip to content

Commit da57dbc

Browse files
committed
Java: Port C++ query cpp/continue-in-false-loop.
1 parent 1c79ec5 commit da57dbc

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

change-notes/1.23/analysis-java.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
The following changes in version 1.23 affect Java analysis in all applications.
44

5+
## New queries
6+
7+
| **Query** | **Tags** | **Purpose** |
8+
|-----------------------------|-----------|--------------------------------------------------------------------|
9+
| Continue statement that does not continue (`java/continue-in-false-loop`) | correctness | Finds `continue` statements in `do { ... } while (false)` loops. |
10+
511
## Changes to existing queries
612

713
| **Query** | **Expected impact** | **Change** |
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
6+
7+
<overview>
8+
<p>A <code>continue</code> statement only re-runs the loop if the loop condition is true. Therefore using <code>continue</code> in a loop with a constant false condition will never cause the loop body to be re-run, which is misleading.
9+
</p>
10+
11+
</overview>
12+
<recommendation>
13+
14+
<p>Replace the <code>continue</code> statement with a <code>break</code> statement if the intent is to break from the loop.
15+
</p>
16+
17+
</recommendation>
18+
19+
<references>
20+
<li>
21+
Java Language Specification:
22+
<a href="http://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.13">14.13 The do Statement</a>.
23+
</li>
24+
</references>
25+
</qhelp>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @name Continue statement that does not continue
3+
* @description A 'continue' statement only re-runs the loop if the
4+
* loop-condition is true. Therefore using 'continue' in a loop
5+
* with a constant false condition is misleading and usually a
6+
* bug.
7+
* @kind problem
8+
* @id java/continue-in-false-loop
9+
* @problem.severity warning
10+
* @precision high
11+
* @tags correctness
12+
*/
13+
14+
import java
15+
16+
from DoStmt do, ContinueStmt continue
17+
where
18+
do.getCondition().(BooleanLiteral).getBooleanValue() = false and
19+
continue.(JumpStmt).getTarget() = do
20+
select continue, "This 'continue' never re-runs the loop - the $@ is always false.",
21+
do.getCondition(), "loop condition"

0 commit comments

Comments
 (0)