codeql icon indicating copy to clipboard operation
codeql copied to clipboard

Rust: Fix some false positives for rust/unused-variable and rust/unused-value

Open geoffw0 opened this issue 1 month ago • 2 comments

Fix some common false positive results for the rust/unused-variable and rust/unused-value queries. Typically these results look something like this:

match foo {
  MY_CONSTANT => { ... }
  ...
}

Due to incorrect code, incorrect extraction, and/or extraction with the wrong settings MY_CONSTANT is misrecognized as a variable and the query observes that it is not used. I've been unable to produce a test case but its common in the wild and I expect DCA to produce many fixed examples of this.

geoffw0 avatar Dec 10 '25 17:12 geoffw0

Unfortunately it turns out this change does exclude some correct results (see the test). I'm not sure how I can improve that, short of looking at the case of the variable name (i.e. heuristic). Lets wait for the DCA results and see what we've got.

geoffw0 avatar Dec 10 '25 18:12 geoffw0

DCA showed 1076 results removed. I've reviewed a large number of these and nearly all resemble the typical example in the PR comment above. We do lose a tiny number of results where the target really was a (used or unused) variable - I've since narrowed the change slightly to address that (by checking capitalization as well).

geoffw0 avatar Dec 11 '25 18:12 geoffw0