Skip to content

Commit

Permalink
Suggest just the placeholder for the fields
Browse files Browse the repository at this point in the history
  • Loading branch information
tunawasabi committed Aug 25, 2024
1 parent 7c3edec commit 052eb70
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 75 deletions.
35 changes: 5 additions & 30 deletions compiler/rustc_hir_typeck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub use coercion::can_coerce;
use fn_ctxt::FnCtxt;
use rustc_data_structures::unord::UnordSet;
use rustc_errors::codes::*;
use rustc_errors::{pluralize, struct_span_code_err, Applicability, ErrorGuaranteed};
use rustc_errors::{struct_span_code_err, Applicability, ErrorGuaranteed};
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::intravisit::Visitor;
Expand Down Expand Up @@ -422,40 +422,15 @@ fn report_unexpected_variant_res(
err
}
Res::Def(DefKind::Variant, _) if expr.is_none() => {
let fields = tcx
.expect_variant_res(res)
.fields
.raw
.iter()
.map(|field| field.name.to_ident_string())
.collect::<Vec<_>>();

err.span_label(span, format!("not a {expected}"));

let span = qpath.span().shrink_to_hi().to(span.shrink_to_hi());
err.span_suggestion_verbose(
span,
"use the struct variant pattern syntax",
if fields.is_empty() {
" {}".to_string()
} else {
format!(" {{ {} }}", fields.join(", "))
},
Applicability::MachineApplicable,
qpath.span().shrink_to_hi().to(span.shrink_to_hi()),
"the struct variant's fields are being ignored",
" { /* field: _, field: _ */ }",
Applicability::HasPlaceholders,
);

if !fields.is_empty() {
let len = fields.len();

let msg = format!(
"if you don't care about {these} field{s}, you can explicitly ignore {them}",
these = pluralize!("this", len),
s = pluralize!(len),
them = if len == 1 { "it" } else { "them" },
);
err.span_suggestion_verbose(span, msg, " { .. }", Applicability::MachineApplicable);
}

err
}
_ => err.with_span_label(span, format!("not a {expected}")),
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/empty/empty-struct-braces-pat-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ error[E0533]: expected unit struct, unit variant or constant, found struct varia
LL | E::Empty3 => ()
| ^^^^^^^^^ not a unit struct, unit variant or constant
|
help: use the struct variant pattern syntax
help: the struct variant's fields are being ignored
|
LL | E::Empty3 {} => ()
| ++
LL | E::Empty3 { /* field: _, field: _ */ } => ()
| ++++++++++++++++++++++++++++

error[E0533]: expected unit struct, unit variant or constant, found struct variant `XE::XEmpty3`
--> $DIR/empty-struct-braces-pat-1.rs:31:9
|
LL | XE::XEmpty3 => ()
| ^^^^^^^^^^^ not a unit struct, unit variant or constant
|
help: use the struct variant pattern syntax
help: the struct variant's fields are being ignored
|
LL | XE::XEmpty3 {} => ()
| ++
LL | XE::XEmpty3 { /* field: _, field: _ */ } => ()
| ++++++++++++++++++++++++++++

error: aborting due to 2 previous errors

