Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test ensuring correct suggestion for pattern dereference #134859

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions tests/ui/pattern/suggest-dereferencing-through-Deref.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//@ run-rustfix
#![allow(dead_code)]

enum A {
V1(u8),
V2(u32),
}

fn foo() {
let a = Some(Box::new(A::V1(1u8)));

if let Some(b) = a.as_ref() {
if let A::V1(v) = &**b { //~ ERROR mismatched types
println!("{:?}", v);
}
}
}

fn bar() {
let a = Some(Box::new(A::V1(1u8)));

if let Some(b) = a.as_ref() {
if let A::V1(v) = **b { //~ ERROR mismatched types
println!("{:?}", v);
}
}
}

fn main() {}
29 changes: 29 additions & 0 deletions tests/ui/pattern/suggest-dereferencing-through-Deref.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//@ run-rustfix
#![allow(dead_code)]

enum A {
V1(u8),
V2(u32),
}

fn foo() {
let a = Some(Box::new(A::V1(1u8)));

Check failure on line 11 in tests/ui/pattern/suggest-dereferencing-through-Deref.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

trailing whitespace
if let Some(b) = a.as_ref() {
if let A::V1(v) = b { //~ ERROR mismatched types
println!("{:?}", v);
}
}
}

fn bar() {
let a = Some(Box::new(A::V1(1u8)));

Check failure on line 21 in tests/ui/pattern/suggest-dereferencing-through-Deref.rs

View workflow job for this annotation

GitHub Actions / PR - mingw-check-tidy

trailing whitespace
if let Some(b) = a.as_ref() {
if let A::V1(v) = *b { //~ ERROR mismatched types
println!("{:?}", v);
}
}
}

fn main() {}
33 changes: 33 additions & 0 deletions tests/ui/pattern/suggest-dereferencing-through-Deref.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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 {
| ~~~

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.
Loading