Skip to content

Commit f6c8e9a

Browse files
committed
don't require a member to call a range method before suggesting to use instanceof
1 parent 7615668 commit f6c8e9a

File tree

5 files changed

+35
-20
lines changed

5 files changed

+35
-20
lines changed

ql/ql/src/codeql_ql/style/UseInstanceofExtensionQuery.qll

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,6 @@ predicate instanceofThisInCharPred(Class c, Type type) {
1717
)
1818
}
1919

20-
/**
21-
* Holds if `c` uses the casting based range pattern, which could be replaced with `instanceof type`.
22-
*/
23-
predicate usesCastingBasedInstanceof(Class c, Type type) {
24-
instanceofThisInCharPred(c, type) and
25-
// require that there is a call to the range class that matches the name of the enclosing predicate
26-
exists(InlineCast cast, MemberCall call |
27-
cast = getAThisCast(c, type) and
28-
call.getBase() = cast and
29-
cast.getEnclosingPredicate().getName() = call.getMemberName()
30-
)
31-
}
32-
3320
/** Gets an inline cast that cases `this` to `type` inside a class predicate for `c`. */
3421
InlineCast getAThisCast(Class c, Type type) {
3522
exists(MemberCall call |
@@ -51,12 +38,6 @@ predicate usesFieldBasedInstanceof(Class c, TypeExpr type, FieldDecl field, Comp
5138
comp.getAnOperand() = fieldAccess and
5239
fieldAccess.getDeclaration() = field and
5340
field.getVarDecl().getTypeExpr() = type
54-
) and
55-
// require that there is a call to the range field that matches the name of the enclosing predicate
56-
exists(FieldAccess access, MemberCall call |
57-
access = getARangeFieldAccess(c, field, _) and
58-
call.getBase() = access and
59-
access.getEnclosingPredicate().getName() = call.getMemberName()
6041
)
6142
}
6243

ql/ql/src/queries/style/UseInstanceofExtension.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import codeql_ql.style.UseInstanceofExtensionQuery
1414
from Class c, Type type, string message
1515
where
1616
(
17-
usesCastingBasedInstanceof(c, type) or
17+
instanceofThisInCharPred(c, type) or
1818
usesFieldBasedInstanceof(c, any(TypeExpr te | te.getResolvedType() = type), _, _)
1919
) and
2020
message = "Consider defining this class as non-extending subtype of $@."
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Range extends string {
2+
Range() { this = "ql" }
3+
4+
string getAChild() { result = "test" }
5+
}
6+
7+
class Inst extends string {
8+
Range range;
9+
10+
Inst() { this = range }
11+
12+
string getAChild() { result = range.getAChild() }
13+
}
14+
15+
class Inst2 extends string {
16+
Inst2() { this instanceof Range }
17+
18+
string getAChild() { result = this.(Range).getAChild() }
19+
}
20+
21+
class Inst3 extends string {
22+
Range range;
23+
24+
Inst3() { this = range }
25+
}
26+
27+
class Inst4 extends string {
28+
Inst4() { this instanceof Range }
29+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
| Foo.qll:7:7:7:10 | Class Inst | Consider defining this class as non-extending subtype of $@. | Foo.qll:1:7:1:11 | Class Range | Range |
2+
| Foo.qll:15:7:15:11 | Class Inst2 | Consider defining this class as non-extending subtype of $@. | Foo.qll:1:7:1:11 | Class Range | Range |
3+
| Foo.qll:21:7:21:11 | Class Inst3 | Consider defining this class as non-extending subtype of $@. | Foo.qll:1:7:1:11 | Class Range | Range |
4+
| Foo.qll:27:7:27:11 | Class Inst4 | Consider defining this class as non-extending subtype of $@. | Foo.qll:1:7:1:11 | Class Range | Range |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
queries/style/UseInstanceofExtension.ql

0 commit comments

Comments
 (0)