Skip to content

Commit f4242ee

Browse files
committed
do not trigger macro expansions
1 parent 029bde5 commit f4242ee

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

clippy_lints/src/loops/manual_flatten.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ pub(super) fn check<'tcx>(
3939
&& !is_local_used(cx, if_then, pat_hir_id)
4040
&& msrv.meets(cx, msrvs::ITER_FLATTEN)
4141
{
42+
if arg.span.from_expansion() || if_then.span.from_expansion() {
43+
return;
44+
}
4245
let if_let_type = if some_ctor { "Some" } else { "Ok" };
4346
// Prepare the error message
4447
let msg =

tests/ui/manual_flatten.fixed

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,32 @@ fn main() {
9191
println!("{}", n);
9292
}
9393

94+
macro_rules! inner {
95+
($id:ident / $new:pat => $action:expr) => {
96+
if let Some($new) = $id {
97+
$action;
98+
}
99+
};
100+
}
101+
102+
// Usage of `if let` expression with macro should not trigger lint
103+
for ab in [Some((1, 2)), Some((3, 4))] {
104+
inner!(ab / (c, d) => println!("{c}-{d}"));
105+
}
106+
107+
macro_rules! args {
108+
($($arg:expr),*) => {
109+
vec![$(Some($arg)),*]
110+
};
111+
}
112+
113+
// Usage of `if let` expression with macro should not trigger lint
114+
for n in args!(1, 2, 3) {
115+
if let Some(n) = n {
116+
println!("{:?}", n);
117+
}
118+
}
119+
94120
run_unformatted_tests();
95121
}
96122

tests/ui/manual_flatten.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,32 @@ fn main() {
123123
println!("{}", n);
124124
}
125125

126+
macro_rules! inner {
127+
($id:ident / $new:pat => $action:expr) => {
128+
if let Some($new) = $id {
129+
$action;
130+
}
131+
};
132+
}
133+
134+
// Usage of `if let` expression with macro should not trigger lint
135+
for ab in [Some((1, 2)), Some((3, 4))] {
136+
inner!(ab / (c, d) => println!("{c}-{d}"));
137+
}
138+
139+
macro_rules! args {
140+
($($arg:expr),*) => {
141+
vec![$(Some($arg)),*]
142+
};
143+
}
144+
145+
// Usage of `if let` expression with macro should not trigger lint
146+
for n in args!(1, 2, 3) {
147+
if let Some(n) = n {
148+
println!("{:?}", n);
149+
}
150+
}
151+
126152
run_unformatted_tests();
127153
}
128154

tests/ui/manual_flatten.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ LL + }
202202
|
203203

204204
error: unnecessary `if let` since only the `Some` variant of the iterator element is used
205-
--> tests/ui/manual_flatten.rs:132:5
205+
--> tests/ui/manual_flatten.rs:158:5
206206
|
207207
LL | / for n in vec![
208208
LL | |
@@ -213,7 +213,7 @@ LL | | }
213213
| |_____^
214214
|
215215
help: try `.flatten()` and remove the `if let` statement in the for loop
216-
--> tests/ui/manual_flatten.rs:139:9
216+
--> tests/ui/manual_flatten.rs:165:9
217217
|
218218
LL | / if let Some(n) = n {
219219
LL | | println!("{:?}", n);

0 commit comments

Comments
 (0)