diff options
| author | yukang <moorekang@gmail.com> | 2022-12-01 02:21:48 +0800 |
|---|---|---|
| committer | yukang <moorekang@gmail.com> | 2022-12-01 02:22:18 +0800 |
| commit | 0e24cee063e7e37d31929faaefaf5ded1732e6e1 (patch) | |
| tree | 25228465b302be09d265663a7a4fa479dc32bd7d | |
| parent | 41e0363055ade59584cff667c79f64937e6ef3f9 (diff) | |
| download | rust-0e24cee063e7e37d31929faaefaf5ded1732e6e1.tar.gz rust-0e24cee063e7e37d31929faaefaf5ded1732e6e1.zip | |
fix #105028, Only suggest removing struct field from destructive binding in shorthand scenario
| -rw-r--r-- | compiler/rustc_passes/src/liveness.rs | 8 | ||||
| -rw-r--r-- | src/test/ui/suggestions/try-removing-the-field.rs | 15 | ||||
| -rw-r--r-- | src/test/ui/suggestions/try-removing-the-field.stderr | 16 |
3 files changed, 37 insertions, 2 deletions
diff --git a/compiler/rustc_passes/src/liveness.rs b/compiler/rustc_passes/src/liveness.rs index 2234837050b..1f65cc8b609 100644 --- a/compiler/rustc_passes/src/liveness.rs +++ b/compiler/rustc_passes/src/liveness.rs @@ -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) { diff --git a/src/test/ui/suggestions/try-removing-the-field.rs b/src/test/ui/suggestions/try-removing-the-field.rs index 9d0573ca255..1b7289b229b 100644 --- a/src/test/ui/suggestions/try-removing-the-field.rs +++ b/src/test/ui/suggestions/try-removing-the-field.rs @@ -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() {} diff --git a/src/test/ui/suggestions/try-removing-the-field.stderr b/src/test/ui/suggestions/try-removing-the-field.stderr index 448a2c3d2ec..7a6013d4a6e 100644 --- a/src/test/ui/suggestions/try-removing-the-field.stderr +++ b/src/test/ui/suggestions/try-removing-the-field.stderr @@ -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 |
