@@ -1000,7 +1000,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
10001000 return ;
10011001 }
10021002
1003- let ( method_names, arg_lists) = method_calls ( expr, 2 ) ;
1003+ let ( method_names, arg_lists, method_spans ) = method_calls ( expr, 2 ) ;
10041004 let method_names: Vec < LocalInternedString > = method_names. iter ( ) . map ( |s| s. as_str ( ) ) . collect ( ) ;
10051005 let method_names: Vec < & str > = method_names. iter ( ) . map ( std:: convert:: AsRef :: as_ref) . collect ( ) ;
10061006
@@ -1020,7 +1020,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
10201020 [ "map" , "find" ] => lint_find_map ( cx, expr, arg_lists[ 1 ] , arg_lists[ 0 ] ) ,
10211021 [ "flat_map" , "filter" ] => lint_filter_flat_map ( cx, expr, arg_lists[ 1 ] , arg_lists[ 0 ] ) ,
10221022 [ "flat_map" , "filter_map" ] => lint_filter_map_flat_map ( cx, expr, arg_lists[ 1 ] , arg_lists[ 0 ] ) ,
1023- [ "flat_map" , ..] => lint_flat_map_identity ( cx, expr, arg_lists[ 0 ] ) ,
1023+ [ "flat_map" , ..] => lint_flat_map_identity ( cx, expr, arg_lists[ 0 ] , method_spans [ 0 ] ) ,
10241024 [ "flatten" , "map" ] => lint_map_flatten ( cx, expr, arg_lists[ 1 ] ) ,
10251025 [ "is_some" , "find" ] => lint_search_is_some ( cx, expr, "find" , arg_lists[ 1 ] , arg_lists[ 0 ] ) ,
10261026 [ "is_some" , "position" ] => lint_search_is_some ( cx, expr, "position" , arg_lists[ 1 ] , arg_lists[ 0 ] ) ,
@@ -1035,7 +1035,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
10351035 [ "collect" , "cloned" ] => lint_iter_cloned_collect ( cx, expr, arg_lists[ 1 ] ) ,
10361036 [ "as_ref" ] => lint_asref ( cx, expr, "as_ref" , arg_lists[ 0 ] ) ,
10371037 [ "as_mut" ] => lint_asref ( cx, expr, "as_mut" , arg_lists[ 0 ] ) ,
1038- [ "fold" , ..] => lint_unnecessary_fold ( cx, expr, arg_lists[ 0 ] ) ,
1038+ [ "fold" , ..] => lint_unnecessary_fold ( cx, expr, arg_lists[ 0 ] , method_spans [ 0 ] ) ,
10391039 [ "filter_map" , ..] => unnecessary_filter_map:: lint ( cx, expr, arg_lists[ 0 ] ) ,
10401040 [ "count" , "map" ] => lint_suspicious_map ( cx, expr) ,
10411041 _ => { } ,
@@ -1712,11 +1712,12 @@ fn lint_iter_cloned_collect<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &hir::Ex
17121712 }
17131713}
17141714
1715- fn lint_unnecessary_fold ( cx : & LateContext < ' _ , ' _ > , expr : & hir:: Expr , fold_args : & [ hir:: Expr ] ) {
1715+ fn lint_unnecessary_fold ( cx : & LateContext < ' _ , ' _ > , expr : & hir:: Expr , fold_args : & [ hir:: Expr ] , fold_span : Span ) {
17161716 fn check_fold_with_op (
17171717 cx : & LateContext < ' _ , ' _ > ,
17181718 expr : & hir:: Expr ,
17191719 fold_args : & [ hir:: Expr ] ,
1720+ fold_span : Span ,
17201721 op : hir:: BinOpKind ,
17211722 replacement_method_name : & str ,
17221723 replacement_has_args : bool ,
@@ -1738,8 +1739,6 @@ fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args:
17381739 if match_var( & * left_expr, first_arg_ident) ;
17391740 if replacement_has_args || match_var( & * right_expr, second_arg_ident) ;
17401741
1741- if let hir:: ExprKind :: MethodCall ( _, span, _) = & expr. node;
1742-
17431742 then {
17441743 let mut applicability = Applicability :: MachineApplicable ;
17451744 let sugg = if replacement_has_args {
@@ -1759,7 +1758,7 @@ fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args:
17591758 span_lint_and_sugg(
17601759 cx,
17611760 UNNECESSARY_FOLD ,
1762- span . with_hi( expr. span. hi( ) ) ,
1761+ fold_span . with_hi( expr. span. hi( ) ) ,
17631762 // TODO #2371 don't suggest e.g., .any(|x| f(x)) if we can suggest .any(f)
17641763 "this `.fold` can be written more succinctly using another method" ,
17651764 "try" ,
@@ -1783,10 +1782,18 @@ fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args:
17831782 // Check if the first argument to .fold is a suitable literal
17841783 if let hir:: ExprKind :: Lit ( ref lit) = fold_args[ 1 ] . node {
17851784 match lit. node {
1786- ast:: LitKind :: Bool ( false ) => check_fold_with_op ( cx, expr, fold_args, hir:: BinOpKind :: Or , "any" , true ) ,
1787- ast:: LitKind :: Bool ( true ) => check_fold_with_op ( cx, expr, fold_args, hir:: BinOpKind :: And , "all" , true ) ,
1788- ast:: LitKind :: Int ( 0 , _) => check_fold_with_op ( cx, expr, fold_args, hir:: BinOpKind :: Add , "sum" , false ) ,
1789- ast:: LitKind :: Int ( 1 , _) => check_fold_with_op ( cx, expr, fold_args, hir:: BinOpKind :: Mul , "product" , false ) ,
1785+ ast:: LitKind :: Bool ( false ) => {
1786+ check_fold_with_op ( cx, expr, fold_args, fold_span, hir:: BinOpKind :: Or , "any" , true )
1787+ } ,
1788+ ast:: LitKind :: Bool ( true ) => {
1789+ check_fold_with_op ( cx, expr, fold_args, fold_span, hir:: BinOpKind :: And , "all" , true )
1790+ } ,
1791+ ast:: LitKind :: Int ( 0 , _) => {
1792+ check_fold_with_op ( cx, expr, fold_args, fold_span, hir:: BinOpKind :: Add , "sum" , false )
1793+ } ,
1794+ ast:: LitKind :: Int ( 1 , _) => {
1795+ check_fold_with_op ( cx, expr, fold_args, fold_span, hir:: BinOpKind :: Mul , "product" , false )
1796+ } ,
17901797 _ => ( ) ,
17911798 }
17921799 }
@@ -2323,22 +2330,21 @@ fn lint_flat_map_identity<'a, 'tcx>(
23232330 cx : & LateContext < ' a , ' tcx > ,
23242331 expr : & ' tcx hir:: Expr ,
23252332 flat_map_args : & ' tcx [ hir:: Expr ] ,
2333+ flat_map_span : Span ,
23262334) {
23272335 if match_trait_method ( cx, expr, & paths:: ITERATOR ) {
23282336 let arg_node = & flat_map_args[ 1 ] . node ;
23292337
23302338 let apply_lint = |message : & str | {
2331- if let hir:: ExprKind :: MethodCall ( _, span, _) = & expr. node {
2332- span_lint_and_sugg (
2333- cx,
2334- FLAT_MAP_IDENTITY ,
2335- span. with_hi ( expr. span . hi ( ) ) ,
2336- message,
2337- "try" ,
2338- "flatten()" . to_string ( ) ,
2339- Applicability :: MachineApplicable ,
2340- ) ;
2341- }
2339+ span_lint_and_sugg (
2340+ cx,
2341+ FLAT_MAP_IDENTITY ,
2342+ flat_map_span. with_hi ( expr. span . hi ( ) ) ,
2343+ message,
2344+ "try" ,
2345+ "flatten()" . to_string ( ) ,
2346+ Applicability :: MachineApplicable ,
2347+ ) ;
23422348 } ;
23432349
23442350 if_chain ! {
0 commit comments