QL: Add redundant overrides query

This commit is contained in:
Tom Hvitved
2022-08-18 14:59:04 +02:00
parent 07e0bd3ce1
commit e5911df697
4 changed files with 93 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
/**
* @name Redundant override
* @description Redundant override
* @kind problem
* @problem.severity warning
* @id ql/redundant-override
* @tags maintainability
* @precision high
*/
import ql
private predicate redundantOverride(ClassPredicate pred, ClassPredicate sup) {
pred.overrides(sup) and
// Can be made more precise, but rules out overrides needed for disambiguation
count(pred.getDeclaringType().getASuperType()) <= 1 and
exists(MemberCall mc |
mc.getBase() instanceof Super and
mc.getTarget() = sup and
not exists(pred.getQLDoc())
|
pred.getBody() =
any(ComparisonFormula comp |
comp.getOperator() = "=" and
comp.getAnOperand() instanceof ResultAccess and
comp.getAnOperand() = mc and
pred.getReturnType() = sup.getReturnType()
)
or
pred.getBody() = mc
)
}
from ClassPredicate pred, ClassPredicate sup
where redundantOverride(pred, sup)
select pred, "Redundant override of $@ predicate", sup, "this"

View File

@@ -0,0 +1,4 @@
| RedundantOverride.qll:12:16:12:19 | ClassPredicate pred | Redundant override of $@ predicate | RedundantOverride.qll:4:7:4:10 | ClassPredicate pred | this |
| RedundantOverride.qll:16:16:16:19 | ClassPredicate pred | Redundant override of $@ predicate | RedundantOverride.qll:4:7:4:10 | ClassPredicate pred | this |
| RedundantOverride.qll:47:22:47:26 | ClassPredicate pred3 | Redundant override of $@ predicate | RedundantOverride.qll:8:13:8:17 | ClassPredicate pred3 | this |
| RedundantOverride.qll:51:16:51:19 | ClassPredicate pred | Redundant override of $@ predicate | RedundantOverride.qll:4:7:4:10 | ClassPredicate pred | this |

View File

@@ -0,0 +1,52 @@
class Foo extends string {
Foo() { this = "Foo" }
Foo pred() { none() }
Foo pred2() { none() }
predicate pred3() { none() }
}
class Bar1 extends Foo {
override Foo pred() { result = Foo.super.pred() } // BAD
}
class Bar2 extends Foo {
override Foo pred() { result = super.pred() } // BAD
}
class Bar3 extends Foo {
override Bar3 pred() { result = super.pred() } // GOOD (refined return type)
}
class Bar4 extends Foo {
override Foo pred() { any() } // GOOD
}
class Bar5 extends Foo {
/** My own overriding QL doc. */
override Foo pred() { result = super.pred() } // GOOD
}
class Bar6 extends Foo {
override Foo pred() { result = super.pred2() } // GOOD
}
class Bar7 extends string {
Bar7() { this = "Bar7" }
Foo pred() { any() }
}
class Bar8 extends Foo, Bar7 {
override Foo pred() { result = Foo.super.pred() } // GOOD
}
class Bar9 extends Foo {
override predicate pred3() { super.pred3() } // BAD
}
class Bar10 extends Foo {
override Foo pred() { Foo.super.pred() = result } // BAD
}

View File

@@ -0,0 +1 @@
queries/style/RedundantOverride.ql