Skip to content

Commit

Permalink
Auto merge of rust-lang#11261 - y21:issue11260, r=blyxyas
Browse files Browse the repository at this point in the history
[`unnecessary_find_map`]: look for then_some

Closes rust-lang#11260

changelog: [`unnecessary_find_map`]: lint `.then_some()` in closure
  • Loading branch information
bors committed Jul 30, 2023
2 parents c0bdb3d + be6a103 commit 2ab1241
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
10 changes: 10 additions & 0 deletions clippy_lints/src/methods/unnecessary_filter_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ fn check_expression<'tcx>(cx: &LateContext<'tcx>, arg_id: hir::HirId, expr: &'tc
}
(true, true)
},
hir::ExprKind::MethodCall(segment, recv, [arg], _) => {
if segment.ident.name == sym!(then_some)
&& cx.typeck_results().expr_ty(recv).is_bool()
&& path_to_local_id(arg, arg_id)
{
(false, true)
} else {
(true, true)
}
},
hir::ExprKind::Block(block, _) => block
.expr
.as_ref()
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/unnecessary_filter_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,9 @@ mod comment_1052978898 {
})
}
}

fn issue11260() {
// #11260 is about unnecessary_find_map, but the fix also kind of applies to
// unnecessary_filter_map
let _x = std::iter::once(1).filter_map(|n| (n > 1).then_some(n));
}
8 changes: 7 additions & 1 deletion tests/ui/unnecessary_filter_map.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,11 @@ error: this `.filter_map` can be written more simply using `.map`
LL | let _ = (0..4).filter_map(|x| Some(x + 1));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

error: aborting due to 5 previous errors

6 changes: 6 additions & 0 deletions tests/ui/unnecessary_find_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ fn main() {
fn find_map_none_changes_item_type() -> Option<bool> {
"".chars().find_map(|_| None)
}

fn issue11260() {
let y = Some(1);
let _x = std::iter::once(1).find_map(|n| (n > 1).then_some(n));
let _x = std::iter::once(1).find_map(|n| (n > 1).then_some(y)); // different option, so can't be just `.find()`
}
8 changes: 7 additions & 1 deletion tests/ui/unnecessary_find_map.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,11 @@ error: this `.find_map` can be written more simply using `.map(..).next()`
LL | let _ = (0..4).find_map(|x| Some(x + 1));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 4 previous errors
error: this `.find_map` can be written more simply using `.find`
--> $DIR/unnecessary_find_map.rs:27:14
|
LL | let _x = std::iter::once(1).find_map(|n| (n > 1).then_some(n));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 5 previous errors

0 comments on commit 2ab1241

Please sign in to comment.