Skip to content

Commit 924f153

Browse files
Fix match_like_matches_macro wrongly unmangled macros (#16018)
Closes #16015 changelog: [`match_like_matches_macro`] fix wrongly unmangled macros
2 parents 893677f + dc0bf26 commit 924f153

File tree

4 files changed

+100
-27
lines changed

4 files changed

+100
-27
lines changed

clippy_lints/src/matches/match_like_matches.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use super::REDUNDANT_PATTERN_MATCHING;
44
use clippy_utils::diagnostics::span_lint_and_then;
55
use clippy_utils::higher::has_let_expr;
6-
use clippy_utils::source::snippet_with_applicability;
6+
use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
77
use clippy_utils::{is_lint_allowed, is_wild, span_contains_comment};
88
use rustc_ast::LitKind;
99
use rustc_errors::Applicability;
@@ -44,6 +44,8 @@ pub(crate) fn check_if_let<'tcx>(
4444
{
4545
ex_new = ex_inner;
4646
}
47+
48+
let (snippet, _) = snippet_with_context(cx, ex_new.span, expr.span.ctxt(), "..", &mut applicability);
4749
span_lint_and_then(
4850
cx,
4951
MATCH_LIKE_MATCHES_MACRO,
@@ -53,11 +55,7 @@ pub(crate) fn check_if_let<'tcx>(
5355
diag.span_suggestion_verbose(
5456
expr.span,
5557
"use `matches!` directly",
56-
format!(
57-
"{}matches!({}, {pat})",
58-
if b0 { "" } else { "!" },
59-
snippet_with_applicability(cx, ex_new.span, "..", &mut applicability),
60-
),
58+
format!("{}matches!({snippet}, {pat})", if b0 { "" } else { "!" }),
6159
applicability,
6260
);
6361
},
@@ -178,6 +176,8 @@ pub(super) fn check_match<'tcx>(
178176
{
179177
ex_new = ex_inner;
180178
}
179+
180+
let (snippet, _) = snippet_with_context(cx, ex_new.span, e.span.ctxt(), "..", &mut applicability);
181181
span_lint_and_then(
182182
cx,
183183
MATCH_LIKE_MATCHES_MACRO,
@@ -187,11 +187,7 @@ pub(super) fn check_match<'tcx>(
187187
diag.span_suggestion_verbose(
188188
e.span,
189189
"use `matches!` directly",
190-
format!(
191-
"{}matches!({}, {pat_and_guard})",
192-
if b0 { "" } else { "!" },
193-
snippet_with_applicability(cx, ex_new.span, "..", &mut applicability),
194-
),
190+
format!("{}matches!({snippet}, {pat_and_guard})", if b0 { "" } else { "!" },),
195191
applicability,
196192
);
197193
},

tests/ui/match_like_matches_macro.fixed

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![warn(clippy::match_like_matches_macro)]
22
#![allow(
33
unreachable_patterns,
4+
irrefutable_let_patterns,
45
clippy::equatable_if_let,
56
clippy::needless_borrowed_reference,
67
clippy::redundant_guards
@@ -230,3 +231,24 @@ fn issue15841(opt: Option<Option<Option<i32>>>, value: i32) {
230231
let _ = matches!(opt, Some(first) if (if let Some(second) = first { true } else { todo!() }));
231232
//~^^^^ match_like_matches_macro
232233
}
234+
235+
fn issue16015<T: 'static, U: 'static>() -> bool {
236+
use std::any::{TypeId, type_name};
237+
pub struct GetTypeId<T>(T);
238+
239+
impl<T: 'static> GetTypeId<T> {
240+
pub const VALUE: TypeId = TypeId::of::<T>();
241+
}
242+
243+
macro_rules! typeid {
244+
($t:ty) => {
245+
GetTypeId::<$t>::VALUE
246+
};
247+
}
248+
249+
matches!(typeid!(T), _);
250+
//~^^^^ match_like_matches_macro
251+
252+
matches!(typeid!(U), _)
253+
//~^ match_like_matches_macro
254+
}

