Skip to content

Commit 408066f

Browse files
committed
Do not suggest introducing new binding when other suggestions are present
1 parent 39631fd commit 408066f

7 files changed

Lines changed: 37 additions & 167 deletions

File tree

compiler/rustc_errors/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@ impl Suggestions {
138138
Suggestions::Disabled => Vec::new(),
139139
}
140140
}
141+
142+
pub fn len(&self) -> usize {
143+
match self {
144+
Suggestions::Enabled(suggestions) => suggestions.len(),
145+
Suggestions::Sealed(suggestions) => suggestions.len(),
146+
Suggestions::Disabled => 0,
147+
}
148+
}
141149
}
142150

143151
impl Default for Suggestions {

compiler/rustc_hir_typeck/src/pat.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1644,7 +1644,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16441644
return;
16451645
}
16461646
};
1647-
if let [hir::PathSegment { ident, args: None, .. }] = segments {
1647+
if let [hir::PathSegment { ident, args: None, .. }] = segments
1648+
&& e.suggestions.len() == 0
1649+
{
16481650
e.span_label(span, format!("{} defined here", res.descr()));
16491651
e.span_label(
16501652
pat_span,

tests/ui/did_you_mean/compatible-variants-in-pat.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ fn b(s: Option<S>) {
2121
S => {
2222
//~^ ERROR mismatched types
2323
//~| HELP try wrapping
24-
//~| HELP introduce a new binding instead
2524
}
2625
_ => {}
2726
}
@@ -32,7 +31,6 @@ fn c(s: Result<S, S>) {
3231
S => {
3332
//~^ ERROR mismatched types
3433
//~| HELP try wrapping
35-
//~| HELP introduce a new binding instead
3634
}
3735
_ => {}
3836
}

tests/ui/did_you_mean/compatible-variants-in-pat.stderr

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,25 @@ LL | Foo::Bar(Bar { x }) => {
1414
error[E0308]: mismatched types
1515
--> $DIR/compatible-variants-in-pat.rs:21:9
1616
|
17-
LL | struct S;
18-
| -------- unit struct defined here
19-
...
2017
LL | match s {
2118
| - this expression has type `Option<S>`
2219
LL | S => {
23-
| ^
24-
| |
25-
| expected `Option<S>`, found `S`
26-
| `S` is interpreted as a unit struct, not a new binding
20+
| ^ expected `Option<S>`, found `S`
2721
|
2822
= note: expected enum `Option<S>`
2923
found struct `S`
3024
help: try wrapping the pattern in `Some`
3125
|
3226
LL | Some(S) => {
3327
| +++++ +
34-
help: introduce a new binding instead
35-
|
36-
LL - S => {
37-
LL + other_s => {
38-
|
3928

4029
error[E0308]: mismatched types
41-
--> $DIR/compatible-variants-in-pat.rs:32:9
30+
--> $DIR/compatible-variants-in-pat.rs:31:9
4231
|
43-
LL | struct S;
44-
| -------- unit struct defined here
45-
...
4632
LL | match s {
4733
| - this expression has type `Result<S, S>`
4834
LL | S => {
49-
| ^
50-
| |
51-
| expected `Result<S, S>`, found `S`
52-
| `S` is interpreted as a unit struct, not a new binding
35+
| ^ expected `Result<S, S>`, found `S`
5336
|
5437
= note: expected enum `Result<S, S>`
5538
found struct `S`
@@ -59,11 +42,6 @@ LL | Ok(S) => {
5942
| +++ +
6043
LL | Err(S) => {
6144
| ++++ +
62-
help: introduce a new binding instead
63-
|
64-
LL - S => {
65-
LL + other_s => {
66-
|
6745

6846
error: aborting due to 3 previous errors
6947

tests/ui/match/issue-12552.stderr

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,14 @@ LL | match t {
2020
| - this expression has type `Result<_, {integer}>`
2121
...
2222
LL | None => ()
23-
| ^^^^
24-
| |
25-
| expected `Result<_, {integer}>`, found `Option<_>`
26-
| `None` is interpreted as a unit variant, not a new binding
27-
|
28-
--> $SRC_DIR/core/src/option.rs:LL:COL
29-
|
30-
= note: unit variant defined here
23+
| ^^^^ expected `Result<_, {integer}>`, found `Option<_>`
3124
|
3225
= note: expected enum `Result<_, {integer}>`
3326
found enum `Option<_>`
3427
help: try wrapping the pattern in `Ok`
3528
|
3629
LL | Ok(None) => ()
3730
| +++ +
38-
help: introduce a new binding instead
39-
|
40-
LL - None => ()
41-
LL + other_none => ()
42-
|
4331

4432
error: aborting due to 2 previous errors
4533

tests/ui/suggestions/suggest-deref-in-match-issue-132784.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ fn main() {
88
//~^ ERROR mismatched types
99
None => {}
1010
//~^ ERROR mismatched types
11-
//~| HELP introduce a new binding instead
1211
}
1312

1413
match &x {
@@ -18,7 +17,6 @@ fn main() {
1817
//~^ ERROR mismatched types
1918
None => {}
2019
//~^ ERROR mismatched types
21-
//~| HELP introduce a new binding instead
2220
}
2321

2422
let mut y = Box::new(Some(1));
@@ -29,7 +27,6 @@ fn main() {
2927
//~^ ERROR mismatched types
3028
None => {}
3129
//~^ ERROR mismatched types
32-
//~| HELP introduce a new binding instead
3330
}
3431

3532
let mut z = Arc::new(Some(1));
@@ -40,7 +37,6 @@ fn main() {
4037
//~^ ERROR mismatched types
4138
None => {}
4239
//~^ ERROR mismatched types
43-
//~| HELP introduce a new binding instead
4440
}
4541

4642
let z_const: &Arc<Option<i32>> = &z;
@@ -51,7 +47,6 @@ fn main() {
5147
//~^ ERROR mismatched types
5248
None => {}
5349
//~^ ERROR mismatched types
54-
//~| HELP introduce a new binding instead
5550
}
5651

5752
// Normal reference because Arc doesn't implement DerefMut.
@@ -63,7 +58,6 @@ fn main() {
6358
//~^ ERROR mismatched types
6459
None => {}
6560
//~^ ERROR mismatched types
66-
//~| HELP introduce a new binding instead
6761
}
6862

6963
// Mutable reference because Box does implement DerefMut.
@@ -75,7 +69,6 @@ fn main() {
7569
//~^ ERROR mismatched types
7670
None => {}
7771
//~^ ERROR mismatched types
78-
//~| HELP introduce a new binding instead
7972
}
8073

8174
// Difficult expression.
@@ -87,6 +80,5 @@ fn main() {
8780
//~^ ERROR mismatched types
8881
None => {}
8982
//~^ ERROR mismatched types
90-
//~| HELP introduce a new binding instead
9183
}
9284
}

0 commit comments

Comments
 (0)