@@ -15,7 +15,8 @@ use if_chain::if_chain;
1515use rustc_ast:: ast;
1616use rustc_errors:: Applicability ;
1717use rustc_hir as hir;
18- use rustc_hir:: { TraitItem , TraitItemKind } ;
18+ use rustc_hir:: def:: Res ;
19+ use rustc_hir:: { Expr , ExprKind , PatKind , QPath , TraitItem , TraitItemKind , UnOp } ;
1920use rustc_lint:: { LateContext , LateLintPass , Lint , LintContext } ;
2021use rustc_middle:: lint:: in_external_macro;
2122use rustc_middle:: ty:: { self , TraitRef , Ty , TyS } ;
@@ -450,6 +451,32 @@ declare_clippy_lint! {
450451 "using combinations of `filter`, `map`, `filter_map` and `flat_map` which can usually be written as a single method call"
451452}
452453
454+ declare_clippy_lint ! {
455+ /// **What it does:** Checks for usage of `_.filter(_).map(_)` that can be written more simply
456+ /// as `filter_map(_)`.
457+ ///
458+ /// **Why is this bad?** Redundant code in the `filter` and `map` operations is poor style and
459+ /// less performant.
460+ ///
461+ /// **Known problems:** None.
462+ ///
463+ /// **Example:**
464+ /// Bad:
465+ /// ```rust
466+ /// (0_i32..10)
467+ /// .filter(|n| n.checked_add(1).is_some())
468+ /// .map(|n| n.checked_add(1).unwrap());
469+ /// ```
470+ ///
471+ /// Good:
472+ /// ```rust
473+ /// (0_i32..10).filter_map(|n| n.checked_add(1));
474+ /// ```
475+ pub MANUAL_FILTER_MAP ,
476+ complexity,
477+ "using `_.filter(_).map(_)` in a way that can be written more simply as `filter_map(_)`"
478+ }
479+
453480declare_clippy_lint ! {
454481 /// **What it does:** Checks for usage of `_.filter_map(_).next()`.
455482 ///
@@ -1473,6 +1500,7 @@ impl_lint_pass!(Methods => [
14731500 FILTER_NEXT ,
14741501 SKIP_WHILE_NEXT ,
14751502 FILTER_MAP ,
1503+ MANUAL_FILTER_MAP ,
14761504 FILTER_MAP_NEXT ,
14771505 FLAT_MAP_IDENTITY ,
14781506 FIND_MAP ,
@@ -1540,7 +1568,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
15401568 [ "next" , "filter" ] => lint_filter_next ( cx, expr, arg_lists[ 1 ] ) ,
15411569 [ "next" , "skip_while" ] => lint_skip_while_next ( cx, expr, arg_lists[ 1 ] ) ,
15421570 [ "next" , "iter" ] => lint_iter_next ( cx, expr, arg_lists[ 1 ] ) ,
1543- [ "map" , "filter" ] => lint_filter_map ( cx, expr, arg_lists [ 1 ] , arg_lists [ 0 ] ) ,
1571+ [ "map" , "filter" ] => lint_filter_map ( cx, expr) ,
15441572 [ "map" , "filter_map" ] => lint_filter_map_map ( cx, expr, arg_lists[ 1 ] , arg_lists[ 0 ] ) ,
15451573 [ "next" , "filter_map" ] => lint_filter_map_next ( cx, expr, arg_lists[ 1 ] , self . msrv . as_ref ( ) ) ,
15461574 [ "map" , "find" ] => lint_find_map ( cx, expr, arg_lists[ 1 ] , arg_lists[ 0 ] ) ,
@@ -2989,17 +3017,72 @@ fn lint_skip_while_next<'tcx>(
29893017}
29903018
29913019/// lint use of `filter().map()` for `Iterators`
2992- fn lint_filter_map < ' tcx > (
2993- cx : & LateContext < ' tcx > ,
2994- expr : & ' tcx hir:: Expr < ' _ > ,
2995- _filter_args : & ' tcx [ hir:: Expr < ' _ > ] ,
2996- _map_args : & ' tcx [ hir:: Expr < ' _ > ] ,
2997- ) {
2998- // lint if caller of `.filter().map()` is an Iterator
2999- if match_trait_method ( cx, expr, & paths:: ITERATOR ) {
3000- let msg = "called `filter(..).map(..)` on an `Iterator`" ;
3001- let hint = "this is more succinctly expressed by calling `.filter_map(..)` instead" ;
3002- span_lint_and_help ( cx, FILTER_MAP , expr. span , msg, None , hint) ;
3020+ fn lint_filter_map < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx hir:: Expr < ' _ > ) {
3021+ if_chain ! {
3022+ if let ExprKind :: MethodCall ( _, _, [ map_recv, map_arg] , map_span) = expr. kind;
3023+ if let ExprKind :: MethodCall ( _, _, [ _, filter_arg] , filter_span) = map_recv. kind;
3024+ if match_trait_method( cx, expr, & paths:: ITERATOR ) ;
3025+
3026+ // filter(|x| ...is_some())...
3027+ if let ExprKind :: Closure ( _, _, filter_body_id, ..) = filter_arg. kind;
3028+ let filter_body = cx. tcx. hir( ) . body( filter_body_id) ;
3029+ if let [ filter_param] = filter_body. params;
3030+ // optional ref pattern: `filter(|&x| ..)`
3031+ let ( filter_pat, is_filter_param_ref) = if let PatKind :: Ref ( ref_pat, _) = filter_param. pat. kind {
3032+ ( ref_pat, true )
3033+ } else {
3034+ ( filter_param. pat, false )
3035+ } ;
3036+ // closure ends with is_some() or is_ok()
3037+ if let PatKind :: Binding ( _, filter_param_id, _, None ) = filter_pat. kind;
3038+ if let ExprKind :: MethodCall ( path, _, [ filter_arg] , _) = filter_body. value. kind;
3039+ if let Some ( opt_ty) = cx. typeck_results( ) . expr_ty( filter_arg) . ty_adt_def( ) ;
3040+ if let Some ( is_result) = if cx. tcx. is_diagnostic_item( sym:: option_type, opt_ty. did) {
3041+ Some ( false )
3042+ } else if cx. tcx. is_diagnostic_item( sym:: result_type, opt_ty. did) {
3043+ Some ( true )
3044+ } else {
3045+ None
3046+ } ;
3047+ if path. ident. name. as_str( ) == if is_result { "is_ok" } else { "is_some" } ;
3048+
3049+ // ...map(|x| ...unwrap())
3050+ if let ExprKind :: Closure ( _, _, map_body_id, ..) = map_arg. kind;
3051+ let map_body = cx. tcx. hir( ) . body( map_body_id) ;
3052+ if let [ map_param] = map_body. params;
3053+ if let PatKind :: Binding ( _, map_param_id, map_param_ident, None ) = map_param. pat. kind;
3054+ // closure ends with expect() or unwrap()
3055+ if let ExprKind :: MethodCall ( seg, _, [ map_arg, ..] , _) = map_body. value. kind;
3056+ if matches!( seg. ident. name, sym:: expect | sym:: unwrap | sym:: unwrap_or) ;
3057+
3058+ let eq_fallback = |a: & Expr <' _>, b: & Expr <' _>| {
3059+ // in `filter(|x| ..)`, replace `*x` with `x`
3060+ let a_path = if_chain! {
3061+ if !is_filter_param_ref;
3062+ if let ExprKind :: Unary ( UnOp :: UnDeref , expr_path) = a. kind;
3063+ then { expr_path } else { a }
3064+ } ;
3065+ // let the filter closure arg and the map closure arg be equal
3066+ if_chain! {
3067+ if let ExprKind :: Path ( QPath :: Resolved ( None , a_path) ) = a_path. kind;
3068+ if let ExprKind :: Path ( QPath :: Resolved ( None , b_path) ) = b. kind;
3069+ if a_path. res == Res :: Local ( filter_param_id) ;
3070+ if b_path. res == Res :: Local ( map_param_id) ;
3071+ if TyS :: same_type( cx. typeck_results( ) . expr_ty_adjusted( a) , cx. typeck_results( ) . expr_ty_adjusted( b) ) ;
3072+ then {
3073+ return true ;
3074+ }
3075+ }
3076+ false
3077+ } ;
3078+ if SpanlessEq :: new( cx) . expr_fallback( eq_fallback) . eq_expr( filter_arg, map_arg) ;
3079+ then {
3080+ let span = filter_span. to( map_span) ;
3081+ let msg = "`filter(..).map(..)` can be simplified as `filter_map(..)`" ;
3082+ let to_opt = if is_result { ".ok()" } else { "" } ;
3083+ let sugg = format!( "filter_map(|{}| {}{})" , map_param_ident, snippet( cx, map_arg. span, ".." ) , to_opt) ;
3084+ span_lint_and_sugg( cx, MANUAL_FILTER_MAP , span, msg, "try" , sugg, Applicability :: MachineApplicable ) ;
3085+ }
30033086 }
30043087}
30053088
0 commit comments