Skip to content

Commit 90c69c8

Browse files
committed
add third help hint to diagnostic error E0027
1 parent 814df6e commit 90c69c8

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

compiler/rustc_hir_typeck/src/pat.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2053,7 +2053,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20532053
.iter()
20542054
.map(|(_, name)| {
20552055
let field_name = name.to_string();
2056-
if is_number(&field_name) { format!("{field_name}: _") } else { field_name }
2056+
if is_number(&field_name) { format!("{}: _", field_name) } else { field_name }
20572057
})
20582058
.collect::<Vec<_>>()
20592059
.join(", "),
@@ -2065,11 +2065,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
20652065
err.span_suggestion(
20662066
sp,
20672067
format!(
2068-
"if you don't care about {these} missing field{s}, you can explicitly ignore {them}",
2069-
these = pluralize!("this", len),
2068+
"if the field{s} {are} not relevant, discard {them} explicitly",
2069+
are = if len == 1 { "is" } else { "are" },
20702070
s = pluralize!(len),
20712071
them = if len == 1 { "it" } else { "them" },
20722072
),
2073+
format!(
2074+
"{}{}{}{}",
2075+
prefix,
2076+
unmentioned_fields
2077+
.iter()
2078+
.map(|(_, name)| format!("{}: _", name.to_string()))
2079+
.collect::<Vec<_>>()
2080+
.join(", "),
2081+
if have_inaccessible_fields { ", .." } else { "" },
2082+
postfix,
2083+
),
2084+
Applicability::MachineApplicable,
2085+
);
2086+
2087+
err.span_suggestion(
2088+
sp,
2089+
format!("or allow missing fields here",),
20732090
format!("{prefix}..{postfix}"),
20742091
Applicability::MachineApplicable,
20752092
);

tests/ui/error-codes/E0027.stderr

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ help: include the missing field in the pattern
88
|
99
LL | Dog { age: x, name } => {}
1010
| ~~~~~~~~
11-
help: if you don't care about this missing field, you can explicitly ignore it
11+
help: if the field is not relevant, discard it explicitly
12+
|
13+
LL | Dog { age: x, name: _ } => {}
14+
| ~~~~~~~~~~~
15+
help: or allow missing fields here
1216
|
1317
LL | Dog { age: x, .. } => {}
1418
| ~~~~~~
@@ -23,7 +27,11 @@ help: include the missing field in the pattern
2327
|
2428
LL | Dog { name: x, age } => {}
2529
| ~~~~~~~
26-
help: if you don't care about this missing field, you can explicitly ignore it
30+
help: if the field is not relevant, discard it explicitly
31+
|
32+
LL | Dog { name: x, age: _ } => {}
33+
| ~~~~~~~~~~
34+
help: or allow missing fields here
2735
|
2836
LL | Dog { name: x, .. } => {}
2937
| ~~~~~~
@@ -38,7 +46,11 @@ help: include the missing field in the pattern
3846
|
3947
LL | Dog { name: x, age } => {}
4048
| ~~~~~~~
41-
help: if you don't care about this missing field, you can explicitly ignore it
49+
help: if the field is not relevant, discard it explicitly
50+
|
51+
LL | Dog { name: x, age: _ } => {}
52+
| ~~~~~~~~~~
53+
help: or allow missing fields here
4254
|
4355
LL | Dog { name: x, .. } => {}
4456
| ~~~~~~
@@ -53,7 +65,11 @@ help: include the missing fields in the pattern
5365
|
5466
LL | Dog { name, age } => {}
5567
| ~~~~~~~~~~~~~
56-
help: if you don't care about these missing fields, you can explicitly ignore them
68+
help: if the fields are not relevant, discard them explicitly
69+
|
70+
LL | Dog { name: _, age: _ } => {}
71+
| ~~~~~~~~~~~~~~~~~~~
72+
help: or allow missing fields here
5773
|
5874
LL | Dog { .. } => {}
5975
| ~~~~~~

0 commit comments

Comments
 (0)