-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
match_like_matches_macro: strip refs in suggestion #6532
Conversation
r? @ebroto (rust-highfive has picked a reviewer for you, use r? to override) |
@matthiaskrgr thanks for PR! But this implementation is not accurate enough. It should also think about a using the variable after enum A { X, Y }
fn foo(x: A) {}
fn main() {
let z = A::X;
let _ = matches!(z, A::Y);
foo(z);
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alex-700 makes a good point, although I was a bit confused at the beginning by the example because A
is a variable binding and not lowercase. Since the scrutinee is not a reference anymore and the pattern is not a reference pattern, the binding moves the value. The same case slightly modified:
struct S(i32);
fn fun(_val: Option<S>) {}
fn main() {
let val = Some(S(42));
// let _res = match &val {
// Some(_a) => true,
// _ => false,
// };
let _res = matches!(val, Some(_a));
fun(val);
}
The current implementation would also fail if the match uses a reference pattern, so match ergonomics does not apply. Consider the following case:
struct S(i32);
fn fun(_val: Option<S>) {}
fn main() {
let val = Some(S(42));
// let _res = match &val {
// &Some(ref _a) => true,
// _ => false,
// };
let _res = matches!(val, &Some(ref _a));
fun(val);
}
This would yield a mismatched types
error
error[E0308]: mismatched types
--> src/main.rs:11:30
|
11 | let _res = matches!(val, &Some(ref _a));
| --- ^^^^^^^^^^^^^ expected enum `Option`, found reference
| |
| this expression has type `Option<S>`
|
= note: expected enum `Option<S>`
found reference `&_`
6a52586
to
a34aca2
Compare
fixes rust-lang#6503 changelog: match_like_matches_macro: strip refs in suggestion (rust-lang#6503)
I think I got all the options covered now..? |
r? @llogiq (I'm leaving the team, so I'm reassigning my PRs to other active members) |
From my point of view, this looks like a very solid improvement, also the tests seem to cover all cases. Thank you! @bors r+ |
📌 Commit 39f39d5 has been approved by |
☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test |
fixes #6503
changelog: match_like_matches_macro: strip refs in suggestion (#6503)