Skip to content

Commit 30d896b

Browse files
committed
QL: make the alert-message more precise when the type-cast is also redundant
1 parent 14d2f5f commit 30d896b

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

ql/ql/src/queries/style/CouldBeCast.ql

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111
import ql
1212
import codeql_ql.style.CouldBeCastQuery
1313

14-
from AstNode aggr, VarDecl var, string msg
14+
from AstNode aggr, VarDecl var, string msg, Expr operand
1515
where
16-
exists(string kind | aggregateCouldBeCast(aggr, _, kind, var, _) |
16+
exists(string kind | aggregateCouldBeCast(aggr, _, kind, var, operand) |
1717
kind = "exists" and
18-
msg = "The assignment to $@ in the exists(..) can replaced with an instanceof expression."
18+
if operand.getType().getASuperType*() = var.getType()
19+
then msg = "The assignment in the exists(..) is redundant."
20+
else msg = "The assignment to $@ in the exists(..) can replaced with an instanceof expression."
1921
or
2022
kind = "any" and
21-
msg = "The assignment to $@ in this any(..) can be replaced with an inline cast."
23+
if operand.getType().getASuperType*() = var.getType()
24+
then msg = "The assignment in the any(..) is redundant."
25+
else msg = "The assignment to $@ in this any(..) can be replaced with an inline cast."
2226
)
2327
select aggr, msg, var, var.getName()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
| Foo.qll:3:3:3:24 | Exists | The assignment to $@ in the exists(..) can replaced with an instanceof expression. | Foo.qll:3:10:3:15 | j | j |
22
| Foo.qll:7:3:7:21 | Any | The assignment to $@ in this any(..) can be replaced with an inline cast. | Foo.qll:7:7:7:12 | j | j |
33
| Foo.qll:9:3:9:25 | Any | The assignment to $@ in this any(..) can be replaced with an inline cast. | Foo.qll:9:7:9:12 | j | j |
4+
| Foo.qll:15:3:15:20 | Any | The assignment in the any(..) is redundant. | Foo.qll:15:7:15:11 | j | j |
5+
| Foo.qll:17:3:17:23 | Exists | The assignment in the exists(..) is redundant. | Foo.qll:17:10:17:14 | j | j |

ql/ql/test/queries/style/CouldBeCast/Foo.qll

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ predicate foo(int i) {
1111
any(Even j | j = i | j * 2) = 4 // OK
1212
or
1313
any(Even j | j = i and j % 4 = 0 | j) = 4 // OK
14+
or
15+
any(int j | j = i) = 2 // NOT OK
16+
or
17+
exists(int j | j = i) // NOT OK
1418
}
1519

1620
class Even extends int {

0 commit comments

Comments
 (0)