Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions clippy_lints/src/methods/filter_map_bool_then.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::FILTER_MAP_BOOL_THEN;
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::SpanRangeExt;
use clippy_utils::source::{SpanRangeExt, snippet_with_context};
use clippy_utils::ty::is_copy;
use clippy_utils::{
CaptureKind, can_move_expr_to_closure, contains_return, is_from_proc_macro, is_trait_method, peel_blocks,
Expand Down Expand Up @@ -45,9 +45,11 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, arg: &
.filter(|adj| matches!(adj.kind, Adjust::Deref(_)))
.count()
&& let Some(param_snippet) = param.span.get_source_text(cx)
&& let Some(filter) = recv.span.get_source_text(cx)
&& let Some(map) = then_body.span.get_source_text(cx)
{
let mut applicability = Applicability::MachineApplicable;
let (filter, _) = snippet_with_context(cx, recv.span, expr.span.ctxt(), "..", &mut applicability);
let (map, _) = snippet_with_context(cx, then_body.span, expr.span.ctxt(), "..", &mut applicability);

span_lint_and_then(
cx,
FILTER_MAP_BOOL_THEN,
Expand All @@ -62,7 +64,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, arg: &
"filter(|&{param_snippet}| {derefs}{filter}).map(|{param_snippet}| {map})",
derefs = "*".repeat(needed_derefs)
),
Applicability::MachineApplicable,
applicability,
);
} else {
diag.help("consider using `filter` then `map` instead");
Expand Down
21 changes: 21 additions & 0 deletions tests/ui/filter_map_bool_then.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,24 @@ fn issue11503() {
let _: Vec<usize> = bools.iter().enumerate().filter(|&(i, b)| ****b).map(|(i, b)| i).collect();
//~^ filter_map_bool_then
}

fn issue15047() {
#[derive(Clone, Copy)]
enum MyEnum {
A,
B,
C,
}

macro_rules! foo {
($e:expr) => {
$e + 1
};
}

let x = 1;
let _ = [(MyEnum::A, "foo", 1i32)]
.iter()
.filter(|&(t, s, i)| matches!(t, MyEnum::A if s.starts_with("bar"))).map(|(t, s, i)| foo!(x));
//~^ filter_map_bool_then
}
21 changes: 21 additions & 0 deletions tests/ui/filter_map_bool_then.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,24 @@ fn issue11503() {
let _: Vec<usize> = bools.iter().enumerate().filter_map(|(i, b)| b.then(|| i)).collect();
//~^ filter_map_bool_then
}

fn issue15047() {
#[derive(Clone, Copy)]
enum MyEnum {
A,
B,
C,
}

macro_rules! foo {
($e:expr) => {
$e + 1
};
}

let x = 1;
let _ = [(MyEnum::A, "foo", 1i32)]
.iter()
.filter_map(|(t, s, i)| matches!(t, MyEnum::A if s.starts_with("bar")).then(|| foo!(x)));
//~^ filter_map_bool_then
}
8 changes: 7 additions & 1 deletion tests/ui/filter_map_bool_then.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,11 @@ error: usage of `bool::then` in `filter_map`
LL | let _: Vec<usize> = bools.iter().enumerate().filter_map(|(i, b)| b.then(|| i)).collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use `filter` then `map` instead: `filter(|&(i, b)| ****b).map(|(i, b)| i)`

error: aborting due to 10 previous errors
error: usage of `bool::then` in `filter_map`
--> tests/ui/filter_map_bool_then.rs:110:10
|
LL | .filter_map(|(t, s, i)| matches!(t, MyEnum::A if s.starts_with("bar")).then(|| foo!(x)));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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))`

error: aborting due to 11 previous errors