Skip to content

Commit a8e3d0b

Browse files
committed
Replace non-shorthand variables with _, not _var
1 parent e22e443 commit a8e3d0b

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

src/librustc_passes/liveness.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -1556,15 +1556,16 @@ impl<'tcx> Liveness<'_, 'tcx> {
15561556
.map(|(_, span)| (span, format!("{}: _", name)))
15571557
.collect::<Vec<_>>();
15581558

1559-
let non_shorthands = non_shorthands
1560-
.into_iter()
1561-
.map(|(_, span)| (span, format!("_{}", name)))
1562-
.collect::<Vec<_>>();
1563-
15641559
// If we have both shorthand and non-shorthand, prefer the "try ignoring
1565-
// the field" message.
1560+
// the field" message, and suggest `_` for the non-shorthands. If we only
1561+
// have non-shorthand, then prefix with an underscore instead.
15661562
if !shorthands.is_empty() {
1567-
shorthands.extend(non_shorthands);
1563+
shorthands.extend(
1564+
non_shorthands
1565+
.into_iter()
1566+
.map(|(_, span)| (span, "_".to_string()))
1567+
.collect::<Vec<_>>(),
1568+
);
15681569

15691570
err.multipart_suggestion(
15701571
"try ignoring the field",
@@ -1574,7 +1575,10 @@ impl<'tcx> Liveness<'_, 'tcx> {
15741575
} else {
15751576
err.multipart_suggestion(
15761577
"if this is intentional, prefix it with an underscore",
1577-
non_shorthands,
1578+
non_shorthands
1579+
.into_iter()
1580+
.map(|(_, span)| (span, format!("_{}", name)))
1581+
.collect::<Vec<_>>(),
15781582
Applicability::MachineApplicable,
15791583
);
15801584
}

src/test/ui/lint/issue-67691-unused-field-in-or-pattern.fixed

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ pub fn inner_with_ref(x: Option<MyEnum>) {
5959

6060
pub fn mixed_no_ref(x: MixedEnum) {
6161
match x {
62-
MixedEnum::A { i: _ } | MixedEnum::B(_i) => {
62+
MixedEnum::A { i: _ } | MixedEnum::B(_) => {
6363
println!("match");
6464
}
6565
}
6666
}
6767

6868
pub fn mixed_with_ref(x: MixedEnum) {
6969
match x {
70-
MixedEnum::A { i: _ } | MixedEnum::B(_i) => {
70+
MixedEnum::A { i: _ } | MixedEnum::B(_) => {
7171
println!("match");
7272
}
7373
}

src/test/ui/lint/issue-67691-unused-field-in-or-pattern.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ LL | MixedEnum::A { i } | MixedEnum::B(i) => {
5656
|
5757
help: try ignoring the field
5858
|
59-
LL | MixedEnum::A { i: _ } | MixedEnum::B(_i) => {
60-
| ^^^^ ^^
59+
LL | MixedEnum::A { i: _ } | MixedEnum::B(_) => {
60+
| ^^^^ ^
6161

6262
error: unused variable: `i`
6363
--> $DIR/issue-67691-unused-field-in-or-pattern.rs:70:24
@@ -67,8 +67,8 @@ LL | MixedEnum::A { ref i } | MixedEnum::B(ref i) => {
6767
|
6868
help: try ignoring the field
6969
|
70-
LL | MixedEnum::A { i: _ } | MixedEnum::B(_i) => {
71-
| ^^^^ ^^
70+
LL | MixedEnum::A { i: _ } | MixedEnum::B(_) => {
71+
| ^^^^ ^
7272

7373
error: aborting due to 6 previous errors
7474

0 commit comments

Comments
 (0)