Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 008746c

Browse files
committed
[unnecessary_find_map]: look for then_some
1 parent 2973096 commit 008746c

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

clippy_lints/src/methods/unnecessary_filter_map.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ fn check_expression<'tcx>(cx: &LateContext<'tcx>, arg_id: hir::HirId, expr: &'tc
7777
}
7878
(true, true)
7979
},
80+
hir::ExprKind::MethodCall(segment, recv, [arg], _) => {
81+
if segment.ident.name == sym!(then_some)
82+
&& cx.typeck_results().expr_ty(recv).is_bool()
83+
&& path_to_local_id(arg, arg_id)
84+
{
85+
(false, true)
86+
} else {
87+
(true, true)
88+
}
89+
},
8090
hir::ExprKind::Block(block, _) => block
8191
.expr
8292
.as_ref()

tests/ui/unnecessary_find_map.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ fn main() {
2121
fn find_map_none_changes_item_type() -> Option<bool> {
2222
"".chars().find_map(|_| None)
2323
}
24+
25+
fn issue11260() {
26+
let _x = std::iter::once(1).find_map(|n| (n > 1).then_some(n));
27+
}

tests/ui/unnecessary_find_map.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,11 @@ error: this `.find_map` can be written more simply using `.map(..).next()`
3434
LL | let _ = (0..4).find_map(|x| Some(x + 1));
3535
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3636

37-
error: aborting due to 4 previous errors
37+
error: this `.find_map` can be written more simply using `.find`
38+
--> $DIR/unnecessary_find_map.rs:26:14
39+
|
40+
LL | let _x = std::iter::once(1).find_map(|n| (n > 1).then_some(n));
41+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
42+
43+
error: aborting due to 5 previous errors
3844

0 commit comments

Comments
 (0)