Skip to content

Commit 1f735f6

Browse files
committed
fix: filter_map_bool_then wrongly showed macro definition in suggestions
1 parent 4ef7529 commit 1f735f6

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

clippy_lints/src/methods/filter_map_bool_then.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, arg: &
4545
.filter(|adj| matches!(adj.kind, Adjust::Deref(_)))
4646
.count()
4747
&& let Some(param_snippet) = param.span.get_source_text(cx)
48-
&& let Some(filter) = recv.span.get_source_text(cx)
49-
&& let Some(map) = then_body.span.get_source_text(cx)
48+
&& let Some(filter) = recv.span.source_callsite().get_source_text(cx)
49+
&& let Some(map) = then_body.span.source_callsite().get_source_text(cx)
5050
{
5151
span_lint_and_then(
5252
cx,

tests/ui/filter_map_bool_then.fixed

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,24 @@ fn issue11503() {
8989
let _: Vec<usize> = bools.iter().enumerate().filter(|&(i, b)| ****b).map(|(i, b)| i).collect();
9090
//~^ filter_map_bool_then
9191
}
92+
93+
fn issue15047() {
94+
#[derive(Clone, Copy)]
95+
enum MyEnum {
96+
A,
97+
B,
98+
C,
99+
}
100+
101+
macro_rules! foo {
102+
($e:expr) => {
103+
$e + 1
104+
};
105+
}
106+
107+
let x = 1;
108+
let _ = [(MyEnum::A, "foo", 1i32)]
109+
.iter()
110+
.filter(|&(t, s, i)| matches!(t, MyEnum::A if s.starts_with("bar"))).map(|(t, s, i)| foo!(x));
111+
//~^ filter_map_bool_then
112+
}

tests/ui/filter_map_bool_then.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,24 @@ fn issue11503() {
8989
let _: Vec<usize> = bools.iter().enumerate().filter_map(|(i, b)| b.then(|| i)).collect();
9090
//~^ filter_map_bool_then
9191
}
92+
93+
fn issue15047() {
94+
#[derive(Clone, Copy)]
95+
enum MyEnum {
96+
A,
97+
B,
98+
C,
99+
}
100+
101+
macro_rules! foo {
102+
($e:expr) => {
103+
$e + 1
104+
};
105+
}
106+
107+
let x = 1;
108+
let _ = [(MyEnum::A, "foo", 1i32)]
109+
.iter()
110+
.filter_map(|(t, s, i)| matches!(t, MyEnum::A if s.starts_with("bar")).then(|| foo!(x)));
111+
//~^ filter_map_bool_then
112+
}

tests/ui/filter_map_bool_then.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,11 @@ error: usage of `bool::then` in `filter_map`
6161
LL | let _: Vec<usize> = bools.iter().enumerate().filter_map(|(i, b)| b.then(|| i)).collect();
6262
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&(i, b)| ****b).map(|(i, b)| i)`
6363

64-
error: aborting due to 10 previous errors
64+
error: usage of `bool::then` in `filter_map`
65+
--> tests/ui/filter_map_bool_then.rs:110:10
66+
|
67+
LL | .filter_map(|(t, s, i)| matches!(t, MyEnum::A if s.starts_with("bar")).then(|| foo!(x)));
68+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&(t, s, i)| matches!(t, MyEnum::A if s.starts_with("bar"))).map(|(t, s, i)| foo!(x))`
69+
70+
error: aborting due to 11 previous errors
6571

0 commit comments

Comments
 (0)