Skip to content

Commit 0610c26

Browse files
authored
Merge pull request #17913 from geoffw0/unusedvar8
Rust: Fix rust/unused-variable FPs
2 parents 9b6c967 + 7c2c5ee commit 0610c26

File tree

8 files changed

+197
-124
lines changed

8 files changed

+197
-124
lines changed

rust/ql/lib/codeql/rust/elements/internal/VariableImpl.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ module Impl {
8787
not name.charAt(0).isUppercase() and
8888
// exclude parameters from functions without a body as these are trait method declarations
8989
// without implementations
90-
not exists(Function f | not f.hasBody() and f.getParamList().getAParam().getPat() = p)
90+
not exists(Function f | not f.hasBody() and f.getParamList().getAParam().getPat() = p) and
91+
// exclude parameters from function pointer types (e.g. `x` in `fn(x: i32) -> i32`)
92+
not exists(FnPtrType fp | fp.getParamList().getParam(_).getPat() = p)
9193
}
9294

9395
/** A variable. */

rust/ql/src/queries/unusedentities/UnusedVariable.ql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@ import rust
1212
import UnusedVariable
1313

1414
from Variable v
15-
where isUnused(v)
15+
where
16+
isUnused(v) and
17+
not isAllowableUnused(v) and
18+
not v instanceof DiscardVariable
1619
select v, "Variable '" + v + "' is not used."
Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
import rust
22

3-
/** A deliberately unused variable. */
3+
/**
4+
* A deliberately unused variable, for example `_` or `_x`.
5+
*/
46
class DiscardVariable extends Variable {
57
DiscardVariable() { this.getName().charAt(0) = "_" }
68
}
79

8-
/** Holds if variable `v` is unused. */
10+
/**
11+
* Holds if variable `v` is unused.
12+
*/
913
predicate isUnused(Variable v) {
14+
// variable is not accessed or initialized
1015
not exists(v.getAnAccess()) and
11-
not exists(v.getInitializer()) and
12-
not v instanceof DiscardVariable and
13-
not v.getPat().isInMacroExpansion()
16+
not exists(v.getInitializer())
17+
}
18+
19+
/**
20+
* Holds if variable `v` is in a context where we may not find a use for it,
21+
* but that's expected and should not be considered a problem.
22+
*/
23+
predicate isAllowableUnused(Variable v) {
24+
// in a macro expansion
25+
v.getPat().isInMacroExpansion()
1426
}

rust/ql/test/query-tests/unusedentities/CONSISTENCY/DataFlowConsistency.expected

