@@ -15,7 +15,7 @@ use rustc::ty::{self, Predicate, Ty};
1515use rustc:: { declare_lint_pass, declare_tool_lint} ;
1616use rustc_errors:: Applicability ;
1717use syntax:: ast;
18- use syntax:: source_map:: { BytePos , Span } ;
18+ use syntax:: source_map:: Span ;
1919use syntax:: symbol:: LocalInternedString ;
2020
2121use crate :: utils:: paths;
@@ -1663,6 +1663,7 @@ fn lint_iter_cloned_collect<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &hir::Ex
16631663fn lint_unnecessary_fold ( cx : & LateContext < ' _ , ' _ > , expr : & hir:: Expr , fold_args : & [ hir:: Expr ] ) {
16641664 fn check_fold_with_op (
16651665 cx : & LateContext < ' _ , ' _ > ,
1666+ expr : & hir:: Expr ,
16661667 fold_args : & [ hir:: Expr ] ,
16671668 op : hir:: BinOpKind ,
16681669 replacement_method_name : & str ,
@@ -1685,30 +1686,28 @@ fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args:
16851686 if match_var( & * left_expr, first_arg_ident) ;
16861687 if replacement_has_args || match_var( & * right_expr, second_arg_ident) ;
16871688
1688- then {
1689- // Span containing `.fold(...)`
1690- let next_point = cx. sess( ) . source_map( ) . next_point( fold_args[ 0 ] . span) ;
1691- let fold_span = next_point. with_hi( fold_args[ 2 ] . span. hi( ) + BytePos ( 1 ) ) ;
1689+ if let hir:: ExprKind :: MethodCall ( _, span, _) = & expr. node;
16921690
1691+ then {
16931692 let mut applicability = Applicability :: MachineApplicable ;
16941693 let sugg = if replacement_has_args {
16951694 format!(
1696- ". {replacement}(|{s}| {r})" ,
1695+ "{replacement}(|{s}| {r})" ,
16971696 replacement = replacement_method_name,
16981697 s = second_arg_ident,
16991698 r = snippet_with_applicability( cx, right_expr. span, "EXPR" , & mut applicability) ,
17001699 )
17011700 } else {
17021701 format!(
1703- ". {replacement}()" ,
1702+ "{replacement}()" ,
17041703 replacement = replacement_method_name,
17051704 )
17061705 } ;
17071706
17081707 span_lint_and_sugg(
17091708 cx,
17101709 UNNECESSARY_FOLD ,
1711- fold_span ,
1710+ span . with_hi ( expr . span . hi ( ) ) ,
17121711 // TODO #2371 don't suggest e.g., .any(|x| f(x)) if we can suggest .any(f)
17131712 "this `.fold` can be written more succinctly using another method" ,
17141713 "try" ,
@@ -1732,10 +1731,10 @@ fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args:
17321731 // Check if the first argument to .fold is a suitable literal
17331732 if let hir:: ExprKind :: Lit ( ref lit) = fold_args[ 1 ] . node {
17341733 match lit. node {
1735- ast:: LitKind :: Bool ( false ) => check_fold_with_op ( cx, fold_args, hir:: BinOpKind :: Or , "any" , true ) ,
1736- ast:: LitKind :: Bool ( true ) => check_fold_with_op ( cx, fold_args, hir:: BinOpKind :: And , "all" , true ) ,
1737- ast:: LitKind :: Int ( 0 , _) => check_fold_with_op ( cx, fold_args, hir:: BinOpKind :: Add , "sum" , false ) ,
1738- ast:: LitKind :: Int ( 1 , _) => check_fold_with_op ( cx, fold_args, hir:: BinOpKind :: Mul , "product" , false ) ,
1734+ ast:: LitKind :: Bool ( false ) => check_fold_with_op ( cx, expr , fold_args, hir:: BinOpKind :: Or , "any" , true ) ,
1735+ ast:: LitKind :: Bool ( true ) => check_fold_with_op ( cx, expr , fold_args, hir:: BinOpKind :: And , "all" , true ) ,
1736+ ast:: LitKind :: Int ( 0 , _) => check_fold_with_op ( cx, expr , fold_args, hir:: BinOpKind :: Add , "sum" , false ) ,
1737+ ast:: LitKind :: Int ( 1 , _) => check_fold_with_op ( cx, expr , fold_args, hir:: BinOpKind :: Mul , "product" , false ) ,
17391738 _ => ( ) ,
17401739 }
17411740 }
0 commit comments