]> git.lizzy.rs Git - rust.git/commitdiff
fix #105028, Only suggest removing struct field from destructive binding in shorthand...
authoryukang <moorekang@gmail.com>
Wed, 30 Nov 2022 18:21:48 +0000 (02:21 +0800)
committeryukang <moorekang@gmail.com>
Wed, 30 Nov 2022 18:22:18 +0000 (02:22 +0800)
compiler/rustc_passes/src/liveness.rs
src/test/ui/suggestions/try-removing-the-field.rs
src/test/ui/suggestions/try-removing-the-field.stderr

index 2234837050bd9683750eb05d301c1c29d57aea36..1f65cc8b6096775368cd47562ecf3e55cf48ea4b 100644 (file)
@@ -1548,7 +1548,13 @@ fn check_unused_vars_in_pat(
                 .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) {
index 9d0573ca2554d536b610bcb5b57e84fc43684e45..1b7289b229b5d488fba8bb5385ca18fd4042dd0f 100644 (file)
@@ -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() {}
index 448a2c3d2ec27a8be9663a575f51ded43a2cf9b4..7a6013d4a6eab978f2900f75b3f50a1f9e2c5ed6 100644 (file)
@@ -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