Expand Down
24 changes: 12 additions & 12 deletions tests/ui/empty/empty-struct-braces-pat-3.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,43 @@ error[E0164]: expected tuple struct or tuple variant, found struct variant `E::E
LL | E::Empty3() => ()
| ^^^^^^^^^^^ not a tuple struct or tuple variant
|
help: use the struct variant pattern syntax
help: the struct variant's fields are being ignored
|
LL | E::Empty3 {} => ()
| ~~
LL | E::Empty3 { /* field: _, field: _ */ } => ()
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0164]: expected tuple struct or tuple variant, found struct variant `XE::XEmpty3`
--> $DIR/empty-struct-braces-pat-3.rs:21:9
|
LL | XE::XEmpty3() => ()
| ^^^^^^^^^^^^^ not a tuple struct or tuple variant
|
help: use the struct variant pattern syntax
help: the struct variant's fields are being ignored
|
LL | XE::XEmpty3 {} => ()
| ~~
LL | XE::XEmpty3 { /* field: _, field: _ */ } => ()
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0164]: expected tuple struct or tuple variant, found struct variant `E::Empty3`
--> $DIR/empty-struct-braces-pat-3.rs:25:9
|
LL | E::Empty3(..) => ()
| ^^^^^^^^^^^^^ not a tuple struct or tuple variant
|
help: use the struct variant pattern syntax
help: the struct variant's fields are being ignored
|
LL | E::Empty3 {} => ()
| ~~
LL | E::Empty3 { /* field: _, field: _ */ } => ()
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0164]: expected tuple struct or tuple variant, found struct variant `XE::XEmpty3`
--> $DIR/empty-struct-braces-pat-3.rs:29:9
|
LL | XE::XEmpty3(..) => ()
| ^^^^^^^^^^^^^^^ not a tuple struct or tuple variant
|
help: use the struct variant pattern syntax
help: the struct variant's fields are being ignored
|
LL | XE::XEmpty3 {} => ()
| ~~
LL | XE::XEmpty3 { /* field: _, field: _ */ } => ()
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error: aborting due to 4 previous errors

Expand Down
10 changes: 3 additions & 7 deletions tests/ui/issues/issue-63983.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,10 @@ error[E0533]: expected unit struct, unit variant or constant, found struct varia
LL | MyEnum::Struct => "",
| ^^^^^^^^^^^^^^ not a unit struct, unit variant or constant
|
help: use the struct variant pattern syntax
help: the struct variant's fields are being ignored
|
LL | MyEnum::Struct { s } => "",
| +++++
help: if you don't care about this field, you can explicitly ignore it
|
LL | MyEnum::Struct { .. } => "",
| ++++++
LL | MyEnum::Struct { /* field: _, field: _ */ } => "",
| ++++++++++++++++++++++++++++

error: aborting due to 2 previous errors

Expand Down
10 changes: 3 additions & 7 deletions tests/ui/parser/recover/recover-from-bad-variant.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@ error[E0164]: expected tuple struct or tuple variant, found struct variant `Enum
LL | Enum::Foo(a, b) => {}
| ^^^^^^^^^^^^^^^ not a tuple struct or tuple variant
|
help: use the struct variant pattern syntax
help: the struct variant's fields are being ignored
|
LL | Enum::Foo { a, b } => {}
| ~~~~~~~~
help: if you don't care about these fields, you can explicitly ignore them
|
LL | Enum::Foo { .. } => {}
| ~~~~~~
LL | Enum::Foo { /* field: _, field: _ */ } => {}
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0769]: tuple variant `Enum::Bar` written as struct variant
--> $DIR/recover-from-bad-variant.rs:12:9
Expand Down
10 changes: 3 additions & 7 deletions tests/ui/suggestions/issue-84700.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,10 @@ error[E0164]: expected tuple struct or tuple variant, found struct variant `Farm
LL | FarmAnimal::Chicken(_) => "cluck, cluck!".to_string(),
| ^^^^^^^^^^^^^^^^^^^^^^ not a tuple struct or tuple variant
|
help: use the struct variant pattern syntax
help: the struct variant's fields are being ignored
|
LL | FarmAnimal::Chicken { num_eggs } => "cluck, cluck!".to_string(),
| ~~~~~~~~~~~~
help: if you don't care about this field, you can explicitly ignore it
|
LL | FarmAnimal::Chicken { .. } => "cluck, cluck!".to_string(),
| ~~~~~~
LL | FarmAnimal::Chicken { /* field: _, field: _ */ } => "cluck, cluck!".to_string(),
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error: aborting due to 2 previous errors

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ error[E0533]: expected unit struct, unit variant or constant, found struct varia
LL | let Alias::Braced = panic!();
| ^^^^^^^^^^^^^ not a unit struct, unit variant or constant
|
help: use the struct variant pattern syntax
help: the struct variant's fields are being ignored
|
LL | let Alias::Braced {} = panic!();
| ++
LL | let Alias::Braced { /* field: _, field: _ */ } = panic!();
| ++++++++++++++++++++++++++++

error[E0164]: expected tuple struct or tuple variant, found struct variant `Alias::Braced`
--> $DIR/incorrect-variant-form-through-alias-caught.rs:12:9
|
LL | let Alias::Braced(..) = panic!();
| ^^^^^^^^^^^^^^^^^ not a tuple struct or tuple variant
|
help: use the struct variant pattern syntax
help: the struct variant's fields are being ignored
|
LL | let Alias::Braced {} = panic!();
| ~~
LL | let Alias::Braced { /* field: _, field: _ */ } = panic!();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0618]: expected function, found enum variant `Alias::Unit`
--> $DIR/incorrect-variant-form-through-alias-caught.rs:15:5
Expand Down

0 comments on commit 052eb70

Please sign in to comment.