Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust: Diagnose unused variable false positives #17656

Merged
merged 5 commits into from
Oct 3, 2024
Merged

Conversation

geoffw0
Copy link
Contributor

@geoffw0 geoffw0 commented Oct 3, 2024

We've seen a lot of false positive results in the wild for the new rust/unused-variable query. I've identified common three patterns, which are tested here:

  • the variable is used, but in a macro invocation (we already knew about this issue).
  • the variable is introduced on the left side of a pattern, but the accesses that follow are not correctly identified (they are VariableAccessCand, but not VariableAccess).
  • the variable is a pattern match on None (I didn't manage to reproduce this issue with any other enum values).

@hvitved do you think any of these have quick fixes I can implement here? Or should I write up issues for them?

@geoffw0 geoffw0 added the Rust Pull requests that update Rust code label Oct 3, 2024
@aibaars
Copy link
Contributor

aibaars commented Oct 3, 2024

The pattern match on None problem is likely the same problem we ran into on CI where None nodes were sometimes considered PathPat and sometimes IdentPat . I suspect that the problem also applies to other enum values without arguments that are referred to without qualifier. I think this is pretty rare except for the Option type though. Most code uses the EnumType::Constant notation instead of just Constant.

@aibaars
Copy link
Contributor

aibaars commented Oct 3, 2024

My work on macro expansion should fix the problem with variables "used" in macro invocations.

@aibaars
Copy link
Contributor

aibaars commented Oct 3, 2024

@geoffw0 Could you include some code snippets as examples (aka test cases) in this issue?

@hvitved
Copy link
Contributor

hvitved commented Oct 3, 2024

Fix for the while let case here: geoffw0#5

@geoffw0
Copy link
Contributor Author

geoffw0 commented Oct 3, 2024

My work on macro expansion should fix the problem with variables "used" in macro invocations.

That's great. Though the volume of FPs is breaking the DCA job, so I will probably have to implement a workaround until then.

Could you include some code snippets as examples (aka test cases) in this issue?

Already here. :)

@geoffw0
Copy link
Contributor Author

geoffw0 commented Oct 3, 2024

I've just added a commit that restricts this query to working in files called main.rs. Obviously this is a temporary and severe restriction, but it should get the DCA job working (it will still show at least a few hundred results!) and does not affect the test. I'll create an issue shortly to track repairing this query.

Rust: Account for variables bound in `while let` expressions
@geoffw0 geoffw0 merged commit 64720ad into github:main Oct 3, 2024
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants