@@ -1309,7 +1309,7 @@ declare_clippy_lint! {
1309
1309
1310
1310
declare_clippy_lint ! {
1311
1311
/// ### What it does
1312
- /// Checks for `filter_map` calls which could be replaced by `filter` or `map`.
1312
+ /// Checks for `filter_map` calls that could be replaced by `filter` or `map`.
1313
1313
/// More specifically it checks if the closure provided is only performing one of the
1314
1314
/// filter or map operations and suggests the appropriate option.
1315
1315
///
@@ -1337,6 +1337,36 @@ declare_clippy_lint! {
1337
1337
"using `filter_map` when a more succinct alternative exists"
1338
1338
}
1339
1339
1340
+ declare_clippy_lint ! {
1341
+ /// ### What it does
1342
+ /// Checks for `find_map` calls that could be replaced by `find` or `map`. More
1343
+ /// specifically it checks if the closure provided is only performing one of the
1344
+ /// find or map operations and suggests the appropriate option.
1345
+ ///
1346
+ /// ### Why is this bad?
1347
+ /// Complexity. The intent is also clearer if only a single
1348
+ /// operation is being performed.
1349
+ ///
1350
+ /// ### Example
1351
+ /// ```rust
1352
+ /// let _ = (0..3).find_map(|x| if x > 2 { Some(x) } else { None });
1353
+ ///
1354
+ /// // As there is no transformation of the argument this could be written as:
1355
+ /// let _ = (0..3).find(|&x| x > 2);
1356
+ /// ```
1357
+ ///
1358
+ /// ```rust
1359
+ /// let _ = (0..4).find_map(|x| Some(x + 1));
1360
+ ///
1361
+ /// // As there is no conditional check on the argument this could be written as:
1362
+ /// let _ = (0..4).map(|x| x + 1).next();
1363
+ /// ```
1364
+ #[ clippy:: version = "1.61.0" ]
1365
+ pub UNNECESSARY_FIND_MAP ,
1366
+ complexity,
1367
+ "using `find_map` when a more succinct alternative exists"
1368
+ }
1369
+
1340
1370
declare_clippy_lint ! {
1341
1371
/// ### What it does
1342
1372
/// Checks for `into_iter` calls on references which should be replaced by `iter`
@@ -2020,6 +2050,7 @@ impl_lint_pass!(Methods => [
2020
2050
USELESS_ASREF ,
2021
2051
UNNECESSARY_FOLD ,
2022
2052
UNNECESSARY_FILTER_MAP ,
2053
+ UNNECESSARY_FIND_MAP ,
2023
2054
INTO_ITER_ON_REF ,
2024
2055
SUSPICIOUS_MAP ,
2025
2056
UNINIT_ASSUMED_INIT ,
@@ -2305,9 +2336,12 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio
2305
2336
extend_with_drain:: check ( cx, expr, recv, arg) ;
2306
2337
} ,
2307
2338
( "filter_map" , [ arg] ) => {
2308
- unnecessary_filter_map:: check ( cx, expr, arg) ;
2339
+ unnecessary_filter_map:: check ( cx, expr, arg, name ) ;
2309
2340
filter_map_identity:: check ( cx, expr, arg, span) ;
2310
2341
} ,
2342
+ ( "find_map" , [ arg] ) => {
2343
+ unnecessary_filter_map:: check ( cx, expr, arg, name) ;
2344
+ } ,
2311
2345
( "flat_map" , [ arg] ) => {
2312
2346
flat_map_identity:: check ( cx, expr, arg, span) ;
2313
2347
flat_map_option:: check ( cx, expr, arg, span) ;
0 commit comments