Skip to content

Commit 2ab1241

Browse files
committed
Auto merge of #11261 - y21:issue11260, r=blyxyas
[`unnecessary_find_map`]: look for then_some Closes #11260 changelog: [`unnecessary_find_map`]: lint `.then_some()` in closure
2 parents c0bdb3d + be6a103 commit 2ab1241

File tree

5 files changed

+36
-2
lines changed

5 files changed

+36
-2
lines changed

clippy_lints/src/methods/unnecessary_filter_map.rs

+10
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_filter_map.rs

+6
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,9 @@ mod comment_1052978898 {
148148
})
149149
}
150150
}
151+
152+
fn issue11260() {
153+
// #11260 is about unnecessary_find_map, but the fix also kind of applies to
154+
// unnecessary_filter_map
155+
let _x = std::iter::once(1).filter_map(|n| (n > 1).then_some(n));
156+
}

tests/ui/unnecessary_filter_map.stderr

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

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

tests/ui/unnecessary_find_map.rs

+6
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,9 @@ fn main() {
2121
fn find_map_none_changes_item_type() -> Option<bool> {
2222
"".chars().find_map(|_| None)
2323
}
24+
25+
fn issue11260() {
26+
let y = Some(1);
27+
let _x = std::iter::once(1).find_map(|n| (n > 1).then_some(n));
28+
let _x = std::iter::once(1).find_map(|n| (n > 1).then_some(y)); // different option, so can't be just `.find()`
29+
}

tests/ui/unnecessary_find_map.stderr

+7-1
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:27: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)