-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathContinueInFalseLoop.ql
More file actions
33 lines (29 loc) · 938 Bytes
/
ContinueInFalseLoop.ql
File metadata and controls
33 lines (29 loc) · 938 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* @name Continue statement that does not continue
* @description A 'continue' statement only re-runs the loop if the loop-condition is true. Therefore
* using 'continue' in a loop with a constant false condition is misleading and usually
* a bug.
* @kind problem
* @id cpp/continue-in-false-loop
* @problem.severity warning
*/
import cpp
Loop getAFalseLoop() {
result.getControllingExpr().getValue() = "0"
and not result.getControllingExpr().isAffectedByMacro()
}
Loop enclosingLoop(Stmt s) {
exists(Stmt parent |
parent = s.getParent() and
if parent instanceof Loop then
result = parent
else
result = enclosingLoop(parent))
}
from Loop loop, ContinueStmt continue
where loop = getAFalseLoop()
and loop = enclosingLoop(continue)
select continue,
"This 'continue' never re-runs the loop - the $@ is always false.",
loop.getControllingExpr(),
"loop condition"