Skip to content

Commit 870521b

Browse files
authored
Merge pull request #8473 from erik-krogh/redundantAnyCast
QL: expand redundant-inline-cast, and rename to redundant-cast
2 parents 86398a8 + fe94421 commit 870521b

File tree

4 files changed

+100
-51
lines changed

4 files changed

+100
-51
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 this.getEnclosingPredicate().getAnAnnotation() instanceof NoOpt
29+
}
30+
31+
TypeExpr getTypeExpr() { result = super.getTypeExpr() }
32+
}
33+
34+
// `any(Bar b)`.
35+
private class AnyCast extends AstNode instanceof FullAggregate {
36+
TypeExpr type;
37+
38+
AnyCast() {
39+
super.getKind() = "any" and
40+
not exists(super.getRange()) and
41+
not exists(super.getExpr(_)) and
42+
count(super.getArgument(_)) = 1 and
43+
type = super.getArgument(0).getTypeExpr()
44+
}
45+
46+
TypeExpr getTypeExpr() { result = type }
47+
}
48+
49+
// `foo = any(Bar b)` is effectively a cast to `Bar`.
50+
class RedundantAnyCast extends AstNode instanceof ComparisonFormula {
51+
AnyCast cast;
52+
Expr operand;
53+
54+
RedundantAnyCast() {
55+
super.getOperator() = "=" and
56+
super.getAnOperand() = cast and
57+
super.getAnOperand() = operand and
58+
cast != operand and
59+
unique( | | operand.getType()).getASuperType*() =
60+
unique( | | cast.getTypeExpr().getResolvedType()) and
61+
not this.getEnclosingPredicate().getAnAnnotation() instanceof NoOpt
62+
}
63+
64+
TypeExpr getTypeExpr() { result = cast.getTypeExpr() }
65+
}
66+
67+
// foo instanceof Bar
68+
class RedundantInstanceof extends AstNode instanceof InstanceOf {
69+
RedundantInstanceof() {
70+
unique( | | super.getExpr().getType()).getASuperType*() =
71+
unique( | | super.getType().getResolvedType()) and
72+
not this.getEnclosingPredicate().getAnAnnotation() instanceof NoOpt
73+
}
74+
75+
TypeExpr getTypeExpr() { result = super.getType() }
76+
}
77+
78+
predicate redundantCast(AstNode n, TypeExpr type) {
79+
n.(RedundantInlineCast).getTypeExpr() = type
80+
or
81+
n.(RedundantAnyCast).getTypeExpr() = type
82+
or
83+
n.(RedundantInstanceof).getTypeExpr() = type
84+
}

ql/ql/src/codeql_ql/style/RedundantInlineCastQuery.qll

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @name Redundant cast
3+
* @description Redundant casts
4+
* @kind problem
5+
* @problem.severity warning
6+
* @id ql/redundant-cast
7+
* @tags maintainability
8+
* @precision high
9+
*/
10+
11+
import ql
12+
import codeql_ql.style.RedundantCastQuery
13+
14+
from AstNode node, TypeExpr type
15+
where redundantCast(node, type)
16+
select node, "Redundant cast to $@", type, type.getResolvedType().getName()

ql/ql/src/queries/style/RedundantInlineCast.ql

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)