about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/librustc_passes/liveness.rs20
-rw-r--r--src/test/ui/lint/issue-67691-unused-field-in-or-pattern.fixed4
-rw-r--r--src/test/ui/lint/issue-67691-unused-field-in-or-pattern.stderr8
3 files changed, 18 insertions, 14 deletions
diff --git a/src/librustc_passes/liveness.rs b/src/librustc_passes/liveness.rs
index 74a2d997896..24f6d1a9c58 100644
--- a/src/librustc_passes/liveness.rs
+++ b/src/librustc_passes/liveness.rs
@@ -1556,15 +1556,16 @@ impl<'tcx> Liveness<'_, 'tcx> {
                             .map(|(_, span)| (span, format!("{}: _", name)))
                             .collect::<Vec<_>>();
 
-                        let non_shorthands = non_shorthands
-                            .into_iter()
-                            .map(|(_, span)| (span, format!("_{}", name)))
-                            .collect::<Vec<_>>();
-
                         // If we have both shorthand and non-shorthand, prefer the "try ignoring
-                        // the field" message.
+                        // the field" message, and suggest `_` for the non-shorthands. If we only
+                        // have non-shorthand, then prefix with an underscore instead.
                         if !shorthands.is_empty() {
-                            shorthands.extend(non_shorthands);
+                            shorthands.extend(
+                                non_shorthands
+                                    .into_iter()
+                                    .map(|(_, span)| (span, "_".to_string()))
+                                    .collect::<Vec<_>>(),
+                            );
 
                             err.multipart_suggestion(
                                 "try ignoring the field",
@@ -1574,7 +1575,10 @@ impl<'tcx> Liveness<'_, 'tcx> {
                         } else {
                             err.multipart_suggestion(
                                 "if this is intentional, prefix it with an underscore",
-                                non_shorthands,
+                                non_shorthands
+                                    .into_iter()
+                                    .map(|(_, span)| (span, format!("_{}", name)))
+                                    .collect::<Vec<_>>(),
                                 Applicability::MachineApplicable,
                             );
                         }
diff --git a/src/test/ui/lint/issue-67691-unused-field-in-or-pattern.fixed b/src/test/ui/lint/issue-67691-unused-field-in-or-pattern.fixed
index cfe37606202..f842fcebe1f 100644
--- a/src/test/ui/lint/issue-67691-unused-field-in-or-pattern.fixed
+++ b/src/test/ui/lint/issue-67691-unused-field-in-or-pattern.fixed
@@ -59,7 +59,7 @@ pub fn inner_with_ref(x: Option<MyEnum>) {
 
 pub fn mixed_no_ref(x: MixedEnum) {
     match x {
-        MixedEnum::A { i: _ } | MixedEnum::B(_i) => {
+        MixedEnum::A { i: _ } | MixedEnum::B(_) => {
             println!("match");
         }
     }
@@ -67,7 +67,7 @@ pub fn mixed_no_ref(x: MixedEnum) {
 
 pub fn mixed_with_ref(x: MixedEnum) {
     match x {
-        MixedEnum::A { i: _ } | MixedEnum::B(_i) => {
+        MixedEnum::A { i: _ } | MixedEnum::B(_) => {
             println!("match");
         }
     }
diff --git a/src/test/ui/lint/issue-67691-unused-field-in-or-pattern.stderr b/src/test/ui/lint/issue-67691-unused-field-in-or-pattern.stderr
index 4e9d02abacd..8aefe243a94 100644
--- a/src/test/ui/lint/issue-67691-unused-field-in-or-pattern.stderr
+++ b/src/test/ui/lint/issue-67691-unused-field-in-or-pattern.stderr
@@ -56,8 +56,8 @@ LL |         MixedEnum::A { i } | MixedEnum::B(i) => {
    |
 help: try ignoring the field
    |
-LL |         MixedEnum::A { i: _ } | MixedEnum::B(_i) => {
-   |                        ^^^^                  ^^
+LL |         MixedEnum::A { i: _ } | MixedEnum::B(_) => {
+   |                        ^^^^                  ^
 
 error: unused variable: `i`
   --> $DIR/issue-67691-unused-field-in-or-pattern.rs:70:24
@@ -67,8 +67,8 @@ LL |         MixedEnum::A { ref i } | MixedEnum::B(ref i) => {
    |
 help: try ignoring the field
    |
-LL |         MixedEnum::A { i: _ } | MixedEnum::B(_i) => {
-   |                        ^^^^                  ^^
+LL |         MixedEnum::A { i: _ } | MixedEnum::B(_) => {
+   |                        ^^^^                  ^
 
 error: aborting due to 6 previous errors