Skip to content

Commit a982ab4

Browse files
committed
Auto merge of rust-lang#6532 - matthiaskrgr:mlmm, r=llogiq
match_like_matches_macro: strip refs in suggestion fixes rust-lang#6503 changelog: match_like_matches_macro: strip refs in suggestion (rust-lang#6503)
2 parents 7b50a4e + 39f39d5 commit a982ab4

File tree

4 files changed

+202
-2
lines changed

4 files changed

+202
-2
lines changed

clippy_lints/src/matches.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,14 @@ fn find_matches_sugg(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr
12251225
} else {
12261226
pat
12271227
};
1228+
1229+
// strip potential borrows (#6503), but only if the type is a reference
1230+
let mut ex_new = ex;
1231+
if let ExprKind::AddrOf(BorrowKind::Ref, .., ex_inner) = ex.kind {
1232+
if let ty::Ref(..) = cx.typeck_results().expr_ty(&ex_inner).kind() {
1233+
ex_new = ex_inner;
1234+
}
1235+
};
12281236
span_lint_and_sugg(
12291237
cx,
12301238
MATCH_LIKE_MATCHES_MACRO,
@@ -1234,7 +1242,7 @@ fn find_matches_sugg(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], expr
12341242
format!(
12351243
"{}matches!({}, {})",
12361244
if b0 { "" } else { "!" },
1237-
snippet_with_applicability(cx, ex.span, "..", &mut applicability),
1245+
snippet_with_applicability(cx, ex_new.span, "..", &mut applicability),
12381246
pat_and_guard,
12391247
),
12401248
applicability,

tests/ui/match_expr_like_matches_macro.fixed

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,51 @@ fn main() {
9999
_ => false,
100100
};
101101
}
102+
103+
{
104+
// should print "z" in suggestion (#6503)
105+
let z = &Some(3);
106+
let _z = matches!(z, Some(3));
107+
}
108+
109+
{
110+
// this could also print "z" in suggestion..?
111+
let z = Some(3);
112+
let _z = matches!(&z, Some(3));
113+
}
114+
115+
{
116+
enum AnEnum {
117+
X,
118+
Y,
119+
}
120+
121+
fn foo(_x: AnEnum) {}
122+
123+
fn main() {
124+
let z = AnEnum::X;
125+
// we can't remove the reference here!
126+
let _ = matches!(&z, AnEnum::X);
127+
foo(z);
128+
}
129+
}
130+
131+
{
132+
struct S(i32);
133+
134+
fn fun(_val: Option<S>) {}
135+
let val = Some(S(42));
136+
// we need the reference here because later val is consumed by fun()
137+
let _res = matches!(&val, &Some(ref _a));
138+
fun(val);
139+
}
140+
141+
{
142+
struct S(i32);
143+
144+
fn fun(_val: Option<S>) {}
145+
let val = Some(S(42));
146+
let _res = matches!(&val, &Some(ref _a));
147+
fun(val);
148+
}
102149
}

tests/ui/match_expr_like_matches_macro.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,66 @@ fn main() {
119119
_ => false,
120120
};
121121
}
122+
123+
{
124+
// should print "z" in suggestion (#6503)
125+
let z = &Some(3);
126+
let _z = match &z {
127+
Some(3) => true,
128+
_ => false,
129+
};
130+
}
131+
132+
{
133+
// this could also print "z" in suggestion..?
134+
let z = Some(3);
135+
let _z = match &z {
136+
Some(3) => true,
137+
_ => false,
138+
};
139+
}
140+
141+
{
142+
enum AnEnum {
143+
X,
144+
Y,
145+
}
146+
147+
fn foo(_x: AnEnum) {}
148+
149+
fn main() {
150+
let z = AnEnum::X;
151+
// we can't remove the reference here!
152+
let _ = match &z {
153+
AnEnum::X => true,
154+
_ => false,
155+
};
156+
foo(z);
157+
}
158+
}
159+
160+
{
161+
struct S(i32);
162+
163+
fn fun(_val: Option<S>) {}
164+
let val = Some(S(42));
165+
// we need the reference here because later val is consumed by fun()
166+
let _res = match &val {
167+
&Some(ref _a) => true,
168+
_ => false,
169+
};
170+
fun(val);
171+
}
172+
173+
{
174+
struct S(i32);
175+
176+
fn fun(_val: Option<S>) {}
177+
let val = Some(S(42));
178+
let _res = match &val {
179+
&Some(ref _a) => true,
180+
_ => false,
181+
};
182+
fun(val);
183+
}
122184
}

