Skip to content

Commit fff953f

Browse files
committed
Add test ensuring correct suggestion for pattern dereference
``` error[E0308]: mismatched types --> $DIR/suggest-dereferencing-through-Deref.rs:13:16 | LL | if let A::V1(v) = b { | ^^^^^^^^ - this expression has type `&Box<A>` | | | expected `Box<A>`, found `A` | = note: expected struct `Box<A>` found enum `A` help: consider dereferencing to access the inner value using the Deref trait | LL | if let A::V1(v) = &**b { | ~~~~ error[E0308]: mismatched types --> $DIR/suggest-dereferencing-through-Deref.rs:23:16 | LL | if let A::V1(v) = *b { | ^^^^^^^^ -- this expression has type `Box<A>` | | | expected `Box<A>`, found `A` | = note: expected struct `Box<A>` found enum `A` help: consider dereferencing the boxed value | LL | if let A::V1(v) = **b { | ~~~ ``` Fix rust-lang#68628
1 parent 3c1e750 commit fff953f

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//@ run-rustfix
2+
#![allow(dead_code)]
3+
4+
enum A {
5+
V1(u8),
6+
V2(u32),
7+
}
8+
9+
fn foo() {
10+
let a = Some(Box::new(A::V1(1u8)));
11+
12+
if let Some(b) = a.as_ref() {
13+
if let A::V1(v) = &**b { //~ ERROR mismatched types
14+
println!("{:?}", v);
15+
}
16+
}
17+
}
18+
19+
fn bar() {
20+
let a = Some(Box::new(A::V1(1u8)));
21+
22+
if let Some(b) = a.as_ref() {
23+
if let A::V1(v) = **b { //~ ERROR mismatched types
24+
println!("{:?}", v);
25+
}
26+
}
27+
}
28+
29+
fn main() {}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//@ run-rustfix
2+
#![allow(dead_code)]
3+
4+
enum A {
5+
V1(u8),
6+
V2(u32),
7+
}
8+
9+
fn foo() {
10+
let a = Some(Box::new(A::V1(1u8)));
11+
12+
if let Some(b) = a.as_ref() {
13+
if let A::V1(v) = b { //~ ERROR mismatched types
14+
println!("{:?}", v);
15+
}
16+
}
17+
}
18+
19+
fn bar() {
20+
let a = Some(Box::new(A::V1(1u8)));
21+
22+
if let Some(b) = a.as_ref() {
23+
if let A::V1(v) = *b { //~ ERROR mismatched types
24+
println!("{:?}", v);
25+
}
26+
}
27+
}
28+
29+
fn main() {}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/suggest-dereferencing-through-Deref.rs:13:16
3+
|
4+
LL | if let A::V1(v) = b {
5+
| ^^^^^^^^ - this expression has type `&Box<A>`
6+
| |
7+
| expected `Box<A>`, found `A`
8+
|
9+
= note: expected struct `Box<A>`
10+
found enum `A`
11+
help: consider dereferencing to access the inner value using the Deref trait
12+
|
13+
LL | if let A::V1(v) = &**b {
14+
| ~~~~
15+
16+
error[E0308]: mismatched types
17+
--> $DIR/suggest-dereferencing-through-Deref.rs:23:16
18+
|
19+
LL | if let A::V1(v) = *b {
20+
| ^^^^^^^^ -- this expression has type `Box<A>`
21+
| |
22+
| expected `Box<A>`, found `A`
23+
|
24+
= note: expected struct `Box<A>`
25+
found enum `A`
26+
help: consider dereferencing the boxed value
27+
|
28+
LL | if let A::V1(v) = **b {
29+
| ~~~
30+
31+
error: aborting due to 2 previous errors
32+
33+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)