Skip to content

Commit 90d84ce

Browse files
authored
Rollup merge of #105174 - chenyukang:yukang/fix-105028-unused, r=eholk
Suggest removing struct field from destructive binding only in shorthand scenario Fixes #105028
2 parents b29a4f9 + 0e24cee commit 90d84ce

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

compiler/rustc_passes/src/liveness.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1548,7 +1548,13 @@ impl<'tcx> Liveness<'_, 'tcx> {
15481548
.or_insert_with(|| (ln, var, vec![id_and_sp]));
15491549
});
15501550

1551-
let can_remove = matches!(&pat.kind, hir::PatKind::Struct(_, _, true));
1551+
let can_remove = match pat.kind {
1552+
hir::PatKind::Struct(_, fields, true) => {
1553+
// if all fields are shorthand, remove the struct field, otherwise, mark with _ as prefix
1554+
fields.iter().all(|f| f.is_shorthand)
1555+
}
1556+
_ => false,
1557+
};
15521558

15531559
for (_, (ln, var, hir_ids_and_spans)) in vars {
15541560
if self.used_on_entry(ln, var) {

src/test/ui/suggestions/try-removing-the-field.rs

+15
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,19 @@ fn use_foo(x: Foo) -> i32 {
1414
return foo;
1515
}
1616

17+
// issue #105028, suggest removing the field only for shorthand
18+
fn use_match(x: Foo) {
19+
match x {
20+
Foo { foo: unused, .. } => { //~ WARNING unused variable
21+
//~| help: if this is intentional, prefix it with an underscore
22+
}
23+
}
24+
25+
match x {
26+
Foo { foo, .. } => { //~ WARNING unused variable
27+
//~| help: try removing the field
28+
}
29+
}
30+
}
31+
1732
fn main() {}

src/test/ui/suggestions/try-removing-the-field.stderr

+15-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,19 @@ LL | let Foo { foo, bar, .. } = x;
88
|
99
= note: `#[warn(unused_variables)]` on by default
1010

11-
warning: 1 warning emitted
11+
warning: unused variable: `unused`
12+
--> $DIR/try-removing-the-field.rs:20:20
13+
|
14+
LL | Foo { foo: unused, .. } => {
15+
| ^^^^^^ help: if this is intentional, prefix it with an underscore: `_unused`
16+
17+
warning: unused variable: `foo`
18+
--> $DIR/try-removing-the-field.rs:26:15
19+
|
20+
LL | Foo { foo, .. } => {
21+
| ^^^-
22+
| |
23+
| help: try removing the field
24+
25+
warning: 3 warnings emitted
1226

0 commit comments

Comments
 (0)