about summary refs log tree commit diff
diff options
context:
space:
mode:
authory21 <30553356+y21@users.noreply.github.com>2023-07-30 13:41:58 +0200
committery21 <30553356+y21@users.noreply.github.com>2023-07-30 13:51:35 +0200
commit008746cae4976baab645a6ca2d49f8392ded68ae (patch)
treeb1612ba1cee8a333377521627c30ccda75569b83
parent29730969b196781e92ca8c8b2d0a8bf9bb105e22 (diff)
downloadrust-008746cae4976baab645a6ca2d49f8392ded68ae.tar.gz
rust-008746cae4976baab645a6ca2d49f8392ded68ae.zip
[`unnecessary_find_map`]: look for then_some
-rw-r--r--clippy_lints/src/methods/unnecessary_filter_map.rs10
-rw-r--r--tests/ui/unnecessary_find_map.rs4
-rw-r--r--tests/ui/unnecessary_find_map.stderr8
3 files changed, 21 insertions, 1 deletions
diff --git a/clippy_lints/src/methods/unnecessary_filter_map.rs b/clippy_lints/src/methods/unnecessary_filter_map.rs
index cc64a2e7948..fabf3fa0c0c 100644
--- a/clippy_lints/src/methods/unnecessary_filter_map.rs
+++ b/clippy_lints/src/methods/unnecessary_filter_map.rs
@@ -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()
diff --git a/tests/ui/unnecessary_find_map.rs b/tests/ui/unnecessary_find_map.rs
index a52390861b4..3be41b775aa 100644
--- a/tests/ui/unnecessary_find_map.rs
+++ b/tests/ui/unnecessary_find_map.rs
@@ -21,3 +21,7 @@ fn main() {
 fn find_map_none_changes_item_type() -> Option<bool> {
     "".chars().find_map(|_| None)
 }
+
+fn issue11260() {
+    let _x = std::iter::once(1).find_map(|n| (n > 1).then_some(n));
+}
diff --git a/tests/ui/unnecessary_find_map.stderr b/tests/ui/unnecessary_find_map.stderr
index fb33c122fe3..3d6ef4f44fd 100644
--- a/tests/ui/unnecessary_find_map.stderr
+++ b/tests/ui/unnecessary_find_map.stderr
@@ -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:26:14
+   |
+LL |     let _x = std::iter::once(1).find_map(|n| (n > 1).then_some(n));
+   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: aborting due to 5 previous errors