@@ -3,6 +3,7 @@ mod inefficient_to_string;
3
3
mod manual_saturating_arithmetic;
4
4
mod option_map_unwrap_or;
5
5
mod unnecessary_filter_map;
6
+ mod unnecessary_lazy_eval;
6
7
7
8
use std:: borrow:: Cow ;
8
9
use std:: fmt;
@@ -1436,17 +1437,18 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
1436
1437
[ "expect" , ..] => lint_expect ( cx, expr, arg_lists[ 0 ] ) ,
1437
1438
[ "unwrap_or" , "map" ] => option_map_unwrap_or:: lint ( cx, expr, arg_lists[ 1 ] , arg_lists[ 0 ] , method_spans[ 1 ] ) ,
1438
1439
[ "unwrap_or_else" , "map" ] => {
1439
- lint_lazy_eval ( cx, expr, arg_lists[ 0 ] , true , "unwrap_or" ) ;
1440
- lint_map_unwrap_or_else ( cx, expr, arg_lists[ 1 ] , arg_lists[ 0 ] ) ;
1440
+ if !lint_map_unwrap_or_else ( cx, expr, arg_lists[ 1 ] , arg_lists[ 0 ] ) {
1441
+ unnecessary_lazy_eval:: lint ( cx, expr, arg_lists[ 0 ] , true , "unwrap_or" ) ;
1442
+ }
1441
1443
} ,
1442
1444
[ "map_or" , ..] => lint_map_or_none ( cx, expr, arg_lists[ 0 ] ) ,
1443
1445
[ "and_then" , ..] => {
1444
- lint_lazy_eval ( cx, expr, arg_lists[ 0 ] , false , "and" ) ;
1446
+ unnecessary_lazy_eval :: lint ( cx, expr, arg_lists[ 0 ] , false , "and" ) ;
1445
1447
bind_instead_of_map:: OptionAndThenSome :: lint ( cx, expr, arg_lists[ 0 ] ) ;
1446
1448
bind_instead_of_map:: ResultAndThenOk :: lint ( cx, expr, arg_lists[ 0 ] ) ;
1447
1449
} ,
1448
1450
[ "or_else" , ..] => {
1449
- lint_lazy_eval ( cx, expr, arg_lists[ 0 ] , false , "or" ) ;
1451
+ unnecessary_lazy_eval :: lint ( cx, expr, arg_lists[ 0 ] , false , "or" ) ;
1450
1452
bind_instead_of_map:: ResultOrElseErrInfo :: lint ( cx, expr, arg_lists[ 0 ] ) ;
1451
1453
} ,
1452
1454
[ "next" , "filter" ] => lint_filter_next ( cx, expr, arg_lists[ 1 ] ) ,
@@ -1490,9 +1492,9 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
1490
1492
[ "is_file" , ..] => lint_filetype_is_file ( cx, expr, arg_lists[ 0 ] ) ,
1491
1493
[ "map" , "as_ref" ] => lint_option_as_ref_deref ( cx, expr, arg_lists[ 1 ] , arg_lists[ 0 ] , false ) ,
1492
1494
[ "map" , "as_mut" ] => lint_option_as_ref_deref ( cx, expr, arg_lists[ 1 ] , arg_lists[ 0 ] , true ) ,
1493
- [ "unwrap_or_else" , ..] => lint_lazy_eval ( cx, expr, arg_lists[ 0 ] , true , "unwrap_or" ) ,
1494
- [ "get_or_insert_with" , ..] => lint_lazy_eval ( cx, expr, arg_lists[ 0 ] , true , "get_or_insert" ) ,
1495
- [ "ok_or_else" , ..] => lint_lazy_eval ( cx, expr, arg_lists[ 0 ] , true , "ok_or" ) ,
1495
+ [ "unwrap_or_else" , ..] => unnecessary_lazy_eval :: lint ( cx, expr, arg_lists[ 0 ] , true , "unwrap_or" ) ,
1496
+ [ "get_or_insert_with" , ..] => unnecessary_lazy_eval :: lint ( cx, expr, arg_lists[ 0 ] , true , "get_or_insert" ) ,
1497
+ [ "ok_or_else" , ..] => unnecessary_lazy_eval :: lint ( cx, expr, arg_lists[ 0 ] , true , "ok_or" ) ,
1496
1498
_ => { } ,
1497
1499
}
1498
1500
@@ -2708,119 +2710,13 @@ fn lint_map_flatten<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, map
2708
2710
}
2709
2711
}
2710
2712
2711
- /// lint use of `<fn>_else(simple closure)` for `Option`s and `Result`s that can be
2712
- /// replaced with `<fn>(return value of simple closure)`
2713
- fn lint_lazy_eval < ' tcx > (
2714
- cx : & LateContext < ' tcx > ,
2715
- expr : & ' tcx hir:: Expr < ' _ > ,
2716
- args : & ' tcx [ hir:: Expr < ' _ > ] ,
2717
- allow_variant_calls : bool ,
2718
- simplify_using : & str ,
2719
- ) {
2720
- let is_option = is_type_diagnostic_item ( cx, cx. typeck_results ( ) . expr_ty ( & args[ 0 ] ) , sym ! ( option_type) ) ;
2721
- let is_result = is_type_diagnostic_item ( cx, cx. typeck_results ( ) . expr_ty ( & args[ 0 ] ) , sym ! ( result_type) ) ;
2722
-
2723
- if !is_option && !is_result {
2724
- return ;
2725
- }
2726
-
2727
- // Return true if the expression is an accessor of any of the arguments
2728
- fn expr_uses_argument ( expr : & hir:: Expr < ' _ > , params : & [ hir:: Param < ' _ > ] ) -> bool {
2729
- params. iter ( ) . any ( |arg| {
2730
- if_chain ! {
2731
- if let hir:: PatKind :: Binding ( _, _, ident, _) = arg. pat. kind;
2732
- if let hir:: ExprKind :: Path ( hir:: QPath :: Resolved ( _, ref path) ) = expr. kind;
2733
- if let [ p, ..] = path. segments;
2734
- then {
2735
- ident. name == p. ident. name
2736
- } else {
2737
- false
2738
- }
2739
- }
2740
- } )
2741
- }
2742
-
2743
- fn match_any_qpath ( path : & hir:: QPath < ' _ > , paths : & [ & [ & str ] ] ) -> bool {
2744
- paths. iter ( ) . any ( |candidate| match_qpath ( path, candidate) )
2745
- }
2746
-
2747
- fn can_simplify ( expr : & hir:: Expr < ' _ > , params : & [ hir:: Param < ' _ > ] , variant_calls : bool ) -> bool {
2748
- match expr. kind {
2749
- // Closures returning literals can be unconditionally simplified
2750
- hir:: ExprKind :: Lit ( _) => true ,
2751
-
2752
- hir:: ExprKind :: Index ( ref object, ref index) => {
2753
- // arguments are not being indexed into
2754
- if !expr_uses_argument ( object, params) {
2755
- // arguments are not used as index
2756
- !expr_uses_argument ( index, params)
2757
- } else {
2758
- false
2759
- }
2760
- } ,
2761
-
2762
- // Reading fields can be simplified if the object is not an argument of the closure
2763
- hir:: ExprKind :: Field ( ref object, _) => !expr_uses_argument ( object, params) ,
2764
-
2765
- // Paths can be simplified if the root is not the argument, this also covers None
2766
- hir:: ExprKind :: Path ( _) => !expr_uses_argument ( expr, params) ,
2767
-
2768
- // Calls to Some, Ok, Err can be considered literals if they don't derive an argument
2769
- hir:: ExprKind :: Call ( ref func, ref args) => if_chain ! {
2770
- if variant_calls; // Disable lint when rules conflict with bind_instead_of_map
2771
- if let hir:: ExprKind :: Path ( ref path) = func. kind;
2772
- if match_any_qpath( path, & [ & [ "Some" ] , & [ "Ok" ] , & [ "Err" ] ] ) ;
2773
- then {
2774
- // Recursively check all arguments
2775
- args. iter( ) . all( |arg| can_simplify( arg, params, variant_calls) )
2776
- } else {
2777
- false
2778
- }
2779
- } ,
2780
-
2781
- // For anything more complex than the above, a closure is probably the right solution,
2782
- // or the case is handled by an other lint
2783
- _ => false ,
2784
- }
2785
- }
2786
-
2787
- if let hir:: ExprKind :: Closure ( _, _, eid, _, _) = args[ 1 ] . kind {
2788
- let body = cx. tcx . hir ( ) . body ( eid) ;
2789
- let ex = & body. value ;
2790
- let params = & body. params ;
2791
-
2792
- if can_simplify ( ex, params, allow_variant_calls) {
2793
- let msg = if is_option {
2794
- "unnecessary closure used to substitute value for `Option::None`"
2795
- } else {
2796
- "unnecessary closure used to substitute value for `Result::Err`"
2797
- } ;
2798
-
2799
- span_lint_and_sugg (
2800
- cx,
2801
- UNNECESSARY_LAZY_EVALUATION ,
2802
- expr. span ,
2803
- msg,
2804
- & format ! ( "Use `{}` instead" , simplify_using) ,
2805
- format ! (
2806
- "{0}.{1}({2})" ,
2807
- snippet( cx, args[ 0 ] . span, ".." ) ,
2808
- simplify_using,
2809
- snippet( cx, ex. span, ".." ) ,
2810
- ) ,
2811
- Applicability :: MachineApplicable ,
2812
- ) ;
2813
- }
2814
- }
2815
- }
2816
-
2817
2713
/// lint use of `map().unwrap_or_else()` for `Option`s and `Result`s
2818
2714
fn lint_map_unwrap_or_else < ' tcx > (
2819
2715
cx : & LateContext < ' tcx > ,
2820
2716
expr : & ' tcx hir:: Expr < ' _ > ,
2821
2717
map_args : & ' tcx [ hir:: Expr < ' _ > ] ,
2822
2718
unwrap_args : & ' tcx [ hir:: Expr < ' _ > ] ,
2823
- ) {
2719
+ ) -> bool {
2824
2720
// lint if the caller of `map()` is an `Option`
2825
2721
let is_option = is_type_diagnostic_item ( cx, cx. typeck_results ( ) . expr_ty ( & map_args[ 0 ] ) , sym ! ( option_type) ) ;
2826
2722
let is_result = is_type_diagnostic_item ( cx, cx. typeck_results ( ) . expr_ty ( & map_args[ 0 ] ) , sym ! ( result_type) ) ;
@@ -2832,10 +2728,10 @@ fn lint_map_unwrap_or_else<'tcx>(
2832
2728
let unwrap_mutated_vars = mutated_variables ( & unwrap_args[ 1 ] , cx) ;
2833
2729
if let ( Some ( map_mutated_vars) , Some ( unwrap_mutated_vars) ) = ( map_mutated_vars, unwrap_mutated_vars) {
2834
2730
if map_mutated_vars. intersection ( & unwrap_mutated_vars) . next ( ) . is_some ( ) {
2835
- return ;
2731
+ return false ;
2836
2732
}
2837
2733
} else {
2838
- return ;
2734
+ return false ;
2839
2735
}
2840
2736
2841
2737
// lint message
@@ -2865,9 +2761,15 @@ fn lint_map_unwrap_or_else<'tcx>(
2865
2761
map_snippet, unwrap_snippet,
2866
2762
) ,
2867
2763
) ;
2764
+ true
2868
2765
} else if same_span && multiline {
2869
2766
span_lint ( cx, MAP_UNWRAP_OR , expr. span , msg) ;
2870
- } ;
2767
+ true
2768
+ } else {
2769
+ false
2770
+ }
2771
+ } else {
2772
+ false
2871
2773
}
2872
2774
}
2873
2775
0 commit comments