tests/ui/match_expr_like_matches_macro.stderr

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,88 @@ LL | | _ => true,
7070
LL | | };
7171
| |_________^ help: try this: `!matches!(x, E::B(_) | E::C)`
7272

73-
error: aborting due to 7 previous errors
73+
error: match expression looks like `matches!` macro
74+
--> $DIR/match_expr_like_matches_macro.rs:126:18
75+
|
76+
LL | let _z = match &z {
77+
| __________________^
78+
LL | | Some(3) => true,
79+
LL | | _ => false,
80+
LL | | };
81+
| |_________^ help: try this: `matches!(z, Some(3))`
82+
83+
error: match expression looks like `matches!` macro
84+
--> $DIR/match_expr_like_matches_macro.rs:135:18
85+
|
86+
LL | let _z = match &z {
87+
| __________________^
88+
LL | | Some(3) => true,
89+
LL | | _ => false,
90+
LL | | };
91+
| |_________^ help: try this: `matches!(&z, Some(3))`
92+
93+
error: match expression looks like `matches!` macro
94+
--> $DIR/match_expr_like_matches_macro.rs:152:21
95+
|
96+
LL | let _ = match &z {
97+
| _____________________^
98+
LL | | AnEnum::X => true,
99+
LL | | _ => false,
100+
LL | | };
101+
| |_____________^ help: try this: `matches!(&z, AnEnum::X)`
102+
103+
error: match expression looks like `matches!` macro
104+
--> $DIR/match_expr_like_matches_macro.rs:166:20
105+
|
106+
LL | let _res = match &val {
107+
| ____________________^
108+
LL | | &Some(ref _a) => true,
109+
LL | | _ => false,
110+
LL | | };
111+
| |_________^ help: try this: `matches!(&val, &Some(ref _a))`
112+
113+
error: you don't need to add `&` to both the expression and the patterns
114+
--> $DIR/match_expr_like_matches_macro.rs:166:20
115+
|
116+
LL | let _res = match &val {
117+
| ____________________^
118+
LL | | &Some(ref _a) => true,
119+
LL | | _ => false,
120+
LL | | };
121+
| |_________^
122+
|
123+
= note: `-D clippy::match-ref-pats` implied by `-D warnings`
124+
help: try
125+
|
126+
LL | let _res = match val {
127+
LL | Some(ref _a) => true,
128+
|
129+
130+
error: match expression looks like `matches!` macro
131+
--> $DIR/match_expr_like_matches_macro.rs:178:20
132+
|
133+
LL | let _res = match &val {
134+
| ____________________^
135+
LL | | &Some(ref _a) => true,
136+
LL | | _ => false,
137+
LL | | };
138+
| |_________^ help: try this: `matches!(&val, &Some(ref _a))`
139+
140+
error: you don't need to add `&` to both the expression and the patterns
141+
--> $DIR/match_expr_like_matches_macro.rs:178:20
142+
|
143+
LL | let _res = match &val {
144+
| ____________________^
145+
LL | | &Some(ref _a) => true,
146+
LL | | _ => false,
147+
LL | | };
148+
| |_________^
149+
|
150+
help: try
151+
|
152+
LL | let _res = match val {
153+
LL | Some(ref _a) => true,
154+
|
155+
156+
error: aborting due to 14 previous errors
74157

0 commit comments

Comments
 (0)