-
-
Notifications
You must be signed in to change notification settings - Fork 14.5k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,15 @@ | ||
| warning: value assigned to `value` is never read | ||
| warning: unused variable: `value` | ||
| --> $DIR/issue-49588-non-shorthand-field-patterns-in-pattern-macro.rs:15:14 | ||
| | | ||
| LL | let pat!(value) = Value { value: () }; | ||
| | ^^^^^ | ||
| | ^^^^^ help: if this is intentional, prefix it with an underscore: `_value` | ||
| | | ||
| = help: maybe it is overwritten before being read? | ||
| = note: `#[warn(unused_assignments)]` (part of `#[warn(unused)]`) on by default | ||
| note: the lint level is defined here | ||
| --> $DIR/issue-49588-non-shorthand-field-patterns-in-pattern-macro.rs:2:9 | ||
| | | ||
| LL | #![warn(unused)] | ||
| | ^^^^^^ | ||
| = note: `#[warn(unused_variables)]` implied by `#[warn(unused)]` | ||
|
|
||
| warning: 1 warning emitted | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| #![feature(proc_macro_quote)] | ||
|
|
||
| extern crate proc_macro; | ||
| use proc_macro::*; | ||
|
|
||
| #[proc_macro_derive(Drop)] | ||
| pub fn generate(ts: TokenStream) -> TokenStream { | ||
| let mut ts = ts.into_iter(); | ||
| let _pub = ts.next(); | ||
| let _struct = ts.next(); | ||
| let name = ts.next().unwrap(); | ||
| let TokenTree::Group(fields) = ts.next().unwrap() else { panic!() }; | ||
| let mut fields = fields.stream().into_iter(); | ||
| let field = fields.next().unwrap(); | ||
|
|
||
| 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 commentThe 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. |
||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // Unused assignments to an unused variable should trigger only the `unused_variables` lint and not | ||
| // also the `unused_assignments` lint. This test covers the situation where the span of the unused | ||
| // variable identifier comes from a different scope to the binding pattern - here, from a proc | ||
| // macro's input tokenstream (whereas the binding pattern is generated within the proc macro | ||
| // itself). | ||
| // | ||
| // Regression test for https://github.com/rust-lang/rust/issues/151514 | ||
| // | ||
| //@ check-pass | ||
| //@ proc-macro: unused_assignment_proc_macro.rs | ||
| #![warn(unused)] | ||
|
|
||
| extern crate unused_assignment_proc_macro; | ||
| use unused_assignment_proc_macro::Drop; | ||
|
|
||
| #[derive(Drop)] | ||
| pub struct S { | ||
| a: (), //~ WARN unused variable: `a` | ||
| } | ||
|
|
||
| fn main() {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| warning: unused variable: `a` | ||
| --> $DIR/unused_assignment.rs:18:5 | ||
| | | ||
| LL | a: (), | ||
| | ^ help: try ignoring the field: `a: _` | ||
| | | ||
| note: the lint level is defined here | ||
| --> $DIR/unused_assignment.rs:11:9 | ||
| | | ||
| LL | #![warn(unused)] | ||
| | ^^^^^^ | ||
| = note: `#[warn(unused_variables)]` implied by `#[warn(unused)]` | ||
|
|
||
| warning: 1 warning emitted | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line does still trigger the |
||
| x = E::NotE; | ||
| //~^ WARN value assigned to `x` is never read | ||
| c += 30; | ||
|
|
||
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.