diff --git a/rust/ql/lib/codeql/rust/elements/internal/VariableImpl.qll b/rust/ql/lib/codeql/rust/elements/internal/VariableImpl.qll index f862ec2cef1c..10f079a4c5d6 100644 --- a/rust/ql/lib/codeql/rust/elements/internal/VariableImpl.qll +++ b/rust/ql/lib/codeql/rust/elements/internal/VariableImpl.qll @@ -87,7 +87,9 @@ module Impl { not name.charAt(0).isUppercase() and // exclude parameters from functions without a body as these are trait method declarations // without implementations - not exists(Function f | not f.hasBody() and f.getParamList().getAParam().getPat() = p) + not exists(Function f | not f.hasBody() and f.getParamList().getAParam().getPat() = p) and + // exclude parameters from function pointer types (e.g. `x` in `fn(x: i32) -> i32`) + not exists(FnPtrType fp | fp.getParamList().getParam(_).getPat() = p) } /** A variable. */ diff --git a/rust/ql/src/queries/unusedentities/UnusedVariable.ql b/rust/ql/src/queries/unusedentities/UnusedVariable.ql index 339bb0967fbf..388c98d07b6a 100644 --- a/rust/ql/src/queries/unusedentities/UnusedVariable.ql +++ b/rust/ql/src/queries/unusedentities/UnusedVariable.ql @@ -12,5 +12,8 @@ import rust import UnusedVariable from Variable v -where isUnused(v) +where + isUnused(v) and + not isAllowableUnused(v) and + not v instanceof DiscardVariable select v, "Variable '" + v + "' is not used." diff --git a/rust/ql/src/queries/unusedentities/UnusedVariable.qll b/rust/ql/src/queries/unusedentities/UnusedVariable.qll index 020b559c2765..d92f8787af12 100644 --- a/rust/ql/src/queries/unusedentities/UnusedVariable.qll +++ b/rust/ql/src/queries/unusedentities/UnusedVariable.qll @@ -1,14 +1,26 @@ import rust -/** A deliberately unused variable. */ +/** + * A deliberately unused variable, for example `_` or `_x`. + */ class DiscardVariable extends Variable { DiscardVariable() { this.getName().charAt(0) = "_" } } -/** Holds if variable `v` is unused. */ +/** + * Holds if variable `v` is unused. + */ predicate isUnused(Variable v) { + // variable is not accessed or initialized not exists(v.getAnAccess()) and - not exists(v.getInitializer()) and - not v instanceof DiscardVariable and - not v.getPat().isInMacroExpansion() + not exists(v.getInitializer()) +} + +/** + * Holds if variable `v` is in a context where we may not find a use for it, + * but that's expected and should not be considered a problem. + */ +predicate isAllowableUnused(Variable v) { + // in a macro expansion + v.getPat().isInMacroExpansion() } diff --git a/rust/ql/test/query-tests/unusedentities/CONSISTENCY/DataFlowConsistency.expected b/rust/ql/test/query-tests/unusedentities/CONSISTENCY/DataFlowConsistency.expected index e3c476b9f73a..6345a132ee60 100644 --- a/rust/ql/test/query-tests/unusedentities/CONSISTENCY/DataFlowConsistency.expected +++ b/rust/ql/test/query-tests/unusedentities/CONSISTENCY/DataFlowConsistency.expected @@ -1,85 +1,100 @@ uniqueEnclosingCallable -| main.rs:382:19:382:21 | Param | Node should have one enclosing callable but has 0. | -| main.rs:430:29:430:38 | Param | Node should have one enclosing callable but has 0. | -| main.rs:430:41:430:56 | Param | Node should have one enclosing callable but has 0. | +| main.rs:392:19:392:21 | Param | Node should have one enclosing callable but has 0. | +| main.rs:440:29:440:38 | Param | Node should have one enclosing callable but has 0. | +| main.rs:440:41:440:56 | Param | Node should have one enclosing callable but has 0. | +| main.rs:493:16:493:18 | Param | Node should have one enclosing callable but has 0. | +| main.rs:494:16:494:21 | Param | Node should have one enclosing callable but has 0. | +| main.rs:495:16:496:25 | Param | Node should have one enclosing callable but has 0. | +| main.rs:496:12:496:17 | Param | Node should have one enclosing callable but has 0. | +| main.rs:499:18:499:23 | Param | Node should have one enclosing callable but has 0. | +| main.rs:502:24:502:29 | Param | Node should have one enclosing callable but has 0. | | more.rs:4:23:4:28 | Param | Node should have one enclosing callable but has 0. | | more.rs:8:19:8:24 | Param | Node should have one enclosing callable but has 0. | uniqueCallEnclosingCallable -| main.rs:11:13:11:29 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:12:13:12:29 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:16:14:16:24 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:18:8:18:13 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:19:18:19:28 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:22:14:22:24 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:23:5:23:19 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:23:5:23:19 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:41:14:41:24 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:45:8:45:13 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:50:14:50:24 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:53:8:53:13 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:58:14:58:24 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:62:14:62:24 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:67:12:67:17 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:68:12:68:17 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:13:13:13:29 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:14:13:14:29 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:18:14:18:24 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:20:8:20:13 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:21:18:21:28 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:24:14:24:24 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:25:5:25:19 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:25:5:25:19 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:43:14:43:24 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:47:8:47:13 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:52:14:52:24 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:55:8:55:13 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:60:14:60:24 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:64:14:64:24 | CallExpr | Call should have one enclosing callable but has 0. | | main.rs:69:12:69:17 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:70:14:70:24 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:96:14:96:45 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:99:14:99:38 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:70:12:70:17 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:71:12:71:17 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:72:14:72:24 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:98:14:98:45 | CallExpr | Call should have one enclosing callable but has 0. | | main.rs:101:14:101:38 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:114:14:114:32 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:117:18:117:33 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:174:18:174:29 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:178:18:178:31 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:182:18:182:37 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:187:22:187:33 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:192:18:192:27 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:196:21:196:30 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:196:21:196:30 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:200:18:200:38 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:204:9:204:24 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:204:9:204:24 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:208:9:208:24 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:213:9:213:32 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:213:20:213:24 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:213:27:213:31 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:240:22:240:29 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:243:22:243:29 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:247:20:247:27 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:254:21:254:28 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:260:13:260:20 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:267:13:267:20 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:275:13:275:28 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:282:13:282:30 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:289:31:289:37 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:337:21:337:51 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:338:5:338:39 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:340:58:340:92 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:340:61:340:91 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:343:22:343:59 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:346:22:346:29 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:396:13:396:24 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:400:13:400:22 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:460:9:464:10 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:471:5:471:14 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:472:5:472:14 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:473:5:473:13 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:474:5:474:12 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:475:5:475:13 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:476:14:476:54 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:476:36:476:54 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:477:5:477:11 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:478:5:478:21 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:479:5:479:15 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:480:5:480:15 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:481:5:481:24 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:483:5:483:22 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:485:5:485:23 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:487:5:487:23 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:488:5:488:23 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:489:5:489:23 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:490:5:490:22 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:491:5:491:22 | CallExpr | Call should have one enclosing callable but has 0. | -| main.rs:493:5:493:12 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:103:14:103:38 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:116:14:116:32 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:119:18:119:33 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:176:18:176:29 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:180:18:180:31 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:184:18:184:37 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:189:22:189:33 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:194:18:194:27 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:198:21:198:30 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:198:21:198:30 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:202:21:202:32 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:202:21:202:32 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:206:21:206:30 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:206:21:206:30 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:210:18:210:38 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:214:9:214:24 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:214:9:214:24 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:218:9:218:24 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:223:9:223:32 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:223:20:223:24 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:223:27:223:31 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:250:22:250:29 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:253:22:253:29 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:257:20:257:27 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:264:21:264:28 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:270:13:270:20 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:277:13:277:20 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:285:13:285:28 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:292:13:292:30 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:299:31:299:37 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:347:21:347:51 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:348:5:348:39 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:350:58:350:92 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:350:61:350:91 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:353:22:353:59 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:356:22:356:29 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:406:13:406:24 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:410:13:410:22 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:470:9:474:10 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:487:5:487:21 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:487:5:487:21 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:508:5:508:14 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:509:5:509:14 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:510:5:510:13 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:511:5:511:12 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:512:5:512:13 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:513:14:513:54 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:513:36:513:54 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:514:5:514:11 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:515:5:515:21 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:516:5:516:15 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:517:5:517:15 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:518:5:518:24 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:519:5:519:12 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:520:5:520:16 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:522:5:522:14 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:523:5:523:14 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:525:5:525:22 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:527:5:527:23 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:529:5:529:23 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:530:5:530:23 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:531:5:531:23 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:532:5:532:22 | CallExpr | Call should have one enclosing callable but has 0. | +| main.rs:533:5:533:22 | CallExpr | Call should have one enclosing callable but has 0. | | more.rs:45:14:45:26 | CallExpr | Call should have one enclosing callable but has 0. | | more.rs:46:14:46:25 | CallExpr | Call should have one enclosing callable but has 0. | | more.rs:47:14:47:26 | CallExpr | Call should have one enclosing callable but has 0. | diff --git a/rust/ql/test/query-tests/unusedentities/UnusedValue.expected b/rust/ql/test/query-tests/unusedentities/UnusedValue.expected index 420ffcee1448..f8538e5b8bc8 100644 --- a/rust/ql/test/query-tests/unusedentities/UnusedValue.expected +++ b/rust/ql/test/query-tests/unusedentities/UnusedValue.expected @@ -1,19 +1,20 @@ -| main.rs:8:9:8:9 | a | Variable $@ is assigned a value that is never used. | main.rs:8:9:8:9 | a | a | -| main.rs:11:9:11:9 | d | Variable $@ is assigned a value that is never used. | main.rs:11:9:11:9 | d | d | -| main.rs:37:5:37:5 | b | Variable $@ is assigned a value that is never used. | main.rs:28:9:28:9 | b | b | -| main.rs:39:5:39:5 | c | Variable $@ is assigned a value that is never used. | main.rs:29:13:29:13 | c | c | -| main.rs:42:5:42:5 | c | Variable $@ is assigned a value that is never used. | main.rs:29:13:29:13 | c | c | -| main.rs:46:9:46:9 | d | Variable $@ is assigned a value that is never used. | main.rs:30:13:30:13 | d | d | -| main.rs:52:5:52:5 | e | Variable $@ is assigned a value that is never used. | main.rs:31:13:31:13 | e | e | -| main.rs:63:5:63:5 | f | Variable $@ is assigned a value that is never used. | main.rs:32:13:32:13 | f | f | -| main.rs:65:5:65:5 | f | Variable $@ is assigned a value that is never used. | main.rs:32:13:32:13 | f | f | -| main.rs:67:5:67:5 | g | Variable $@ is assigned a value that is never used. | main.rs:33:9:33:9 | g | g | -| main.rs:89:9:89:9 | a | Variable $@ is assigned a value that is never used. | main.rs:89:9:89:9 | a | a | -| main.rs:110:9:110:10 | is | Variable $@ is assigned a value that is never used. | main.rs:110:9:110:10 | is | is | -| main.rs:133:13:133:17 | total | Variable $@ is assigned a value that is never used. | main.rs:133:13:133:17 | total | total | -| main.rs:270:13:270:17 | total | Variable $@ is assigned a value that is never used. | main.rs:238:13:238:17 | total | total | -| main.rs:363:9:363:9 | x | Variable $@ is assigned a value that is never used. | main.rs:363:9:363:9 | x | x | -| main.rs:371:17:371:17 | x | Variable $@ is assigned a value that is never used. | main.rs:371:17:371:17 | x | x | +| main.rs:10:9:10:9 | a | Variable $@ is assigned a value that is never used. | main.rs:10:9:10:9 | a | a | +| main.rs:13:9:13:9 | d | Variable $@ is assigned a value that is never used. | main.rs:13:9:13:9 | d | d | +| main.rs:39:5:39:5 | b | Variable $@ is assigned a value that is never used. | main.rs:30:9:30:9 | b | b | +| main.rs:41:5:41:5 | c | Variable $@ is assigned a value that is never used. | main.rs:31:13:31:13 | c | c | +| main.rs:44:5:44:5 | c | Variable $@ is assigned a value that is never used. | main.rs:31:13:31:13 | c | c | +| main.rs:48:9:48:9 | d | Variable $@ is assigned a value that is never used. | main.rs:32:13:32:13 | d | d | +| main.rs:54:5:54:5 | e | Variable $@ is assigned a value that is never used. | main.rs:33:13:33:13 | e | e | +| main.rs:65:5:65:5 | f | Variable $@ is assigned a value that is never used. | main.rs:34:13:34:13 | f | f | +| main.rs:67:5:67:5 | f | Variable $@ is assigned a value that is never used. | main.rs:34:13:34:13 | f | f | +| main.rs:69:5:69:5 | g | Variable $@ is assigned a value that is never used. | main.rs:35:9:35:9 | g | g | +| main.rs:91:9:91:9 | a | Variable $@ is assigned a value that is never used. | main.rs:91:9:91:9 | a | a | +| main.rs:112:9:112:10 | is | Variable $@ is assigned a value that is never used. | main.rs:112:9:112:10 | is | is | +| main.rs:135:13:135:17 | total | Variable $@ is assigned a value that is never used. | main.rs:135:13:135:17 | total | total | +| main.rs:280:13:280:17 | total | Variable $@ is assigned a value that is never used. | main.rs:248:13:248:17 | total | total | +| main.rs:373:9:373:9 | x | Variable $@ is assigned a value that is never used. | main.rs:373:9:373:9 | x | x | +| main.rs:381:17:381:17 | x | Variable $@ is assigned a value that is never used. | main.rs:381:17:381:17 | x | x | +| main.rs:482:9:482:9 | c | Variable $@ is assigned a value that is never used. | main.rs:482:9:482:9 | c | c | | more.rs:44:9:44:14 | a_ptr4 | Variable $@ is assigned a value that is never used. | more.rs:44:9:44:14 | a_ptr4 | a_ptr4 | | more.rs:59:9:59:13 | d_ptr | Variable $@ is assigned a value that is never used. | more.rs:59:9:59:13 | d_ptr | d_ptr | | more.rs:65:9:65:17 | f_ptr | Variable $@ is assigned a value that is never used. | more.rs:65:13:65:17 | f_ptr | f_ptr | diff --git a/rust/ql/test/query-tests/unusedentities/UnusedVariable.expected b/rust/ql/test/query-tests/unusedentities/UnusedVariable.expected index 7f459575f02e..dcfde3c46f54 100644 --- a/rust/ql/test/query-tests/unusedentities/UnusedVariable.expected +++ b/rust/ql/test/query-tests/unusedentities/UnusedVariable.expected @@ -1,22 +1,22 @@ -| main.rs:27:9:27:9 | a | Variable 'a' is not used. | -| main.rs:92:13:92:13 | d | Variable 'd' is not used. | -| main.rs:141:5:141:5 | y | Variable 'y' is not used. | -| main.rs:168:9:168:9 | x | Variable 'x' is not used. | -| main.rs:240:17:240:17 | a | Variable 'a' is not used. | -| main.rs:248:20:248:22 | val | Variable 'val' is not used. | -| main.rs:262:14:262:16 | val | Variable 'val' is not used. | -| main.rs:277:22:277:24 | val | Variable 'val' is not used. | -| main.rs:284:24:284:26 | val | Variable 'val' is not used. | -| main.rs:292:13:292:15 | num | Variable 'num' is not used. | -| main.rs:307:12:307:12 | j | Variable 'j' is not used. | -| main.rs:327:25:327:25 | y | Variable 'y' is not used. | -| main.rs:330:28:330:28 | a | Variable 'a' is not used. | -| main.rs:333:9:333:9 | p | Variable 'p' is not used. | -| main.rs:351:9:351:13 | right | Variable 'right' is not used. | -| main.rs:357:9:357:14 | right2 | Variable 'right2' is not used. | -| main.rs:364:13:364:13 | y | Variable 'y' is not used. | -| main.rs:372:21:372:21 | y | Variable 'y' is not used. | -| main.rs:417:26:417:28 | val | Variable 'val' is not used. | -| main.rs:420:21:420:23 | acc | Variable 'acc' is not used. | -| main.rs:441:9:441:14 | unused | Variable 'unused' is not used. | +| main.rs:29:9:29:9 | a | Variable 'a' is not used. | +| main.rs:94:13:94:13 | d | Variable 'd' is not used. | +| main.rs:143:5:143:5 | y | Variable 'y' is not used. | +| main.rs:170:9:170:9 | x | Variable 'x' is not used. | +| main.rs:250:17:250:17 | a | Variable 'a' is not used. | +| main.rs:258:20:258:22 | val | Variable 'val' is not used. | +| main.rs:272:14:272:16 | val | Variable 'val' is not used. | +| main.rs:287:22:287:24 | val | Variable 'val' is not used. | +| main.rs:294:24:294:26 | val | Variable 'val' is not used. | +| main.rs:302:13:302:15 | num | Variable 'num' is not used. | +| main.rs:317:12:317:12 | j | Variable 'j' is not used. | +| main.rs:337:25:337:25 | y | Variable 'y' is not used. | +| main.rs:340:28:340:28 | a | Variable 'a' is not used. | +| main.rs:343:9:343:9 | p | Variable 'p' is not used. | +| main.rs:361:9:361:13 | right | Variable 'right' is not used. | +| main.rs:367:9:367:14 | right2 | Variable 'right2' is not used. | +| main.rs:374:13:374:13 | y | Variable 'y' is not used. | +| main.rs:382:21:382:21 | y | Variable 'y' is not used. | +| main.rs:427:26:427:28 | val | Variable 'val' is not used. | +| main.rs:430:21:430:23 | acc | Variable 'acc' is not used. | +| main.rs:451:9:451:14 | unused | Variable 'unused' is not used. | | more.rs:24:9:24:11 | val | Variable 'val' is not used. | diff --git a/rust/ql/test/query-tests/unusedentities/main.rs b/rust/ql/test/query-tests/unusedentities/main.rs index ee315f63d002..814a2ffee5b2 100644 --- a/rust/ql/test/query-tests/unusedentities/main.rs +++ b/rust/ql/test/query-tests/unusedentities/main.rs @@ -1,5 +1,7 @@ +mod more; mod unreachable; +use more::*; use unreachable::*; // --- locals --- @@ -196,6 +198,14 @@ fn loops() { _ = format!("x is {x}"); } + for x in 1..10 { + _ = format!("x is {x:?}"); + } + + [1, 2, 3].iter().for_each(|x| { + _ = format!("x is {x}"); + }); + for x in 1..10 { println!("x is {val}", val = x); } @@ -464,6 +474,33 @@ fn macros() { }) ) } +// --- references --- + +fn references() { + let a = 1; + let b = &a; + let c = *b; // $ Alert[rust/unused-value] + let d = 2; + let e = 3; + let f = &&e; + + assert!(&d != *f); +} + +// --- declarations in types --- + +pub struct my_declaration { + field1: fn(i32) -> i32, + field2: fn(x: i32) -> i32, + field3: fn(y: + fn(z: i32) -> i32) -> i32, +} + +type MyType = fn(x: i32) -> i32; + +trait MyTrait { + fn my_func2(&self, x: i32) -> i32; +} // --- main --- @@ -479,6 +516,11 @@ fn main() { shadowing(); func_ptrs(); folds_and_closures(); + macros(); + references(); + + generics(); + pointers(); unreachable_if_1(); // unreachable_panic(); @@ -489,6 +531,4 @@ fn main() { unreachable_let_2(); unreachable_if_2(); unreachable_if_3(); - - macros(); } diff --git a/rust/ql/test/query-tests/unusedentities/more.rs b/rust/ql/test/query-tests/unusedentities/more.rs index 4788575b9de1..27acf729ef65 100644 --- a/rust/ql/test/query-tests/unusedentities/more.rs +++ b/rust/ql/test/query-tests/unusedentities/more.rs @@ -27,7 +27,7 @@ impl MyGettable for MyContainer { } } -fn generics() { +pub fn generics() { let mut a = MyContainer { val: 1 }; // $ MISSING: Alert[rust/unused-value] let b = MyContainer { val: 2 }; @@ -36,7 +36,7 @@ fn generics() { // --- pointers --- -fn pointers() { +pub fn pointers() { let a = 1; let a_ptr1 = &a; let a_ptr2 = &a;