Skip to content

Commit 038295a

Browse files
committed
fix: filter_map_bool_then wrongly showed macro definition in suggestions
1 parent e1be062 commit 038295a

File tree

4 files changed

+55
-5
lines changed

4 files changed

+55
-5
lines changed

clippy_lints/src/methods/filter_map_bool_then.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::FILTER_MAP_BOOL_THEN;
22
use clippy_utils::diagnostics::span_lint_and_then;
3-
use clippy_utils::source::SpanRangeExt;
3+
use clippy_utils::source::{SpanRangeExt, snippet_with_context};
44
use clippy_utils::ty::is_copy;
55
use clippy_utils::{
66
CaptureKind, can_move_expr_to_closure, contains_return, is_from_proc_macro, is_trait_method, peel_blocks,
@@ -45,9 +45,11 @@ 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)
5048
{
49+
let mut applicability = Applicability::MachineApplicable;
50+
let (filter, _) = snippet_with_context(cx, recv.span, expr.span.ctxt(), "..", &mut applicability);
51+
let (map, _) = snippet_with_context(cx, then_body.span, expr.span.ctxt(), "..", &mut applicability);
52+
5153
span_lint_and_then(
5254
cx,
5355
FILTER_MAP_BOOL_THEN,
@@ -62,7 +64,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, arg: &
6264
"filter(|&{param_snippet}| {derefs}{filter}).map(|{param_snippet}| {map})",
6365
derefs = "*".repeat(needed_derefs)
6466
),
65-
Applicability::MachineApplicable,
67+
applicability,
6668
);
6769
} else {
6870
diag.help("consider using `filter` then `map` instead");

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)