From a8e3d0b71e2b79b0ce880541e5a0f0fe303a1a8d Mon Sep 17 00:00:00 2001 From: sapir Date: Fri, 6 Mar 2020 10:03:34 +0200 Subject: [PATCH] Replace non-shorthand variables with _, not _var --- src/librustc_passes/liveness.rs | 20 +++++++++++-------- ...sue-67691-unused-field-in-or-pattern.fixed | 4 ++-- ...ue-67691-unused-field-in-or-pattern.stderr | 8 ++++---- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/librustc_passes/liveness.rs b/src/librustc_passes/liveness.rs index 74a2d9978968..24f6d1a9c589 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::>(); - let non_shorthands = non_shorthands - .into_iter() - .map(|(_, span)| (span, format!("_{}", name))) - .collect::>(); - // 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::>(), + ); 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::>(), 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 cfe376062020..f842fcebe1f8 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) { 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 4e9d02abacd7..8aefe243a944 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