Skip to content

Suggest removing struct field from destructive binding only in shorthand scenario #105174

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 1 commit into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion compiler/rustc_passes/src/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1548,7 +1548,13 @@ impl<'tcx> Liveness<'_, 'tcx> {
.or_insert_with(|| (ln, var, vec![id_and_sp]));
});

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

for (_, (ln, var, hir_ids_and_spans)) in vars {
if self.used_on_entry(ln, var) {
Expand Down
15 changes: 15 additions & 0 deletions src/test/ui/suggestions/try-removing-the-field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,19 @@ fn use_foo(x: Foo) -> i32 {
return foo;
}

// issue #105028, suggest removing the field only for shorthand
fn use_match(x: Foo) {
match x {
Foo { foo: unused, .. } => { //~ WARNING unused variable
//~| help: if this is intentional, prefix it with an underscore
}
}

match x {
Foo { foo, .. } => { //~ WARNING unused variable
//~| help: try removing the field
}
}
}

fn main() {}
16 changes: 15 additions & 1 deletion src/test/ui/suggestions/try-removing-the-field.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,19 @@ LL | let Foo { foo, bar, .. } = x;
|
= note: `#[warn(unused_variables)]` on by default

warning: 1 warning emitted
warning: unused variable: `unused`
--> $DIR/try-removing-the-field.rs:20:20
|
LL | Foo { foo: unused, .. } => {
| ^^^^^^ help: if this is intentional, prefix it with an underscore: `_unused`

warning: unused variable: `foo`
--> $DIR/try-removing-the-field.rs:26:15
|
LL | Foo { foo, .. } => {
| ^^^-
| |
| help: try removing the field

warning: 3 warnings emitted