Lines changed: 91 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,100 @@
11
uniqueEnclosingCallable
2-
| main.rs:382:19:382:21 | Param | Node should have one enclosing callable but has 0. |
3-
| main.rs:430:29:430:38 | Param | Node should have one enclosing callable but has 0. |
4-
| main.rs:430:41:430:56 | Param | Node should have one enclosing callable but has 0. |
2+
| main.rs:392:19:392:21 | Param | Node should have one enclosing callable but has 0. |
3+
| main.rs:440:29:440:38 | Param | Node should have one enclosing callable but has 0. |
4+
| main.rs:440:41:440:56 | Param | Node should have one enclosing callable but has 0. |
5+
| main.rs:493:16:493:18 | Param | Node should have one enclosing callable but has 0. |
6+
| main.rs:494:16:494:21 | Param | Node should have one enclosing callable but has 0. |
7+
| main.rs:495:16:496:25 | Param | Node should have one enclosing callable but has 0. |
8+
| main.rs:496:12:496:17 | Param | Node should have one enclosing callable but has 0. |
9+
| main.rs:499:18:499:23 | Param | Node should have one enclosing callable but has 0. |
10+
| main.rs:502:24:502:29 | Param | Node should have one enclosing callable but has 0. |
511
| more.rs:4:23:4:28 | Param | Node should have one enclosing callable but has 0. |
612
| more.rs:8:19:8:24 | Param | Node should have one enclosing callable but has 0. |
713
uniqueCallEnclosingCallable
8-
| main.rs:11:13:11:29 | CallExpr | Call should have one enclosing callable but has 0. |
9-
| main.rs:12:13:12:29 | CallExpr | Call should have one enclosing callable but has 0. |
10-
| main.rs:16:14:16:24 | CallExpr | Call should have one enclosing callable but has 0. |
11-
| main.rs:18:8:18:13 | CallExpr | Call should have one enclosing callable but has 0. |
12-
| main.rs:19:18:19:28 | CallExpr | Call should have one enclosing callable but has 0. |
13-
| main.rs:22:14:22:24 | CallExpr | Call should have one enclosing callable but has 0. |
14-
| main.rs:23:5:23:19 | CallExpr | Call should have one enclosing callable but has 0. |
15-
| main.rs:23:5:23:19 | CallExpr | Call should have one enclosing callable but has 0. |
16-
| main.rs:41:14:41:24 | CallExpr | Call should have one enclosing callable but has 0. |
17-
| main.rs:45:8:45:13 | CallExpr | Call should have one enclosing callable but has 0. |
18-
| main.rs:50:14:50:24 | CallExpr | Call should have one enclosing callable but has 0. |
19-
| main.rs:53:8:53:13 | CallExpr | Call should have one enclosing callable but has 0. |
20-
| main.rs:58:14:58:24 | CallExpr | Call should have one enclosing callable but has 0. |
21-
| main.rs:62:14:62:24 | CallExpr | Call should have one enclosing callable but has 0. |
22-
| main.rs:67:12:67:17 | CallExpr | Call should have one enclosing callable but has 0. |
23-
| main.rs:68:12:68:17 | CallExpr | Call should have one enclosing callable but has 0. |
14+
| main.rs:13:13:13:29 | CallExpr | Call should have one enclosing callable but has 0. |
15+
| main.rs:14:13:14:29 | CallExpr | Call should have one enclosing callable but has 0. |
16+
| main.rs:18:14:18:24 | CallExpr | Call should have one enclosing callable but has 0. |
17+
| main.rs:20:8:20:13 | CallExpr | Call should have one enclosing callable but has 0. |
18+
| main.rs:21:18:21:28 | CallExpr | Call should have one enclosing callable but has 0. |
19+
| main.rs:24:14:24:24 | CallExpr | Call should have one enclosing callable but has 0. |
20+
| main.rs:25:5:25:19 | CallExpr | Call should have one enclosing callable but has 0. |
21+
| main.rs:25:5:25:19 | CallExpr | Call should have one enclosing callable but has 0. |
22+
| main.rs:43:14:43:24 | CallExpr | Call should have one enclosing callable but has 0. |
23+
| main.rs:47:8:47:13 | CallExpr | Call should have one enclosing callable but has 0. |
24+
| main.rs:52:14:52:24 | CallExpr | Call should have one enclosing callable but has 0. |
25+
| main.rs:55:8:55:13 | CallExpr | Call should have one enclosing callable but has 0. |
26+
| main.rs:60:14:60:24 | CallExpr | Call should have one enclosing callable but has 0. |
27+
| main.rs:64:14:64:24 | CallExpr | Call should have one enclosing callable but has 0. |
2428
| main.rs:69:12:69:17 | CallExpr | Call should have one enclosing callable but has 0. |
25-
| main.rs:70:14:70:24 | CallExpr | Call should have one enclosing callable but has 0. |
26-
| main.rs:96:14:96:45 | CallExpr | Call should have one enclosing callable but has 0. |
27-
| main.rs:99:14:99:38 | CallExpr | Call should have one enclosing callable but has 0. |
29+
| main.rs:70:12:70:17 | CallExpr | Call should have one enclosing callable but has 0. |
30+
| main.rs:71:12:71:17 | CallExpr | Call should have one enclosing callable but has 0. |
31+
| main.rs:72:14:72:24 | CallExpr | Call should have one enclosing callable but has 0. |
32+
| main.rs:98:14:98:45 | CallExpr | Call should have one enclosing callable but has 0. |
2833
| main.rs:101:14:101:38 | CallExpr | Call should have one enclosing callable but has 0. |
29-
| main.rs:114:14:114:32 | CallExpr | Call should have one enclosing callable but has 0. |
30-
| main.rs:117:18:117:33 | CallExpr | Call should have one enclosing callable but has 0. |
31-
| main.rs:174:18:174:29 | CallExpr | Call should have one enclosing callable but has 0. |
32-
| main.rs:178:18:178:31 | CallExpr | Call should have one enclosing callable but has 0. |
33-
| main.rs:182:18:182:37 | CallExpr | Call should have one enclosing callable but has 0. |
34-
| main.rs:187:22:187:33 | CallExpr | Call should have one enclosing callable but has 0. |
35-
| main.rs:192:18:192:27 | CallExpr | Call should have one enclosing callable but has 0. |
36-
| main.rs:196:21:196:30 | CallExpr | Call should have one enclosing callable but has 0. |
37-
| main.rs:196:21:196:30 | CallExpr | Call should have one enclosing callable but has 0. |
38-
| main.rs:200:18:200:38 | CallExpr | Call should have one enclosing callable but has 0. |
39-
| main.rs:204:9:204:24 | CallExpr | Call should have one enclosing callable but has 0. |
40-
| main.rs:204:9:204:24 | CallExpr | Call should have one enclosing callable but has 0. |
41-
| main.rs:208:9:208:24 | CallExpr | Call should have one enclosing callable but has 0. |
42-
| main.rs:213:9:213:32 | CallExpr | Call should have one enclosing callable but has 0. |
43-
| main.rs:213:20:213:24 | CallExpr | Call should have one enclosing callable but has 0. |
44-
| main.rs:213:27:213:31 | CallExpr | Call should have one enclosing callable but has 0. |
45-
| main.rs:240:22:240:29 | CallExpr | Call should have one enclosing callable but has 0. |
46-
| main.rs:243:22:243:29 | CallExpr | Call should have one enclosing callable but has 0. |
47-
| main.rs:247:20:247:27 | CallExpr | Call should have one enclosing callable but has 0. |
48-
| main.rs:254:21:254:28 | CallExpr | Call should have one enclosing callable but has 0. |
49-
| main.rs:260:13:260:20 | CallExpr | Call should have one enclosing callable but has 0. |
50-
| main.rs:267:13:267:20 | CallExpr | Call should have one enclosing callable but has 0. |
51-
| main.rs:275:13:275:28 | CallExpr | Call should have one enclosing callable but has 0. |
52-
| main.rs:282:13:282:30 | CallExpr | Call should have one enclosing callable but has 0. |
53-
| main.rs:289:31:289:37 | CallExpr | Call should have one enclosing callable but has 0. |
54-
| main.rs:337:21:337:51 | CallExpr | Call should have one enclosing callable but has 0. |
55-
| main.rs:338:5:338:39 | CallExpr | Call should have one enclosing callable but has 0. |
56-
| main.rs:340:58:340:92 | CallExpr | Call should have one enclosing callable but has 0. |
57-
| main.rs:340:61:340:91 | CallExpr | Call should have one enclosing callable but has 0. |
58-
| main.rs:343:22:343:59 | CallExpr | Call should have one enclosing callable but has 0. |
59-
| main.rs:346:22:346:29 | CallExpr | Call should have one enclosing callable but has 0. |
60-
| main.rs:396:13:396:24 | CallExpr | Call should have one enclosing callable but has 0. |
61-
| main.rs:400:13:400:22 | CallExpr | Call should have one enclosing callable but has 0. |
62-
| main.rs:460:9:464:10 | CallExpr | Call should have one enclosing callable but has 0. |
63-
| main.rs:471:5:471:14 | CallExpr | Call should have one enclosing callable but has 0. |
64-
| main.rs:472:5:472:14 | CallExpr | Call should have one enclosing callable but has 0. |
65-
| main.rs:473:5:473:13 | CallExpr | Call should have one enclosing callable but has 0. |
66-
| main.rs:474:5:474:12 | CallExpr | Call should have one enclosing callable but has 0. |
67-
| main.rs:475:5:475:13 | CallExpr | Call should have one enclosing callable but has 0. |
68-
| main.rs:476:14:476:54 | CallExpr | Call should have one enclosing callable but has 0. |
69-
| main.rs:476:36:476:54 | CallExpr | Call should have one enclosing callable but has 0. |
70-
| main.rs:477:5:477:11 | CallExpr | Call should have one enclosing callable but has 0. |
71-
| main.rs:478:5:478:21 | CallExpr | Call should have one enclosing callable but has 0. |
72-
| main.rs:479:5:479:15 | CallExpr | Call should have one enclosing callable but has 0. |
73-
| main.rs:480:5:480:15 | CallExpr | Call should have one enclosing callable but has 0. |
74-
| main.rs:481:5:481:24 | CallExpr | Call should have one enclosing callable but has 0. |
75-
| main.rs:483:5:483:22 | CallExpr | Call should have one enclosing callable but has 0. |
76-
| main.rs:485:5:485:23 | CallExpr | Call should have one enclosing callable but has 0. |
77-
| main.rs:487:5:487:23 | CallExpr | Call should have one enclosing callable but has 0. |
78-
| main.rs:488:5:488:23 | CallExpr | Call should have one enclosing callable but has 0. |
79-
| main.rs:489:5:489:23 | CallExpr | Call should have one enclosing callable but has 0. |
80-
| main.rs:490:5:490:22 | CallExpr | Call should have one enclosing callable but has 0. |
81-
| main.rs:491:5:491:22 | CallExpr | Call should have one enclosing callable but has 0. |
82-
| main.rs:493:5:493:12 | CallExpr | Call should have one enclosing callable but has 0. |
34+
| main.rs:103:14:103:38 | CallExpr | Call should have one enclosing callable but has 0. |
35+
| main.rs:116:14:116:32 | CallExpr | Call should have one enclosing callable but has 0. |
36+
| main.rs:119:18:119:33 | CallExpr | Call should have one enclosing callable but has 0. |
37+
| main.rs:176:18:176:29 | CallExpr | Call should have one enclosing callable but has 0. |
38+
| main.rs:180:18:180:31 | CallExpr | Call should have one enclosing callable but has 0. |
39+
| main.rs:184:18:184:37 | CallExpr | Call should have one enclosing callable but has 0. |
40+
| main.rs:189:22:189:33 | CallExpr | Call should have one enclosing callable but has 0. |
41+
| main.rs:194:18:194:27 | CallExpr | Call should have one enclosing callable but has 0. |
42+
| main.rs:198:21:198:30 | CallExpr | Call should have one enclosing callable but has 0. |
43+
| main.rs:198:21:198:30 | CallExpr | Call should have one enclosing callable but has 0. |
44+
| main.rs:202:21:202:32 | CallExpr | Call should have one enclosing callable but has 0. |
45+
| main.rs:202:21:202:32 | CallExpr | Call should have one enclosing callable but has 0. |
46+
| main.rs:206:21:206:30 | CallExpr | Call should have one enclosing callable but has 0. |
47+
| main.rs:206:21:206:30 | CallExpr | Call should have one enclosing callable but has 0. |
48+
| main.rs:210:18:210:38 | CallExpr | Call should have one enclosing callable but has 0. |
49+
| main.rs:214:9:214:24 | CallExpr | Call should have one enclosing callable but has 0. |
50+
| main.rs:214:9:214:24 | CallExpr | Call should have one enclosing callable but has 0. |
51+
| main.rs:218:9:218:24 | CallExpr | Call should have one enclosing callable but has 0. |
52+
| main.rs:223:9:223:32 | CallExpr | Call should have one enclosing callable but has 0. |
53+
| main.rs:223:20:223:24 | CallExpr | Call should have one enclosing callable but has 0. |
54+
| main.rs:223:27:223:31 | CallExpr | Call should have one enclosing callable but has 0. |
55+
| main.rs:250:22:250:29 | CallExpr | Call should have one enclosing callable but has 0. |
56+
| main.rs:253:22:253:29 | CallExpr | Call should have one enclosing callable but has 0. |
57+
| main.rs:257:20:257:27 | CallExpr | Call should have one enclosing callable but has 0. |
58+
| main.rs:264:21:264:28 | CallExpr | Call should have one enclosing callable but has 0. |
59+
| main.rs:270:13:270:20 | CallExpr | Call should have one enclosing callable but has 0. |
60+
| main.rs:277:13:277:20 | CallExpr | Call should have one enclosing callable but has 0. |
61+
| main.rs:285:13:285:28 | CallExpr | Call should have one enclosing callable but has 0. |
62+
| main.rs:292:13:292:30 | CallExpr | Call should have one enclosing callable but has 0. |
63+
| main.rs:299:31:299:37 | CallExpr | Call should have one enclosing callable but has 0. |
64+
| main.rs:347:21:347:51 | CallExpr | Call should have one enclosing callable but has 0. |
65+
| main.rs:348:5:348:39 | CallExpr | Call should have one enclosing callable but has 0. |
66+
| main.rs:350:58:350:92 | CallExpr | Call should have one enclosing callable but has 0. |
67+
| main.rs:350:61:350:91 | CallExpr | Call should have one enclosing callable but has 0. |
68+
| main.rs:353:22:353:59 | CallExpr | Call should have one enclosing callable but has 0. |
69+
| main.rs:356:22:356:29 | CallExpr | Call should have one enclosing callable but has 0. |
70+
| main.rs:406:13:406:24 | CallExpr | Call should have one enclosing callable but has 0. |
71+
| main.rs:410:13:410:22 | CallExpr | Call should have one enclosing callable but has 0. |
72+
| main.rs:470:9:474:10 | CallExpr | Call should have one enclosing callable but has 0. |
73+
| main.rs:487:5:487:21 | CallExpr | Call should have one enclosing callable but has 0. |
74+
| main.rs:487:5:487:21 | CallExpr | Call should have one enclosing callable but has 0. |
75+
| main.rs:508:5:508:14 | CallExpr | Call should have one enclosing callable but has 0. |
76+
| main.rs:509:5:509:14 | CallExpr | Call should have one enclosing callable but has 0. |
77+
| main.rs:510:5:510:13 | CallExpr | Call should have one enclosing callable but has 0. |
78+
| main.rs:511:5:511:12 | CallExpr | Call should have one enclosing callable but has 0. |
79+
| main.rs:512:5:512:13 | CallExpr | Call should have one enclosing callable but has 0. |
80+
| main.rs:513:14:513:54 | CallExpr | Call should have one enclosing callable but has 0. |
81+
| main.rs:513:36:513:54 | CallExpr | Call should have one enclosing callable but has 0. |
82+
| main.rs:514:5:514:11 | CallExpr | Call should have one enclosing callable but has 0. |
83+
| main.rs:515:5:515:21 | CallExpr | Call should have one enclosing callable but has 0. |
84+
| main.rs:516:5:516:15 | CallExpr | Call should have one enclosing callable but has 0. |
85+
| main.rs:517:5:517:15 | CallExpr | Call should have one enclosing callable but has 0. |
86+
| main.rs:518:5:518:24 | CallExpr | Call should have one enclosing callable but has 0. |
87+
| main.rs:519:5:519:12 | CallExpr | Call should have one enclosing callable but has 0. |
88+
| main.rs:520:5:520:16 | CallExpr | Call should have one enclosing callable but has 0. |
89+
| main.rs:522:5:522:14 | CallExpr | Call should have one enclosing callable but has 0. |
90+
| main.rs:523:5:523:14 | CallExpr | Call should have one enclosing callable but has 0. |
91+
| main.rs:525:5:525:22 | CallExpr | Call should have one enclosing callable but has 0. |
92+
| main.rs:527:5:527:23 | CallExpr | Call should have one enclosing callable but has 0. |
93+
| main.rs:529:5:529:23 | CallExpr | Call should have one enclosing callable but has 0. |
94+
| main.rs:530:5:530:23 | CallExpr | Call should have one enclosing callable but has 0. |
95+
| main.rs:531:5:531:23 | CallExpr | Call should have one enclosing callable but has 0. |
96+
| main.rs:532:5:532:22 | CallExpr | Call should have one enclosing callable but has 0. |
97+
| main.rs:533:5:533:22 | CallExpr | Call should have one enclosing callable but has 0. |
8398
| more.rs:45:14:45:26 | CallExpr | Call should have one enclosing callable but has 0. |
8499
| more.rs:46:14:46:25 | CallExpr | Call should have one enclosing callable but has 0. |
85100
| more.rs:47:14:47:26 | CallExpr | Call should have one enclosing callable but has 0. |

0 commit comments

Comments
 (0)