Skip to content

Commit 052eb70

Browse files
committed
Suggest just the placeholder for the fields
1 parent 7c3edec commit 052eb70

File tree

7 files changed

+38
-75
lines changed

7 files changed

+38
-75
lines changed

compiler/rustc_hir_typeck/src/lib.rs

+5-30
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub use coercion::can_coerce;
4545
use fn_ctxt::FnCtxt;
4646
use rustc_data_structures::unord::UnordSet;
4747
use rustc_errors::codes::*;
48-
use rustc_errors::{pluralize, struct_span_code_err, Applicability, ErrorGuaranteed};
48+
use rustc_errors::{struct_span_code_err, Applicability, ErrorGuaranteed};
4949
use rustc_hir as hir;
5050
use rustc_hir::def::{DefKind, Res};
5151
use rustc_hir::intravisit::Visitor;
@@ -422,40 +422,15 @@ fn report_unexpected_variant_res(
422422
err
423423
}
424424
Res::Def(DefKind::Variant, _) if expr.is_none() => {
425-
let fields = tcx
426-
.expect_variant_res(res)
427-
.fields
428-
.raw
429-
.iter()
430-
.map(|field| field.name.to_ident_string())
431-
.collect::<Vec<_>>();
432-
433425
err.span_label(span, format!("not a {expected}"));
434426

435-
let span = qpath.span().shrink_to_hi().to(span.shrink_to_hi());
436427
err.span_suggestion_verbose(
437-
span,
438-
"use the struct variant pattern syntax",
439-
if fields.is_empty() {
440-
" {}".to_string()
441-
} else {
442-
format!(" {{ {} }}", fields.join(", "))
443-
},
444-
Applicability::MachineApplicable,
428+
qpath.span().shrink_to_hi().to(span.shrink_to_hi()),
429+
"the struct variant's fields are being ignored",
430+
" { /* field: _, field: _ */ }",
431+
Applicability::HasPlaceholders,
445432
);
446433

447-
if !fields.is_empty() {
448-
let len = fields.len();
449-
450-
let msg = format!(
451-
"if you don't care about {these} field{s}, you can explicitly ignore {them}",
452-
these = pluralize!("this", len),
453-
s = pluralize!(len),
454-
them = if len == 1 { "it" } else { "them" },
455-
);
456-
err.span_suggestion_verbose(span, msg, " { .. }", Applicability::MachineApplicable);
457-
}
458-
459434
err
460435
}
461436
_ => err.with_span_label(span, format!("not a {expected}")),

tests/ui/empty/empty-struct-braces-pat-1.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ error[E0533]: expected unit struct, unit variant or constant, found struct varia
44
LL | E::Empty3 => ()
55
| ^^^^^^^^^ not a unit struct, unit variant or constant
66
|
7-
help: use the struct variant pattern syntax
7+
help: the struct variant's fields are being ignored
88
|
9-
LL | E::Empty3 {} => ()
10-
| ++
9+
LL | E::Empty3 { /* field: _, field: _ */ } => ()
10+
| ++++++++++++++++++++++++++++
1111

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

2323
error: aborting due to 2 previous errors
2424

tests/ui/empty/empty-struct-braces-pat-3.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,43 @@ error[E0164]: expected tuple struct or tuple variant, found struct variant `E::E
44
LL | E::Empty3() => ()
55
| ^^^^^^^^^^^ not a tuple struct or tuple variant
66
|
7-
help: use the struct variant pattern syntax
7+
help: the struct variant's fields are being ignored
88
|
9-
LL | E::Empty3 {} => ()
10-
| ~~
9+
LL | E::Empty3 { /* field: _, field: _ */ } => ()
10+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1111

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

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

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

4545
error: aborting due to 4 previous errors
4646

tests/ui/issues/issue-63983.stderr

+3-7
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,10 @@ error[E0533]: expected unit struct, unit variant or constant, found struct varia
1313
LL | MyEnum::Struct => "",
1414
| ^^^^^^^^^^^^^^ not a unit struct, unit variant or constant
1515
|
16-
help: use the struct variant pattern syntax
16+
help: the struct variant's fields are being ignored
1717
|
18-
LL | MyEnum::Struct { s } => "",
19-
| +++++
20-
help: if you don't care about this field, you can explicitly ignore it
21-
|
22-
LL | MyEnum::Struct { .. } => "",
23-
| ++++++
18+
LL | MyEnum::Struct { /* field: _, field: _ */ } => "",
19+
| ++++++++++++++++++++++++++++
2420

2521
error: aborting due to 2 previous errors
2622

tests/ui/parser/recover/recover-from-bad-variant.stderr

+3-7
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,10 @@ error[E0164]: expected tuple struct or tuple variant, found struct variant `Enum
2020
LL | Enum::Foo(a, b) => {}
2121
| ^^^^^^^^^^^^^^^ not a tuple struct or tuple variant
2222
|
23-
help: use the struct variant pattern syntax
23+
help: the struct variant's fields are being ignored
2424
|
25-
LL | Enum::Foo { a, b } => {}
26-
| ~~~~~~~~
27-
help: if you don't care about these fields, you can explicitly ignore them
28-
|
29-
LL | Enum::Foo { .. } => {}
30-
| ~~~~~~
25+
LL | Enum::Foo { /* field: _, field: _ */ } => {}
26+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3127

3228
error[E0769]: tuple variant `Enum::Bar` written as struct variant
3329
--> $DIR/recover-from-bad-variant.rs:12:9

tests/ui/suggestions/issue-84700.stderr

+3-7
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,10 @@ error[E0164]: expected tuple struct or tuple variant, found struct variant `Farm
1313
LL | FarmAnimal::Chicken(_) => "cluck, cluck!".to_string(),
1414
| ^^^^^^^^^^^^^^^^^^^^^^ not a tuple struct or tuple variant
1515
|
16-
help: use the struct variant pattern syntax
16+
help: the struct variant's fields are being ignored
1717
|
18-
LL | FarmAnimal::Chicken { num_eggs } => "cluck, cluck!".to_string(),
19-
| ~~~~~~~~~~~~
20-
help: if you don't care about this field, you can explicitly ignore it
21-
|
22-
LL | FarmAnimal::Chicken { .. } => "cluck, cluck!".to_string(),
23-
| ~~~~~~
18+
LL | FarmAnimal::Chicken { /* field: _, field: _ */ } => "cluck, cluck!".to_string(),
19+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2420

2521
error: aborting due to 2 previous errors
2622

tests/ui/type-alias-enum-variants/incorrect-variant-form-through-alias-caught.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ error[E0533]: expected unit struct, unit variant or constant, found struct varia
1515
LL | let Alias::Braced = panic!();
1616
| ^^^^^^^^^^^^^ not a unit struct, unit variant or constant
1717
|
18-
help: use the struct variant pattern syntax
18+
help: the struct variant's fields are being ignored
1919
|
20-
LL | let Alias::Braced {} = panic!();
21-
| ++
20+
LL | let Alias::Braced { /* field: _, field: _ */ } = panic!();
21+
| ++++++++++++++++++++++++++++
2222

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

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

0 commit comments

Comments
 (0)