add redundant inline cast query

This commit is contained in:
Erik Krogh Kristensen
2021-11-18 12:23:55 +01:00
parent 97461d1f11
commit 89604deb8d
2 changed files with 51 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
import ql
class RedundantInlineCast extends AstNode instanceof InlineCast {
Type t;
RedundantInlineCast() {
t = unique( | | super.getType()) and
(
// The cast is to the type the base expression already has
t = unique( | | super.getBase().getType())
or
// The cast is to the same type as the other expression in an equality comparison
exists(ComparisonFormula comp, Expr other | comp.getOperator() = "=" |
this = comp.getAnOperand() and
other = comp.getAnOperand() and
this != other and
t = unique( | | other.getType()) and
not other instanceof InlineCast // we don't want to risk both sides being "redundant"
)
or
exists(Call call, int i, Predicate target |
this = call.getArgument(i) and
target = unique( | | call.getTarget()) and
t = unique( | | target.getParameterType(i))
)
) and
// noopt can require explicit casts
not exists(Annotation annon | annon = this.getEnclosingPredicate().getAnAnnotation() |
annon.getName() = "pragma" and
annon.getArgs(0).getValue() = "noopt"
)
}
TypeExpr getTypeExpr() { result = super.getTypeExpr() }
}

View File

@@ -0,0 +1,16 @@
/**
* @name Redundant inline cast
* @description Redundant inline casts
* @kind problem
* @problem.severity error
* @id ql/redundant-inline-cast
* @tags maintainability
* @precision high
*/
import ql
import codeql_ql.style.RedundantInlineCastQuery
from RedundantInlineCast cast
select cast, "Redundant cast to $@", cast.getTypeExpr(),
cast.getTypeExpr().getResolvedType().getName()