Skip to content

Commit 89604de

Browse files
committed
add redundant inline cast query
1 parent 97461d1 commit 89604de

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import ql
2+
3+
class RedundantInlineCast extends AstNode instanceof InlineCast {
4+
Type t;
5+
6+
RedundantInlineCast() {
7+
t = unique( | | super.getType()) and
8+
(
9+
// The cast is to the type the base expression already has
10+
t = unique( | | super.getBase().getType())
11+
or
12+
// The cast is to the same type as the other expression in an equality comparison
13+
exists(ComparisonFormula comp, Expr other | comp.getOperator() = "=" |
14+
this = comp.getAnOperand() and
15+
other = comp.getAnOperand() and
16+
this != other and
17+
t = unique( | | other.getType()) and
18+
not other instanceof InlineCast // we don't want to risk both sides being "redundant"
19+
)
20+
or
21+
exists(Call call, int i, Predicate target |
22+
this = call.getArgument(i) and
23+
target = unique( | | call.getTarget()) and
24+
t = unique( | | target.getParameterType(i))
25+
)
26+
) and
27+
// noopt can require explicit casts
28+
not exists(Annotation annon | annon = this.getEnclosingPredicate().getAnAnnotation() |
29+
annon.getName() = "pragma" and
30+
annon.getArgs(0).getValue() = "noopt"
31+
)
32+
}
33+
34+
TypeExpr getTypeExpr() { result = super.getTypeExpr() }
35+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @name Redundant inline cast
3+
* @description Redundant inline casts
4+
* @kind problem
5+
* @problem.severity error
6+
* @id ql/redundant-inline-cast
7+
* @tags maintainability
8+
* @precision high
9+
*/
10+
11+
import ql
12+
import codeql_ql.style.RedundantInlineCastQuery
13+
14+
from RedundantInlineCast cast
15+
select cast, "Redundant cast to $@", cast.getTypeExpr(),
16+
cast.getTypeExpr().getResolvedType().getName()

0 commit comments

Comments
 (0)