-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathDuplicateCondition.ql
More file actions
34 lines (30 loc) · 1.03 KB
/
DuplicateCondition.ql
File metadata and controls
34 lines (30 loc) · 1.03 KB
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
34
/**
* @name Duplicate 'if' condition
* @description If two conditions in an 'if'-'else if' chain are identical, the
* second condition will never hold.
* @kind problem
* @problem.severity error
* @id go/duplicate-condition
* @tags quality
* reliability
* correctness
* external/cwe/cwe-561
* @precision very-high
*/
import go
/** Gets the `i`th condition in the `if`-`else if` chain starting at `stmt`. */
Expr getCondition(IfStmt stmt, int i) {
i = 0 and result = stmt.getCond()
or
exists(IfStmt elsif | elsif = stmt.getElse() |
not exists(elsif.getInit()) and
result = getCondition(stmt.getElse(), i - 1)
)
}
/** Gets the global value number of `e`, which is the `i`th condition of `is`. */
GVN conditionGvn(IfStmt is, int i, Expr e) {
e = getCondition(is, i) and result = e.getGlobalValueNumber()
}
from IfStmt is, Expr e, Expr f, int i, int j
where conditionGvn(is, i, e) = conditionGvn(is, j, f) and i < j
select f, "This condition is a duplicate of an $@.", e, "earlier condition"