Skip to content

Commit 9959d6d

Browse files
committed
Only suggest .. if more than one field is missing
1 parent fe82cc3 commit 9959d6d

File tree

9 files changed

+68
-135
lines changed

9 files changed

+68
-135
lines changed

compiler/rustc_typeck/src/check/pat.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,27 +1061,30 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10611061
wildcard_sugg = String::from(", ") + &wildcard_sugg;
10621062
}
10631063

1064-
err.span_suggestion(
1064+
err.span_suggestion_short(
10651065
after_fields_span,
10661066
"use `_` to explicitly ignore each field",
10671067
wildcard_sugg,
10681068
Applicability::MaybeIncorrect,
10691069
);
10701070

1071-
if subpats.is_empty() || all_wildcards {
1072-
err.span_suggestion(
1073-
all_fields_span,
1074-
"use `..` to ignore all fields",
1075-
String::from(".."),
1076-
Applicability::MaybeIncorrect,
1077-
);
1078-
} else {
1079-
err.span_suggestion(
1080-
after_fields_span,
1081-
"use `..` to ignore the rest of the fields",
1082-
String::from(", .."),
1083-
Applicability::MaybeIncorrect,
1084-
);
1071+
// Only suggest `..` if more than one field is missing.
1072+
if fields.len() - subpats.len() > 1 {
1073+
if subpats.is_empty() || all_wildcards {
1074+
err.span_suggestion_short(
1075+
all_fields_span,
1076+
"use `..` to ignore all fields",
1077+
String::from(".."),
1078+
Applicability::MaybeIncorrect,
1079+
);
1080+
} else {
1081+
err.span_suggestion_short(
1082+
after_fields_span,
1083+
"use `..` to ignore the rest of the fields",
1084+
String::from(", .."),
1085+
Applicability::MaybeIncorrect,
1086+
);
1087+
}
10851088
}
10861089
}
10871090

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

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,10 @@ LL | struct TupleStruct<S, T>(S, T);
3030
| ------------------------------- tuple struct defined here
3131
...
3232
LL | TupleStruct(_) = TupleStruct(1, 2);
33-
| ^^^^^^^^^^^^^^ expected 2 fields, found 1
34-
|
35-
help: use `_` to explicitly ignore each field
36-
|
37-
LL | TupleStruct(_, _) = TupleStruct(1, 2);
38-
| ^^^
39-
help: use `..` to ignore all fields
40-
|
41-
LL | TupleStruct(..) = TupleStruct(1, 2);
42-
| ^^
33+
| ^^^^^^^^^^^^^-
34+
| | |
35+
| | help: use `_` to explicitly ignore each field
36+
| expected 2 fields, found 1
4337

4438
error[E0023]: this pattern has 3 fields, but the corresponding tuple variant has 2 fields
4539
--> $DIR/tuple_struct_destructure_fail.rs:34:5
@@ -57,16 +51,10 @@ LL | SingleVariant(S, T)
5751
| ------------------- tuple variant defined here
5852
...
5953
LL | Enum::SingleVariant(_) = Enum::SingleVariant(1, 2);
60-
| ^^^^^^^^^^^^^^^^^^^^^^ expected 2 fields, found 1
61-
|
62-
help: use `_` to explicitly ignore each field
63-
|
64-
LL | Enum::SingleVariant(_, _) = Enum::SingleVariant(1, 2);
65-
| ^^^
66-
help: use `..` to ignore all fields
67-
|
68-
LL | Enum::SingleVariant(..) = Enum::SingleVariant(1, 2);
69-
| ^^
54+
| ^^^^^^^^^^^^^^^^^^^^^-
55+
| | |
56+
| | help: use `_` to explicitly ignore each field
57+
| expected 2 fields, found 1
7058

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

src/test/ui/error-codes/E0023.stderr

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,10 @@ LL | Apple(String, String),
55
| --------------------- tuple variant defined here
66
...
77
LL | Fruit::Apple(a) => {},
8-
| ^^^^^^^^^^^^^^^ expected 2 fields, found 1
9-
|
10-
help: use `_` to explicitly ignore each field
11-
|
12-
LL | Fruit::Apple(a, _) => {},
13-
| ^^^
14-
help: use `..` to ignore the rest of the fields
15-
|
16-
LL | Fruit::Apple(a, ..) => {},
17-
| ^^^^
8+
| ^^^^^^^^^^^^^^-
9+
| | |
10+
| | help: use `_` to explicitly ignore each field
11+
| expected 2 fields, found 1
1812

1913
error[E0023]: this pattern has 3 fields, but the corresponding tuple variant has 2 fields
2014
--> $DIR/E0023.rs:12:9

src/test/ui/issues/issue-67037-pat-tup-scrut-ty-diff-less-fields.stderr

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,10 @@ LL | struct P<T>(T); // 1 type parameter wanted
1616
| --------------- tuple struct defined here
1717
...
1818
LL | let P() = U {};
19-
| ^^^ expected 1 field, found 0
20-
|
21-
help: use `_` to explicitly ignore each field
22-
|
23-
LL | let P(_) = U {};
24-
| ^
25-
help: use `..` to ignore all fields
26-
|
27-
LL | let P(..) = U {};
28-
| ^^
19+
| ^^-
20+
| | |
21+
| | help: use `_` to explicitly ignore each field
22+
| expected 1 field, found 0
2923

3024
error: aborting due to 2 previous errors
3125

src/test/ui/issues/issue-72574-2.stderr

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,10 @@ LL | struct Binder(i32, i32, i32);
2525
| ----------------------------- tuple struct defined here
2626
...
2727
LL | Binder(_a, _x @ ..) => {}
28-
| ^^^^^^^^^^^^^^^^^^^ expected 3 fields, found 2
29-
|
30-
help: use `_` to explicitly ignore each field
31-
|
32-
LL | Binder(_a, _x @ .., _) => {}
33-
| ^^^
34-
help: use `..` to ignore the rest of the fields
35-
|
36-
LL | Binder(_a, _x @ .., ..) => {}
37-
| ^^^^
28+
| ^^^^^^^^^^^^^^^^^^-
29+
| | |
30+
| | help: use `_` to explicitly ignore each field
31+
| expected 3 fields, found 2
3832

3933
error: aborting due to 3 previous errors
4034

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,10 @@ LL | Rgb(usize, usize, usize),
55
| ------------------------ tuple variant defined here
66
...
77
LL | Color::Rgb(_, _) => { }
8-
| ^^^^^^^^^^^^^^^^ expected 3 fields, found 2
9-
|
10-
help: use `_` to explicitly ignore each field
11-
|
12-
LL | Color::Rgb(_, _, _) => { }
13-
| ^^^
14-
help: use `..` to ignore all fields
15-
|
16-
LL | Color::Rgb(..) => { }
17-
| ^^
8+
| ^^^^^^^^^^^^^^^-
9+
| | |
10+
| | help: use `_` to explicitly ignore each field
11+
| expected 3 fields, found 2
1812

1913
error: aborting due to previous error
2014

src/test/ui/pattern/issue-74539.stderr

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,10 @@ LL | A(u8, u8),
2525
| --------- tuple variant defined here
2626
...
2727
LL | E::A(x @ ..) => {
28-
| ^^^^^^^^^^^^ expected 2 fields, found 1
29-
|
30-
help: use `_` to explicitly ignore each field
31-
|
32-
LL | E::A(x @ .., _) => {
33-
| ^^^
34-
help: use `..` to ignore the rest of the fields
35-
|
36-
LL | E::A(x @ .., ..) => {
37-
| ^^^^
28+
| ^^^^^^^^^^^-
29+
| | |
30+
| | help: use `_` to explicitly ignore each field
31+
| expected 2 fields, found 1
3832

3933
error: aborting due to 3 previous errors
4034

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ fn main() {
88
S(x) => {}
99
//~^ ERROR this pattern has 1 field, but the corresponding tuple struct has 2 fields
1010
//~| HELP use `_` to explicitly ignore each field
11-
//~| HELP use `..` to ignore the rest of the fields
1211
}
1312
match S(0, 1.0) {
1413
S(_) => {}
1514
//~^ ERROR this pattern has 1 field, but the corresponding tuple struct has 2 fields
1615
//~| HELP use `_` to explicitly ignore each field
17-
//~| HELP use `..` to ignore all fields
1816
}
1917
match S(0, 1.0) {
2018
S() => {}
@@ -27,13 +25,11 @@ fn main() {
2725
E::S(x) => {}
2826
//~^ ERROR this pattern has 1 field, but the corresponding tuple variant has 2 fields
2927
//~| HELP use `_` to explicitly ignore each field
30-
//~| HELP use `..` to ignore the rest of the fields
3128
}
3229
match E::S(0, 1.0) {
3330
E::S(_) => {}
3431
//~^ ERROR this pattern has 1 field, but the corresponding tuple variant has 2 fields
3532
//~| HELP use `_` to explicitly ignore each field
36-
//~| HELP use `..` to ignore all fields
3733
}
3834
match E::S(0, 1.0) {
3935
E::S() => {}

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

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0532]: expected unit struct, unit variant or constant, found tuple variant `E::S`
2-
--> $DIR/pat-tuple-underfield.rs:45:9
2+
--> $DIR/pat-tuple-underfield.rs:41:9
33
|
44
LL | S(i32, f32),
55
| ----------- `E::S` defined here
@@ -14,37 +14,25 @@ LL | struct S(i32, f32);
1414
| ------------------- tuple struct defined here
1515
...
1616
LL | S(x) => {}
17-
| ^^^^ expected 2 fields, found 1
18-
|
19-
help: use `_` to explicitly ignore each field
20-
|
21-
LL | S(x, _) => {}
22-
| ^^^
23-
help: use `..` to ignore the rest of the fields
24-
|
25-
LL | S(x, ..) => {}
26-
| ^^^^
17+
| ^^^-
18+
| | |
19+
| | help: use `_` to explicitly ignore each field
20+
| expected 2 fields, found 1
2721

2822
error[E0023]: this pattern has 1 field, but the corresponding tuple struct has 2 fields
29-
--> $DIR/pat-tuple-underfield.rs:14:9
23+
--> $DIR/pat-tuple-underfield.rs:13:9
3024
|
3125
LL | struct S(i32, f32);
3226
| ------------------- tuple struct defined here
3327
...
3428
LL | S(_) => {}
35-
| ^^^^ expected 2 fields, found 1
36-
|
37-
help: use `_` to explicitly ignore each field
38-
|
39-
LL | S(_, _) => {}
40-
| ^^^
41-
help: use `..` to ignore all fields
42-
|
43-
LL | S(..) => {}
44-
| ^^
29+
| ^^^-
30+
| | |
31+
| | help: use `_` to explicitly ignore each field
32+
| expected 2 fields, found 1
4533

4634
error[E0023]: this pattern has 0 fields, but the corresponding tuple struct has 2 fields
47-
--> $DIR/pat-tuple-underfield.rs:20:9
35+
--> $DIR/pat-tuple-underfield.rs:18:9
4836
|
4937
LL | struct S(i32, f32);
5038
| ------------------- tuple struct defined here
@@ -62,43 +50,31 @@ LL | S(..) => {}
6250
| ^^
6351

6452
error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields
65-
--> $DIR/pat-tuple-underfield.rs:27:9
53+
--> $DIR/pat-tuple-underfield.rs:25:9
6654
|
6755
LL | S(i32, f32),
6856
| ----------- tuple variant defined here
6957
...
7058
LL | E::S(x) => {}
71-
| ^^^^^^^ expected 2 fields, found 1
72-
|
73-
help: use `_` to explicitly ignore each field
74-
|
75-
LL | E::S(x, _) => {}
76-
| ^^^
77-
help: use `..` to ignore the rest of the fields
78-
|
79-
LL | E::S(x, ..) => {}
80-
| ^^^^
59+
| ^^^^^^-
60+
| | |
61+
| | help: use `_` to explicitly ignore each field
62+
| expected 2 fields, found 1
8163

8264
error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields
83-
--> $DIR/pat-tuple-underfield.rs:33:9
65+
--> $DIR/pat-tuple-underfield.rs:30:9
8466
|
8567
LL | S(i32, f32),
8668
| ----------- tuple variant defined here
8769
...
8870
LL | E::S(_) => {}
89-
| ^^^^^^^ expected 2 fields, found 1
90-
|
91-
help: use `_` to explicitly ignore each field
92-
|
93-
LL | E::S(_, _) => {}
94-
| ^^^
95-
help: use `..` to ignore all fields
96-
|
97-
LL | E::S(..) => {}
98-
| ^^
71+
| ^^^^^^-
72+
| | |
73+
| | help: use `_` to explicitly ignore each field
74+
| expected 2 fields, found 1
9975

10076
error[E0023]: this pattern has 0 fields, but the corresponding tuple variant has 2 fields
101-
--> $DIR/pat-tuple-underfield.rs:39:9
77+
--> $DIR/pat-tuple-underfield.rs:35:9
10278
|
10379
LL | S(i32, f32),
10480
| ----------- tuple variant defined here

0 commit comments

Comments
 (0)