Skip to content

Commit ca2ff6f

Browse files
committed
C/C++: Fixing minor issues in Useless Test query
1 parent d7313f3 commit ca2ff6f

File tree

2 files changed

+11
-19
lines changed

2 files changed

+11
-19
lines changed

cpp/ql/src/experimental/Best Practices/UselessTest.qhelp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
<qhelp>
55

66
<overview>
7-
<p>Comparisons operation like <code>a==8 && a!=7</code> contains an useless part : the non-equal part. This rule finds any test of this kind within an if or a while statement
8-
This rule will only find useless comparisons with a right literal operand. </p>
7+
<p>Comparison operations like <code>a==8 &amp;&amp; a!=7</code> contain a useless part : the non-equal part. This rule finds tests of this kind within an <code>if</code> or a <code>while</code> statement</p>
98
</overview>
109

1110
<recommendation>
Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @name Useless Test
3-
* @description Find any useless test of kind a==8 && a!=7
3+
* @description A boolean condition that is guaranteed to never be evaluated should be deleted.
44
* @kind problem
55
* @problem.severity warning
66
* @id cpp/uselesstest
@@ -9,16 +9,9 @@
99
*/
1010

1111
import cpp
12+
import semmle.code.cpp.valuenumbering.GlobalValueNumbering
1213

13-
predicate sameStmt(Expr e1, Expr e2) {
14-
e1.getNumChild() = e2.getNumChild() and
15-
e1.toString() = e2.toString() and
16-
(
17-
e1.getNumChild() = 0
18-
or
19-
forall(int i | i in [0 .. e1.getNumChild() - 1] | sameStmt(e1.getChild(i), e2.getChild(i)))
20-
)
21-
}
14+
predicate sameExpr(Expr e1, Expr e2) { globalValueNumber(e1).getAnExpr() = e2 }
2215

2316
Element nearestParent(Expr e) {
2417
if
@@ -38,13 +31,13 @@ where
3831
ne.getParent() instanceof LogicalAndExpr
3932
) and
4033
(
41-
eq.getChild(0) instanceof VariableAccess and ne.getChild(0) instanceof VariableAccess
34+
eq.getLeftOperand() instanceof VariableAccess and ne.getLeftOperand() instanceof VariableAccess
4235
or
43-
eq.getChild(0) instanceof PointerDereferenceExpr and
44-
ne.getChild(0) instanceof PointerDereferenceExpr
36+
eq.getLeftOperand() instanceof PointerDereferenceExpr and
37+
ne.getLeftOperand() instanceof PointerDereferenceExpr
4538
) and
46-
eq.getChild(1) instanceof Literal and
47-
ne.getChild(1) instanceof Literal and
39+
eq.getRightOperand() instanceof Literal and
40+
ne.getRightOperand() instanceof Literal and
4841
nearestParent(eq) = nearestParent(ne) and
49-
sameStmt(eq.getChild(0), ne.getChild(0))
50-
select "Useless test", ne
42+
sameExpr(eq.getLeftOperand(), ne.getLeftOperand())
43+
select "Useless test", ne

0 commit comments

Comments
 (0)