-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
Fix suppression of unused_assignment in binding of unused_variable
#151556
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
Fix suppression of unused_assignment in binding of unused_variable
#151556
Conversation
Unused assignments to an unused variable should trigger only the `unused_variables` lint and not also the `unused_assignments` lint. This was previously implemented by checking whether the span of the assignee was within the span of the binding pattern, however that failed to capture situations was imported from elsewhere (eg from the input tokenstream of a proc-macro that generates the binding pattern). By comparing the span of the assignee to those of the variable introductions instead, a reported stable-to-stable regression is resolved. This fix also impacted some other preexisting tests, which had (undesirably) been triggering both the `unused_variables` and `unused_assignments` lints on the same initializing assignment; those tests have therefore now been updated to expect only the former lint.
|
Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt |
| @@ -53,7 +53,6 @@ pub fn main() { | |||
| } | |||
| match (E::E { a: 10, e: C { c: 20 } }) { | |||
| mut x @ E::E{ a, e: C { mut c } } => { | |||
| //~^ WARN value assigned to `a` is never read | |||
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 line does still trigger the unused_variables lint in respect of a, but that lint is allowed by the test and so is not shown here. I'd be happy to enable it, but there are a bunch of other occasions in the test that would then trigger it (if enabled test-wide; of course I could just enable it for this scope).
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 resulting diagnostics are more precise without weakening either lint.
| // from the list of assignments the ones that happen at the definition site. | ||
| statements.retain(|source_info, _| { | ||
| source_info.span.find_ancestor_inside(binding.pat_span).is_none() | ||
| !binding.introductions.iter().any(|intro| intro.span == source_info.span) |
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 additional filter against binding.introductions makes sense, especially for proc-macro generated bindings where spans don’t line up lexically. It avoids double warnings without weakening the lint.
| quote! { | ||
| impl Drop for $name { | ||
| fn drop(&mut self) { | ||
| let Self { $field } = self; |
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.
Without the fix, the compiler would incorrectly emit both unused_variables and unused_assignments. This test clearly asserts the intended behavior.
|
r=me if perf is clean @bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
…r=<try> Fix suppression of `unused_assignment` in binding of `unused_variable`
This comment has been minimized.
This comment has been minimized.
|
Finished benchmarking commit (38bad8b): comparison URL. Overall result: no relevant changes - no action neededBenchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf. @bors rollup=never Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)Results (primary 2.4%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (primary -1.7%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 472.296s -> 472.772s (0.10%) |
|
@bors r+ rollup |
This comment has been minimized.
This comment has been minimized.
What is this?This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.Comparing 4d38622 (parent) -> 9415853 (this PR) Test differencesShow 5 test diffsStage 1
Stage 2
Additionally, 3 doctest diffs were found. These are ignored, as they are noisy. Job group index
Test dashboardRun cargo run --manifest-path src/ci/citool/Cargo.toml -- \
test-dashboard 9415853279dda1394c1f7d4114e1fe1e3ced76ec --output-dir test-dashboardAnd then open Job duration changes
How to interpret the job duration changes?Job durations can vary a lot, based on the actual runner instance |
|
Finished benchmarking commit (9415853): comparison URL. Overall result: ❌✅ regressions and improvements - no action needed@rustbot label: -perf-regression Instruction countOur most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.
Max RSS (memory usage)Results (secondary -1.5%)A less reliable metric. May be of interest, but not used to determine the overall result above.
CyclesResults (secondary 0.4%)A less reliable metric. May be of interest, but not used to determine the overall result above.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 473.581s -> 470.507s (-0.65%) |
Unused assignments to an unused variable should trigger only the
unused_variableslint and not also theunused_assignmentslint. This was previously implemented by checking whether the span of the assignee was within the span of the binding pattern, however that failed to capture situations was imported from elsewhere (eg from the input tokenstream of a proc-macro that generates the binding pattern).By comparing the span of the assignee to those of the variable introductions instead, a reported stable-to-stable regression is resolved.
This fix also impacted some other preexisting tests, which had (undesirably) been triggering both the
unused_variablesandunused_assignmentslints on the same initializing assignment; those tests have therefore now been updated to expect only the former lint.Fixes #151514
r? cjgillot (as author of reworked liveness testing in #142390)