mirror of
https://github.com/github/codeql.git
synced 2026-04-30 11:15:13 +02:00
C/C++: Fixing minor issues in Useless Test query
This commit is contained in:
@@ -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 && 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>
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user