C/C++: Fixing minor issues in Useless Test query

This commit is contained in:
BACK Yonah
2022-01-31 16:04:56 +01:00
parent d7313f3a82
commit ca2ff6f9fb
2 changed files with 11 additions and 19 deletions

View File

@@ -4,8 +4,7 @@
<qhelp>
<overview>
<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
This rule will only find useless comparisons with a right literal operand. </p>
<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>
</overview>
<recommendation>

View File

@@ -1,6 +1,6 @@
/**
* @name Useless Test
* @description Find any useless test of kind a==8 && a!=7
* @description A boolean condition that is guaranteed to never be evaluated should be deleted.
* @kind problem
* @problem.severity warning
* @id cpp/uselesstest
@@ -9,16 +9,9 @@
*/
import cpp
import semmle.code.cpp.valuenumbering.GlobalValueNumbering
predicate sameStmt(Expr e1, Expr e2) {
e1.getNumChild() = e2.getNumChild() and
e1.toString() = e2.toString() and
(
e1.getNumChild() = 0
or
forall(int i | i in [0 .. e1.getNumChild() - 1] | sameStmt(e1.getChild(i), e2.getChild(i)))
)
}
predicate sameExpr(Expr e1, Expr e2) { globalValueNumber(e1).getAnExpr() = e2 }
Element nearestParent(Expr e) {
if
@@ -38,13 +31,13 @@ where
ne.getParent() instanceof LogicalAndExpr
) and
(
eq.getChild(0) instanceof VariableAccess and ne.getChild(0) instanceof VariableAccess
eq.getLeftOperand() instanceof VariableAccess and ne.getLeftOperand() instanceof VariableAccess
or
eq.getChild(0) instanceof PointerDereferenceExpr and
ne.getChild(0) instanceof PointerDereferenceExpr
eq.getLeftOperand() instanceof PointerDereferenceExpr and
ne.getLeftOperand() instanceof PointerDereferenceExpr
) and
eq.getChild(1) instanceof Literal and
ne.getChild(1) instanceof Literal and
eq.getRightOperand() instanceof Literal and
ne.getRightOperand() instanceof Literal and
nearestParent(eq) = nearestParent(ne) and
sameStmt(eq.getChild(0), ne.getChild(0))
select "Useless test", ne
sameExpr(eq.getLeftOperand(), ne.getLeftOperand())
select "Useless test", ne