tests/ui/match_like_matches_macro.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![warn(clippy::match_like_matches_macro)]
22
#![allow(
33
unreachable_patterns,
4+
irrefutable_let_patterns,
45
clippy::equatable_if_let,
56
clippy::needless_borrowed_reference,
67
clippy::redundant_guards
@@ -277,3 +278,27 @@ fn issue15841(opt: Option<Option<Option<i32>>>, value: i32) {
277278
};
278279
//~^^^^ match_like_matches_macro
279280
}
281+
282+
fn issue16015<T: 'static, U: 'static>() -> bool {
283+
use std::any::{TypeId, type_name};
284+
pub struct GetTypeId<T>(T);
285+
286+
impl<T: 'static> GetTypeId<T> {
287+
pub const VALUE: TypeId = TypeId::of::<T>();
288+
}
289+
290+
macro_rules! typeid {
291+
($t:ty) => {
292+
GetTypeId::<$t>::VALUE
293+
};
294+
}
295+
296+
match typeid!(T) {
297+
_ => true,
298+
_ => false,
299+
};
300+
//~^^^^ match_like_matches_macro
301+
302+
if let _ = typeid!(U) { true } else { false }
303+
//~^ match_like_matches_macro
304+
}

tests/ui/match_like_matches_macro.stderr

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: match expression looks like `matches!` macro
2-
--> tests/ui/match_like_matches_macro.rs:13:14
2+
--> tests/ui/match_like_matches_macro.rs:14:14
33
|
44
LL | let _y = match x {
55
| ______________^
@@ -20,7 +20,7 @@ LL + let _y = matches!(x, Some(0));
2020
|
2121

2222
error: redundant pattern matching, consider using `is_some()`
23-
--> tests/ui/match_like_matches_macro.rs:20:14
23+
--> tests/ui/match_like_matches_macro.rs:21:14
2424
|
2525
LL | let _w = match x {
2626
| ______________^
@@ -33,7 +33,7 @@ LL | | };
3333
= help: to override `-D warnings` add `#[allow(clippy::redundant_pattern_matching)]`
3434

3535
error: redundant pattern matching, consider using `is_none()`
36-
--> tests/ui/match_like_matches_macro.rs:27:14
36+
--> tests/ui/match_like_matches_macro.rs:28:14
3737
|
3838
LL | let _z = match x {
3939
| ______________^
@@ -43,7 +43,7 @@ LL | | };
4343
| |_____^ help: try: `x.is_none()`
4444

4545
error: match expression looks like `matches!` macro
46-
--> tests/ui/match_like_matches_macro.rs:34:15
46+
--> tests/ui/match_like_matches_macro.rs:35:15
4747
|
4848
LL | let _zz = match x {
4949
| _______________^
@@ -62,7 +62,7 @@ LL + let _zz = !matches!(x, Some(r) if r == 0);
6262
|
6363

6464
error: `if let .. else` expression looks like `matches!` macro
65-
--> tests/ui/match_like_matches_macro.rs:41:16
65+
--> tests/ui/match_like_matches_macro.rs:42:16
6666
|
6767
LL | let _zzz = if let Some(5) = x { true } else { false };
6868
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -74,7 +74,7 @@ LL + let _zzz = matches!(x, Some(5));
7474
|
7575

7676
error: match expression looks like `matches!` macro
77-
--> tests/ui/match_like_matches_macro.rs:66:20
77+
--> tests/ui/match_like_matches_macro.rs:67:20
7878
|
7979
LL | let _ans = match x {
8080
| ____________________^
@@ -95,7 +95,7 @@ LL + let _ans = matches!(x, E::A(_) | E::B(_));
9595
|
9696

9797
error: match expression looks like `matches!` macro
98-
--> tests/ui/match_like_matches_macro.rs:77:20
98+
--> tests/ui/match_like_matches_macro.rs:78:20
9999
|
100100
LL | let _ans = match x {
101101
| ____________________^
@@ -119,7 +119,7 @@ LL + let _ans = matches!(x, E::A(_) | E::B(_));
119119
|
120120

121121
error: match expression looks like `matches!` macro
122-
--> tests/ui/match_like_matches_macro.rs:88:20
122+
--> tests/ui/match_like_matches_macro.rs:89:20
123123
|
124124
LL | let _ans = match x {
125125
| ____________________^
@@ -140,7 +140,7 @@ LL + let _ans = !matches!(x, E::B(_) | E::C);
140140
|
141141

142142
error: match expression looks like `matches!` macro
143-
--> tests/ui/match_like_matches_macro.rs:149:18
143+
--> tests/ui/match_like_matches_macro.rs:150:18
144144
|
145145
LL | let _z = match &z {
146146
| __________________^
@@ -159,7 +159,7 @@ LL + let _z = matches!(z, Some(3));
159159
|
160160

161161
error: match expression looks like `matches!` macro
162-
--> tests/ui/match_like_matches_macro.rs:159:18
162+
--> tests/ui/match_like_matches_macro.rs:160:18
163163
|
164164
LL | let _z = match &z {
165165
| __________________^
@@ -178,7 +178,7 @@ LL + let _z = matches!(&z, Some(3));
178178
|
179179

180180
error: match expression looks like `matches!` macro
181-
--> tests/ui/match_like_matches_macro.rs:177:21
181+
--> tests/ui/match_like_matches_macro.rs:178:21
182182
|
183183
LL | let _ = match &z {
184184
| _____________________^
@@ -197,7 +197,7 @@ LL + let _ = matches!(&z, AnEnum::X);
197197
|
198198

199199
error: match expression looks like `matches!` macro
200-
--> tests/ui/match_like_matches_macro.rs:192:20
200+
--> tests/ui/match_like_matches_macro.rs:193:20
201201
|
202202
LL | let _res = match &val {
203203
| ____________________^
@@ -216,7 +216,7 @@ LL + let _res = matches!(&val, &Some(ref _a));
216216
|
217217

218218
error: match expression looks like `matches!` macro
219-
--> tests/ui/match_like_matches_macro.rs:205:20
219+
--> tests/ui/match_like_matches_macro.rs:206:20
220220
|
221221
LL | let _res = match &val {
222222
| ____________________^
@@ -235,7 +235,7 @@ LL + let _res = matches!(&val, &Some(ref _a));
235235
|
236236

237237
error: match expression looks like `matches!` macro
238-
--> tests/ui/match_like_matches_macro.rs:264:14
238+
--> tests/ui/match_like_matches_macro.rs:265:14
239239
|
240240
LL | let _y = match Some(5) {
241241
| ______________^
@@ -254,7 +254,7 @@ LL + let _y = matches!(Some(5), Some(0));
254254
|
255255

256256
error: match expression looks like `matches!` macro
257-
--> tests/ui/match_like_matches_macro.rs:274:13
257+
--> tests/ui/match_like_matches_macro.rs:275:13
258258
|
259259
LL | let _ = match opt {
260260
| _____________^
@@ -272,5 +272,35 @@ LL - };
272272
LL + let _ = matches!(opt, Some(first) if (if let Some(second) = first { true } else { todo!() }));
273273
|
274274

275-
error: aborting due to 15 previous errors
275+
error: match expression looks like `matches!` macro
276+
--> tests/ui/match_like_matches_macro.rs:296:5
277+
|
278+
LL | / match typeid!(T) {
279+
LL | | _ => true,
280+
LL | | _ => false,
281+
LL | | };
282+
| |_____^
283+
|
284+
help: use `matches!` directly
285+
|
286+
LL - match typeid!(T) {
287+
LL - _ => true,
288+
LL - _ => false,
289+
LL - };
290+
LL + matches!(typeid!(T), _);
291+
|
292+
293+
error: `if let .. else` expression looks like `matches!` macro
294+
--> tests/ui/match_like_matches_macro.rs:302:5
295+
|
296+
LL | if let _ = typeid!(U) { true } else { false }
297+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
298+
|
299+
help: use `matches!` directly
300+
|
301+
LL - if let _ = typeid!(U) { true } else { false }
302+
LL + matches!(typeid!(U), _)
303+
|
304+
305+
error: aborting due to 17 previous errors
276306

0 commit comments

Comments
 (0)