-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Rust: Improve rust/unused-variable and rust/unused-value #18952
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
Conversation
…what the comments apply to.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR Overview
This PR improves the filtering of unused variable and unused value alerts in Rust by excluding results in functions that contain unexpanded macros. Key changes include:
- Addition of new test functions (macros1–macros6, hygiene_mismatch) to cover macro expansion scenarios.
- Adjustments in test annotations to identify missing and spurious alerts.
- A configuration update in options.yml to disable Cargo checking.
Reviewed Changes
File | Description |
---|---|
rust/ql/test/query-tests/unusedentities/main.rs | Added and updated test cases for macros and hygiene scenarios. |
rust/ql/test/query-tests/unusedentities/options.yml | Updated testing option to disable cargo check. |
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (1)
rust/ql/test/query-tests/unusedentities/main.rs:533
- The assignment inside the block in macros6 does not trigger the expected 'unused-value' alert. Verify if the macro expansion behavior should indeed trigger an unused value alert or update the test annotation accordingly.
x = 10; // $ MISSING: Alert[rust/unused-value]
Tip: Copilot only keeps its highest confidence comments to reduce noise and keep you focused. Learn more
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me
Some expected output still needs an update:
|
DCA shows 1509 less results for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a great way to reduce the number of FPs. I've left one comment and otherwise it looks great.
@@ -24,6 +38,9 @@ predicate isAllowableUnused(Variable v) { | |||
// in a macro expansion | |||
v.getPat().isInMacroExpansion() | |||
or | |||
// declared in an incomplete callable | |||
v.getEnclosingCfgScope() instanceof IncompleteCallable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this works, it would perhaps be more correct to only exclude results when there is an unexpandable macro in the scope that the variable is defined. However, I realize that the concept of variable scope is not currently exposed, so perhaps that is better done follow-up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is required to expose the concept of a variable scope?
I've added a test that exposes the situation I think we're talking about here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We would have to expose the VariableScope
class from VariableImpl.qll
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I've added it to the existing improvements issue for rust/unused-variable
. It sounds like a straightforward follow-up, apart from the need to go through another cycle of DCA / testing.
Is there any reason VariableScope
shouldn't be exposed publically?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any reason
VariableScope
shouldn't be exposed publically?
Only that we haven't needed it before. We may also have to change it slightly, for example right now x
and y
are considered to be in the same scope in
let x = ...;
let y = ...;
…s not needed) and accept consistency check changes.
Improve
rust/unused-variable
andrust/unused-value
, by excluding results in functions that contain unexpanded macros. In these functions, there may be and often are accesses of the supposedly unused variables / values that we simply can't see.In my local testing this removed about 80% of results, for
rust/unused-variable
the quality of remaining results seemed much better (on average), though forrust/unused-value
even the remaining results were still mostly false positives (note that the latter query has a lower@precision
).DCA will confirm.