Skip to content

Commit 38bad8b

Browse files
committed
Auto merge of #151556 - eggyal:unused-assignment-to-unused-variable, r=<try>
Fix suppression of `unused_assignment` in binding of `unused_variable`
2 parents 021fc25 + 22b3f59 commit 38bad8b

8 files changed

Lines changed: 72 additions & 18 deletions

File tree

compiler/rustc_mir_transform/src/liveness.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,7 @@ impl<'a, 'tcx> AssignmentResult<'a, 'tcx> {
986986
// warn twice, for the unused local and for the unused assignment. Therefore, we remove
987987
// from the list of assignments the ones that happen at the definition site.
988988
statements.retain(|source_info, _| {
989-
source_info.span.find_ancestor_inside(binding.pat_span).is_none()
989+
!binding.introductions.iter().any(|intro| intro.span == source_info.span)
990990
});
991991

992992
// Extra assignments that we recognize thanks to the initialization span. We need to

tests/ui/lint/issue-49588-non-shorthand-field-patterns-in-pattern-macro.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@ run-pass
2-
#![allow(unused_variables)]
2+
#![warn(unused)]
33
#![deny(non_shorthand_field_patterns)]
44

55
pub struct Value<A> { pub value: A }
@@ -13,5 +13,5 @@ macro_rules! pat {
1313

1414
fn main() {
1515
let pat!(value) = Value { value: () };
16-
//~^ WARN value assigned to `value` is never read
16+
//~^ WARN unused variable: `value`
1717
}
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
warning: value assigned to `value` is never read
1+
warning: unused variable: `value`
22
--> $DIR/issue-49588-non-shorthand-field-patterns-in-pattern-macro.rs:15:14
33
|
44
LL | let pat!(value) = Value { value: () };
5-
| ^^^^^
5+
| ^^^^^ help: if this is intentional, prefix it with an underscore: `_value`
66
|
7-
= help: maybe it is overwritten before being read?
8-
= note: `#[warn(unused_assignments)]` (part of `#[warn(unused)]`) on by default
7+
note: the lint level is defined here
8+
--> $DIR/issue-49588-non-shorthand-field-patterns-in-pattern-macro.rs:2:9
9+
|
10+
LL | #![warn(unused)]
11+
| ^^^^^^
12+
= note: `#[warn(unused_variables)]` implied by `#[warn(unused)]`
913

1014
warning: 1 warning emitted
1115

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![feature(proc_macro_quote)]
2+
3+
extern crate proc_macro;
4+
use proc_macro::*;
5+
6+
#[proc_macro_derive(Drop)]
7+
pub fn generate(ts: TokenStream) -> TokenStream {
8+
let mut ts = ts.into_iter();
9+
let _pub = ts.next();
10+
let _struct = ts.next();
11+
let name = ts.next().unwrap();
12+
let TokenTree::Group(fields) = ts.next().unwrap() else { panic!() };
13+
let mut fields = fields.stream().into_iter();
14+
let field = fields.next().unwrap();
15+
16+
quote! {
17+
impl Drop for $name {
18+
fn drop(&mut self) {
19+
let Self { $field } = self;
20+
}
21+
}
22+
}
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Unused assignments to an unused variable should trigger only the `unused_variables` lint and not
2+
// also the `unused_assignments` lint. This test covers the situation where the span of the unused
3+
// variable identifier comes from a different scope to the binding pattern - here, from a proc
4+
// macro's input tokenstream (whereas the binding pattern is generated within the proc macro
5+
// itself).
6+
//
7+
// Regression test for https://github.com/rust-lang/rust/issues/151514
8+
//
9+
//@ check-pass
10+
//@ proc-macro: unused_assignment_proc_macro.rs
11+
#![warn(unused)]
12+
13+
extern crate unused_assignment_proc_macro;
14+
use unused_assignment_proc_macro::Drop;
15+
16+
#[derive(Drop)]
17+
pub struct S {
18+
a: (), //~ WARN unused variable: `a`
19+
}
20+
21+
fn main() {}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
warning: unused variable: `a`
2+
--> $DIR/unused_assignment.rs:18:5
3+
|
4+
LL | a: (),
5+
| ^ help: try ignoring the field: `a: _`
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/unused_assignment.rs:11:9
9+
|
10+
LL | #![warn(unused)]
11+
| ^^^^^^
12+
= note: `#[warn(unused_variables)]` implied by `#[warn(unused)]`
13+
14+
warning: 1 warning emitted
15+

tests/ui/pattern/bindings-after-at/bind-by-copy.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ pub fn main() {
5353
}
5454
match (E::E { a: 10, e: C { c: 20 } }) {
5555
mut x @ E::E{ a, e: C { mut c } } => {
56-
//~^ WARN value assigned to `a` is never read
5756
x = E::NotE;
5857
//~^ WARN value assigned to `x` is never read
5958
c += 30;

tests/ui/pattern/bindings-after-at/bind-by-copy.stderr

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,12 @@ LL | y.d.c = 30;
2020
= help: maybe it is overwritten before being read?
2121

2222
warning: value assigned to `x` is never read
23-
--> $DIR/bind-by-copy.rs:57:13
23+
--> $DIR/bind-by-copy.rs:56:13
2424
|
2525
LL | x = E::NotE;
2626
| ^^^^^^^^^^^
2727
|
2828
= help: maybe it is overwritten before being read?
2929

30-
warning: value assigned to `a` is never read
31-
--> $DIR/bind-by-copy.rs:55:23
32-
|
33-
LL | mut x @ E::E{ a, e: C { mut c } } => {
34-
| ^
35-
|
36-
= help: maybe it is overwritten before being read?
37-
38-
warning: 4 warnings emitted
30+
warning: 3 warnings emitted
3931

0 commit comments

Comments
 (0)