|
| 1 | +/** |
| 2 | + * @name Useless Test |
| 3 | + * @description A boolean condition that is guaranteed to never be evaluated should be deleted. |
| 4 | + * @kind problem |
| 5 | + * @problem.severity warning |
| 6 | + * @id cpp/uselesstest |
| 7 | + * @tags reliability |
| 8 | + * readability |
| 9 | + */ |
| 10 | + |
| 11 | +import cpp |
| 12 | +import semmle.code.cpp.valuenumbering.GlobalValueNumbering |
| 13 | + |
| 14 | +predicate sameExpr(Expr e1, Expr e2) { globalValueNumber(e1).getAnExpr() = e2 } |
| 15 | + |
| 16 | +Element nearestParent(Expr e) { |
| 17 | + if |
| 18 | + e.getParent().(Expr).getConversion*() instanceof ParenthesisExpr or |
| 19 | + e.getParent() instanceof IfStmt or |
| 20 | + e.getParent() instanceof WhileStmt |
| 21 | + then result = e.getParent() |
| 22 | + else result = nearestParent(e.getParent()) |
| 23 | +} |
| 24 | + |
| 25 | +from LogicalAndExpr b, EQExpr eq, NEExpr ne |
| 26 | +where |
| 27 | + ( |
| 28 | + b.getAChild*() = eq and |
| 29 | + b.getAChild*() = ne and |
| 30 | + eq.getParent() instanceof LogicalAndExpr and |
| 31 | + ne.getParent() instanceof LogicalAndExpr |
| 32 | + ) and |
| 33 | + ( |
| 34 | + eq.getLeftOperand() instanceof VariableAccess and ne.getLeftOperand() instanceof VariableAccess |
| 35 | + or |
| 36 | + eq.getLeftOperand() instanceof PointerDereferenceExpr and |
| 37 | + ne.getLeftOperand() instanceof PointerDereferenceExpr |
| 38 | + ) and |
| 39 | + eq.getRightOperand() instanceof Literal and |
| 40 | + ne.getRightOperand() instanceof Literal and |
| 41 | + nearestParent(eq) = nearestParent(ne) and |
| 42 | + sameExpr(eq.getLeftOperand(), ne.getLeftOperand()) |
| 43 | +select ne, "Useless Test" |
0 commit comments