Skip to content

Commit e5911df

Browse files
committed
QL: Add redundant overrides query
1 parent 07e0bd3 commit e5911df

File tree

4 files changed

+93
-0
lines changed

4 files changed

+93
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @name Redundant override
3+
* @description Redundant override
4+
* @kind problem
5+
* @problem.severity warning
6+
* @id ql/redundant-override
7+
* @tags maintainability
8+
* @precision high
9+
*/
10+
11+
import ql
12+
13+
private predicate redundantOverride(ClassPredicate pred, ClassPredicate sup) {
14+
pred.overrides(sup) and
15+
// Can be made more precise, but rules out overrides needed for disambiguation
16+
count(pred.getDeclaringType().getASuperType()) <= 1 and
17+
exists(MemberCall mc |
18+
mc.getBase() instanceof Super and
19+
mc.getTarget() = sup and
20+
not exists(pred.getQLDoc())
21+
|
22+
pred.getBody() =
23+
any(ComparisonFormula comp |
24+
comp.getOperator() = "=" and
25+
comp.getAnOperand() instanceof ResultAccess and
26+
comp.getAnOperand() = mc and
27+
pred.getReturnType() = sup.getReturnType()
28+
)
29+
or
30+
pred.getBody() = mc
31+
)
32+
}
33+
34+
from ClassPredicate pred, ClassPredicate sup
35+
where redundantOverride(pred, sup)
36+
select pred, "Redundant override of $@ predicate", sup, "this"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| RedundantOverride.qll:12:16:12:19 | ClassPredicate pred | Redundant override of $@ predicate | RedundantOverride.qll:4:7:4:10 | ClassPredicate pred | this |
2+
| RedundantOverride.qll:16:16:16:19 | ClassPredicate pred | Redundant override of $@ predicate | RedundantOverride.qll:4:7:4:10 | ClassPredicate pred | this |
3+
| RedundantOverride.qll:47:22:47:26 | ClassPredicate pred3 | Redundant override of $@ predicate | RedundantOverride.qll:8:13:8:17 | ClassPredicate pred3 | this |
4+
| RedundantOverride.qll:51:16:51:19 | ClassPredicate pred | Redundant override of $@ predicate | RedundantOverride.qll:4:7:4:10 | ClassPredicate pred | this |
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class Foo extends string {
2+
Foo() { this = "Foo" }
3+
4+
Foo pred() { none() }
5+
6+
Foo pred2() { none() }
7+
8+
predicate pred3() { none() }
9+
}
10+
11+
class Bar1 extends Foo {
12+
override Foo pred() { result = Foo.super.pred() } // BAD
13+
}
14+
15+
class Bar2 extends Foo {
16+
override Foo pred() { result = super.pred() } // BAD
17+
}
18+
19+
class Bar3 extends Foo {
20+
override Bar3 pred() { result = super.pred() } // GOOD (refined return type)
21+
}
22+
23+
class Bar4 extends Foo {
24+
override Foo pred() { any() } // GOOD
25+
}
26+
27+
class Bar5 extends Foo {
28+
/** My own overriding QL doc. */
29+
override Foo pred() { result = super.pred() } // GOOD
30+
}
31+
32+
class Bar6 extends Foo {
33+
override Foo pred() { result = super.pred2() } // GOOD
34+
}
35+
36+
class Bar7 extends string {
37+
Bar7() { this = "Bar7" }
38+
39+
Foo pred() { any() }
40+
}
41+
42+
class Bar8 extends Foo, Bar7 {
43+
override Foo pred() { result = Foo.super.pred() } // GOOD
44+
}
45+
46+
class Bar9 extends Foo {
47+
override predicate pred3() { super.pred3() } // BAD
48+
}
49+
50+
class Bar10 extends Foo {
51+
override Foo pred() { Foo.super.pred() = result } // BAD
52+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
queries/style/RedundantOverride.ql

0 commit comments

Comments
 (0)