From 10b60eba9bc55449721db912fe75bab11de43c4e Mon Sep 17 00:00:00 2001 From: Duncan Proctor Date: Tue, 22 Oct 2024 01:51:54 -0400 Subject: [PATCH] add third help hint to diagnostic error E0027 --- compiler/rustc_hir_typeck/src/pat.rs | 19 +++++++++++++++++++ .../struct_destructure_fail.stderr | 4 ++++ tests/ui/error-codes/E0027.stderr | 16 ++++++++++++++++ .../usefulness/doc-hidden-fields.stderr | 12 ++++++++++++ .../usefulness/stable-gated-fields.stderr | 4 ++++ .../usefulness/unstable-gated-fields.stderr | 8 ++++++++ tests/ui/structs/struct-field-cfg.stderr | 4 ++++ .../structs/struct-pat-derived-error.stderr | 4 ++++ .../structs/struct-tuple-field-names.stderr | 4 ++++ ...ing-field-when-specifying-same-type.stderr | 16 ++++++++++++++++ ...-missing-inaccessible-field-pattern.stderr | 4 ++++ 11 files changed, 95 insertions(+) diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs index e961752b24c2c..5868c4bc1fa51 100644 --- a/compiler/rustc_hir_typeck/src/pat.rs +++ b/compiler/rustc_hir_typeck/src/pat.rs @@ -2070,6 +2070,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { s = pluralize!(len), them = if len == 1 { "it" } else { "them" }, ), + format!( + "{}{}{}{}", + prefix, + unmentioned_fields + .iter() + .map(|(_, name)| { + let field_name = name.to_string(); + format!("{field_name}: _") + }) + .collect::>() + .join(", "), + if have_inaccessible_fields { ", .." } else { "" }, + postfix, + ), + Applicability::MachineApplicable, + ); + err.span_suggestion( + sp, + "or always ignore missing fields here", format!("{prefix}..{postfix}"), Applicability::MachineApplicable, ); diff --git a/tests/ui/destructuring-assignment/struct_destructure_fail.stderr b/tests/ui/destructuring-assignment/struct_destructure_fail.stderr index 57851ed417ea8..4c4f0663eeb43 100644 --- a/tests/ui/destructuring-assignment/struct_destructure_fail.stderr +++ b/tests/ui/destructuring-assignment/struct_destructure_fail.stderr @@ -41,6 +41,10 @@ LL | Struct { a, b } = Struct { a: 1, b: 2 }; | ~~~~~ help: if you don't care about this missing field, you can explicitly ignore it | +LL | Struct { a, b: _ } = Struct { a: 1, b: 2 }; + | ~~~~~~~~ +help: or always ignore missing fields here + | LL | Struct { a, .. } = Struct { a: 1, b: 2 }; | ~~~~~~ diff --git a/tests/ui/error-codes/E0027.stderr b/tests/ui/error-codes/E0027.stderr index 9ae97e4a994da..7bbafcf0a27ab 100644 --- a/tests/ui/error-codes/E0027.stderr +++ b/tests/ui/error-codes/E0027.stderr @@ -10,6 +10,10 @@ LL | Dog { age: x, name } => {} | ~~~~~~~~ help: if you don't care about this missing field, you can explicitly ignore it | +LL | Dog { age: x, name: _ } => {} + | ~~~~~~~~~~~ +help: or always ignore missing fields here + | LL | Dog { age: x, .. } => {} | ~~~~~~ @@ -25,6 +29,10 @@ LL | Dog { name: x, age } => {} | ~~~~~~~ help: if you don't care about this missing field, you can explicitly ignore it | +LL | Dog { name: x, age: _ } => {} + | ~~~~~~~~~~ +help: or always ignore missing fields here + | LL | Dog { name: x, .. } => {} | ~~~~~~ @@ -40,6 +48,10 @@ LL | Dog { name: x, age } => {} | ~~~~~~~ help: if you don't care about this missing field, you can explicitly ignore it | +LL | Dog { name: x, age: _ } => {} + | ~~~~~~~~~~ +help: or always ignore missing fields here + | LL | Dog { name: x, .. } => {} | ~~~~~~ @@ -55,6 +67,10 @@ LL | Dog { name, age } => {} | ~~~~~~~~~~~~~ help: if you don't care about these missing fields, you can explicitly ignore them | +LL | Dog { name: _, age: _ } => {} + | ~~~~~~~~~~~~~~~~~~~ +help: or always ignore missing fields here + | LL | Dog { .. } => {} | ~~~~~~ diff --git a/tests/ui/pattern/usefulness/doc-hidden-fields.stderr b/tests/ui/pattern/usefulness/doc-hidden-fields.stderr index f277bfbc884fb..158eac9a1bd14 100644 --- a/tests/ui/pattern/usefulness/doc-hidden-fields.stderr +++ b/tests/ui/pattern/usefulness/doc-hidden-fields.stderr @@ -21,6 +21,10 @@ LL | let HiddenStruct { one, two, .. } = HiddenStruct::default(); | ~~~~~~~~~~~ help: if you don't care about this missing field, you can explicitly ignore it | +LL | let HiddenStruct { one, two: _, .. } = HiddenStruct::default(); + | ~~~~~~~~~~~~~~ +help: or always ignore missing fields here + | LL | let HiddenStruct { one, .. } = HiddenStruct::default(); | ~~~~~~ @@ -36,6 +40,10 @@ LL | let HiddenStruct { one, hide, two } = HiddenStruct::default(); | ~~~~~~~ help: if you don't care about this missing field, you can explicitly ignore it | +LL | let HiddenStruct { one, hide, two: _ } = HiddenStruct::default(); + | ~~~~~~~~~~ +help: or always ignore missing fields here + | LL | let HiddenStruct { one, hide, .. } = HiddenStruct::default(); | ~~~~~~ @@ -51,6 +59,10 @@ LL | let InCrate { a, b, im_hidden } = InCrate { a: 0, b: false, im_hidden: | ~~~~~~~~~~~~~ help: if you don't care about this missing field, you can explicitly ignore it | +LL | let InCrate { a, b, im_hidden: _ } = InCrate { a: 0, b: false, im_hidden: 0 }; + | ~~~~~~~~~~~~~~~~ +help: or always ignore missing fields here + | LL | let InCrate { a, b, .. } = InCrate { a: 0, b: false, im_hidden: 0 }; | ~~~~~~ diff --git a/tests/ui/pattern/usefulness/stable-gated-fields.stderr b/tests/ui/pattern/usefulness/stable-gated-fields.stderr index cf98c51a2b41e..d6e9bac7c136b 100644 --- a/tests/ui/pattern/usefulness/stable-gated-fields.stderr +++ b/tests/ui/pattern/usefulness/stable-gated-fields.stderr @@ -10,6 +10,10 @@ LL | let UnstableStruct { stable, stable2, .. } = UnstableStruct::default(); | ~~~~~~~~~~~~~~~ help: if you don't care about this missing field, you can explicitly ignore it | +LL | let UnstableStruct { stable, stable2: _, .. } = UnstableStruct::default(); + | ~~~~~~~~~~~~~~~~~~ +help: or always ignore missing fields here + | LL | let UnstableStruct { stable, .. } = UnstableStruct::default(); | ~~~~~~ diff --git a/tests/ui/pattern/usefulness/unstable-gated-fields.stderr b/tests/ui/pattern/usefulness/unstable-gated-fields.stderr index e4f5fa06b3ff5..bb10e439ee23d 100644 --- a/tests/ui/pattern/usefulness/unstable-gated-fields.stderr +++ b/tests/ui/pattern/usefulness/unstable-gated-fields.stderr @@ -10,6 +10,10 @@ LL | let UnstableStruct { stable, stable2, unstable } = UnstableStruct::defa | ~~~~~~~~~~~~ help: if you don't care about this missing field, you can explicitly ignore it | +LL | let UnstableStruct { stable, stable2, unstable: _ } = UnstableStruct::default(); + | ~~~~~~~~~~~~~~~ +help: or always ignore missing fields here + | LL | let UnstableStruct { stable, stable2, .. } = UnstableStruct::default(); | ~~~~~~ @@ -25,6 +29,10 @@ LL | let UnstableStruct { stable, unstable, stable2 } = UnstableStruct::defa | ~~~~~~~~~~~ help: if you don't care about this missing field, you can explicitly ignore it | +LL | let UnstableStruct { stable, unstable, stable2: _ } = UnstableStruct::default(); + | ~~~~~~~~~~~~~~ +help: or always ignore missing fields here + | LL | let UnstableStruct { stable, unstable, .. } = UnstableStruct::default(); | ~~~~~~ diff --git a/tests/ui/structs/struct-field-cfg.stderr b/tests/ui/structs/struct-field-cfg.stderr index 2b9ba85ddcb88..f30d343d58287 100644 --- a/tests/ui/structs/struct-field-cfg.stderr +++ b/tests/ui/structs/struct-field-cfg.stderr @@ -24,6 +24,10 @@ LL | let Foo { present } = foo; | ~~~~~~~~~~~ help: if you don't care about this missing field, you can explicitly ignore it | +LL | let Foo { present: _ } = foo; + | ~~~~~~~~~~~~~~ +help: or always ignore missing fields here + | LL | let Foo { .. } = foo; | ~~~~~~ diff --git a/tests/ui/structs/struct-pat-derived-error.stderr b/tests/ui/structs/struct-pat-derived-error.stderr index 78bb018cb4be7..d1d68121cf168 100644 --- a/tests/ui/structs/struct-pat-derived-error.stderr +++ b/tests/ui/structs/struct-pat-derived-error.stderr @@ -27,6 +27,10 @@ LL | let A { x, y, b, c } = self.d; | ~~~~~~~~ help: if you don't care about these missing fields, you can explicitly ignore them | +LL | let A { x, y, b: _, c: _ } = self.d; + | ~~~~~~~~~~~~~~ +help: or always ignore missing fields here + | LL | let A { x, y, .. } = self.d; | ~~~~~~ diff --git a/tests/ui/structs/struct-tuple-field-names.stderr b/tests/ui/structs/struct-tuple-field-names.stderr index 0b837a47a8254..5f1ab2f9d6823 100644 --- a/tests/ui/structs/struct-tuple-field-names.stderr +++ b/tests/ui/structs/struct-tuple-field-names.stderr @@ -32,6 +32,10 @@ LL | if let E::S { 0: a, 1: _ } = x { | ~~~~~~~~ help: if you don't care about this missing field, you can explicitly ignore it | +LL | if let E::S { 0: a, 1: _ } = x { + | ~~~~~~~~ +help: or always ignore missing fields here + | LL | if let E::S { 0: a, .. } = x { | ~~~~~~ diff --git a/tests/ui/structs/suggest-replacing-field-when-specifying-same-type.stderr b/tests/ui/structs/suggest-replacing-field-when-specifying-same-type.stderr index e8503f540c288..af530e2b75931 100644 --- a/tests/ui/structs/suggest-replacing-field-when-specifying-same-type.stderr +++ b/tests/ui/structs/suggest-replacing-field-when-specifying-same-type.stderr @@ -19,6 +19,10 @@ LL | Foo::Bar { a, aa: 1, c, b } => (), | ~~~~~ help: if you don't care about this missing field, you can explicitly ignore it | +LL | Foo::Bar { a, aa: 1, c, b: _ } => (), + | ~~~~~~~~ +help: or always ignore missing fields here + | LL | Foo::Bar { a, aa: 1, c, .. } => (), | ~~~~~~ @@ -43,6 +47,10 @@ LL | Foo::Baz { bb: 1.0, a } => (), | ~~~~~ help: if you don't care about this missing field, you can explicitly ignore it | +LL | Foo::Baz { bb: 1.0, a: _ } => (), + | ~~~~~~~~ +help: or always ignore missing fields here + | LL | Foo::Baz { bb: 1.0, .. } => (), | ~~~~~~ @@ -64,6 +72,10 @@ LL | Foo::Bar { a, aa: "", c, b } => (), | ~~~~~ help: if you don't care about this missing field, you can explicitly ignore it | +LL | Foo::Bar { a, aa: "", c, b: _ } => (), + | ~~~~~~~~ +help: or always ignore missing fields here + | LL | Foo::Bar { a, aa: "", c, .. } => (), | ~~~~~~ @@ -85,6 +97,10 @@ LL | Foo::Baz { bb: "", a } => (), | ~~~~~ help: if you don't care about this missing field, you can explicitly ignore it | +LL | Foo::Baz { bb: "", a: _ } => (), + | ~~~~~~~~ +help: or always ignore missing fields here + | LL | Foo::Baz { bb: "", .. } => (), | ~~~~~~ diff --git a/tests/ui/typeck/issue-87872-missing-inaccessible-field-pattern.stderr b/tests/ui/typeck/issue-87872-missing-inaccessible-field-pattern.stderr index 96ac481438f7a..b9bdf6f9a3990 100644 --- a/tests/ui/typeck/issue-87872-missing-inaccessible-field-pattern.stderr +++ b/tests/ui/typeck/issue-87872-missing-inaccessible-field-pattern.stderr @@ -10,6 +10,10 @@ LL | let foo::Foo { visible, .. } = foo::Foo::default(); | ~~~~~~~~~~~~~~~ help: if you don't care about this missing field, you can explicitly ignore it | +LL | let foo::Foo { visible: _, .. } = foo::Foo::default(); + | ~~~~~~~~~~~~~~~~~~ +help: or always ignore missing fields here + | LL | let foo::Foo { .. } = foo::Foo::default(); | ~~~~~~