Skip to content

Commit f3d9df5

Browse files
committed
Suggest Variant(..) if all of the mentioned fields are _
1 parent 5fe61a7 commit f3d9df5

File tree

4 files changed

+34
-19
lines changed

4 files changed

+34
-19
lines changed

compiler/rustc_typeck/src/check/pat.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use rustc_trait_selection::traits::{ObligationCause, Pattern};
2020

2121
use std::cmp;
2222
use std::collections::hash_map::Entry::{Occupied, Vacant};
23-
use std::iter;
2423

2524
use super::report_unexpected_variant_res;
2625

@@ -1048,26 +1047,42 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10481047
} else {
10491048
pat_span.with_hi(pat_span.hi() - BytePos(1)).shrink_to_hi()
10501049
};
1050+
let all_fields_span = match subpats {
1051+
[] => after_fields_span,
1052+
[field] => field.span,
1053+
[first, .., last] => first.span.to(last.span),
1054+
};
1055+
1056+
// Check if all the fields in the pattern are wildcards.
1057+
let all_wildcards = subpats.iter().all(|pat| matches!(pat.kind, PatKind::Wild));
10511058

10521059
let mut wildcard_sugg = vec!["_"; fields.len() - subpats.len()].join(", ");
10531060
if !subpats.is_empty() {
10541061
wildcard_sugg = String::from(", ") + &wildcard_sugg;
10551062
}
10561063

1057-
let rest_sugg = if subpats.is_empty() { "..".to_owned() } else { ", ..".to_owned() };
1058-
10591064
err.span_suggestion(
10601065
after_fields_span,
10611066
"use `_` to explicitly ignore each field",
10621067
wildcard_sugg,
10631068
Applicability::MaybeIncorrect,
10641069
);
1065-
err.span_suggestion(
1066-
after_fields_span,
1067-
"use `..` to ignore all unmentioned fields",
1068-
rest_sugg,
1069-
Applicability::MaybeIncorrect,
1070-
);
1070+
1071+
if subpats.is_empty() || all_wildcards {
1072+
err.span_suggestion(
1073+
all_fields_span,
1074+
"use `..` to ignore all unmentioned fields",
1075+
String::from(".."),
1076+
Applicability::MaybeIncorrect,
1077+
);
1078+
} else {
1079+
err.span_suggestion(
1080+
after_fields_span,
1081+
"use `..` to ignore all unmentioned fields",
1082+
String::from(", .."),
1083+
Applicability::MaybeIncorrect,
1084+
);
1085+
}
10711086
}
10721087

10731088
err.emit();

src/test/ui/destructuring-assignment/tuple_struct_destructure_fail.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ LL | TupleStruct(_, _) = TupleStruct(1, 2);
3838
| ^^^
3939
help: use `..` to ignore all unmentioned fields
4040
|
41-
LL | TupleStruct(_, ..) = TupleStruct(1, 2);
42-
| ^^^^
41+
LL | TupleStruct(..) = TupleStruct(1, 2);
42+
| ^^
4343

4444
error[E0023]: this pattern has 3 fields, but the corresponding tuple variant has 2 fields
4545
--> $DIR/tuple_struct_destructure_fail.rs:34:5
@@ -65,8 +65,8 @@ LL | Enum::SingleVariant(_, _) = Enum::SingleVariant(1, 2);
6565
| ^^^
6666
help: use `..` to ignore all unmentioned fields
6767
|
68-
LL | Enum::SingleVariant(_, ..) = Enum::SingleVariant(1, 2);
69-
| ^^^^
68+
LL | Enum::SingleVariant(..) = Enum::SingleVariant(1, 2);
69+
| ^^
7070

7171
error[E0070]: invalid left-hand side of assignment
7272
--> $DIR/tuple_struct_destructure_fail.rs:40:12

src/test/ui/match/match-pattern-field-mismatch.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ LL | Color::Rgb(_, _, _) => { }
1313
| ^^^
1414
help: use `..` to ignore all unmentioned fields
1515
|
16-
LL | Color::Rgb(_, _, ..) => { }
17-
| ^^^^
16+
LL | Color::Rgb(..) => { }
17+
| ^^
1818

1919
error: aborting due to previous error
2020

src/test/ui/pattern/pat-tuple-underfield.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ LL | S(_, _) => {}
4040
| ^^^
4141
help: use `..` to ignore all unmentioned fields
4242
|
43-
LL | S(_, ..) => {}
44-
| ^^^^
43+
LL | S(..) => {}
44+
| ^^
4545

4646
error[E0023]: this pattern has 0 fields, but the corresponding tuple struct has 2 fields
4747
--> $DIR/pat-tuple-underfield.rs:20:9
@@ -94,8 +94,8 @@ LL | E::S(_, _) => {}
9494
| ^^^
9595
help: use `..` to ignore all unmentioned fields
9696
|
97-
LL | E::S(_, ..) => {}
98-
| ^^^^
97+
LL | E::S(..) => {}
98+
| ^^
9999

100100
error[E0023]: this pattern has 0 fields, but the corresponding tuple variant has 2 fields
101101
--> $DIR/pat-tuple-underfield.rs:39:9

0 commit comments

Comments
 (0)