Skip to content

Commit 50f5c83

Browse files
authored
QL: implement ql/override-swapped-name
1 parent d8b6579 commit 50f5c83

4 files changed

Lines changed: 49 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @name Swapped parameter names in overriding predicate.
3+
* @description Swapping the parameter names in an overriding method indicates an implementation mistake.
4+
* @kind problem
5+
* @problem.severity warning
6+
* @id ql/override-swapped-name
7+
* @tags correctness
8+
* maintainability
9+
* @precision high
10+
*/
11+
12+
import ql
13+
14+
pragma[noinline]
15+
private predicate getAnOverridingParameter(
16+
ClassPredicate pred, ClassPredicate sup, VarDecl parameter, string parName, string superName,
17+
int index
18+
) {
19+
pred.overrides(sup) and
20+
parameter = pred.getParameter(index) and
21+
parameter.getName() = parName and
22+
sup.getParameter(index).getName() = superName
23+
}
24+
25+
from
26+
ClassPredicate pred, ClassPredicate sup, VarDecl parameter, string parName, string superName,
27+
int index
28+
where
29+
getAnOverridingParameter(pred, sup, parameter, parName, superName, index) and
30+
superName != parName and
31+
exists(int other | other != index | sup.getParameter(other).getName() = parName)
32+
select parameter, parName + " was $@ in the super class.", sup.getParameter(index),
33+
"named " + superName
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| Test.qll:12:27:12:35 | VarDecl | succ was $@ in the super class. | Test.qll:4:27:4:35 | VarDecl | named pred |
2+
| Test.qll:12:38:12:46 | VarDecl | pred was $@ in the super class. | Test.qll:4:38:4:46 | VarDecl | named succ |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
queries/style/SwappedParameterNames.ql
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import ql
2+
3+
class Sup extends AstNode {
4+
abstract predicate step(Expr pred, Expr succ);
5+
}
6+
7+
class Correct extends Sup {
8+
override predicate step(Expr pred, Expr succ) { none() }
9+
}
10+
11+
class Wrong extends Sup {
12+
override predicate step(Expr succ, Expr pred) { none() } // <- swapped parameter names
13+
}

0 commit comments

Comments
 (0)