-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Rust: Fix rust/unused-variable FPs #17913
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3091083
Rust: Call all the test cases.
geoffw0 35ffd0c
Rust: Add more test cases for unused variables (inspired by real worl…
geoffw0 278760c
Rust: Add another exception to rust/unused-variable.
geoffw0 93836a5
Rust: Clean up code / clarify responsibilities and fix the issue in r…
geoffw0 1a5d2e1
Rust: Correct a comment and autoformat.
geoffw0 8a35e15
Rust: Move the function pointer parameter exclusion into VariableImpl…
geoffw0 18ce8be
Update rust/ql/lib/codeql/rust/elements/internal/VariableImpl.qll
geoffw0 df7bcfd
Rust: Accept dataflow consistency check changes.
geoffw0 fc444f1
Merge branch 'main' into unusedvar8
geoffw0 7c2c5ee
Rust: Fix expected consistency results.
geoffw0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |
167 changes: 91 additions & 76 deletions
167
rust/ql/test/query-tests/unusedentities/CONSISTENCY/DataFlowConsistency.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@hvitved Should there be a "write access" for a variable with an initializer? The following two examples should probably be considered equivalent.
and
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.
Yep, I have plans to flip this behaviour so that both would be considered an unused variable (and not an unused value). There's an issue tracking this, it will probably be simple, but I haven't got around to it yet.
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.
The
x
inlet x : i 32 = 10;
is a pattern and not an expression, so making it a write access is not that natural.