@@ -15,7 +15,7 @@ use rustc::ty::{self, Predicate, Ty};
15
15
use rustc:: { declare_lint_pass, declare_tool_lint} ;
16
16
use rustc_errors:: Applicability ;
17
17
use syntax:: ast;
18
- use syntax:: source_map:: { BytePos , Span } ;
18
+ use syntax:: source_map:: Span ;
19
19
use syntax:: symbol:: LocalInternedString ;
20
20
21
21
use crate :: utils:: paths;
@@ -1663,6 +1663,7 @@ fn lint_iter_cloned_collect<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &hir::Ex
1663
1663
fn lint_unnecessary_fold ( cx : & LateContext < ' _ , ' _ > , expr : & hir:: Expr , fold_args : & [ hir:: Expr ] ) {
1664
1664
fn check_fold_with_op (
1665
1665
cx : & LateContext < ' _ , ' _ > ,
1666
+ expr : & hir:: Expr ,
1666
1667
fold_args : & [ hir:: Expr ] ,
1667
1668
op : hir:: BinOpKind ,
1668
1669
replacement_method_name : & str ,
@@ -1685,30 +1686,28 @@ fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args:
1685
1686
if match_var( & * left_expr, first_arg_ident) ;
1686
1687
if replacement_has_args || match_var( & * right_expr, second_arg_ident) ;
1687
1688
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;
1692
1690
1691
+ then {
1693
1692
let mut applicability = Applicability :: MachineApplicable ;
1694
1693
let sugg = if replacement_has_args {
1695
1694
format!(
1696
- ". {replacement}(|{s}| {r})" ,
1695
+ "{replacement}(|{s}| {r})" ,
1697
1696
replacement = replacement_method_name,
1698
1697
s = second_arg_ident,
1699
1698
r = snippet_with_applicability( cx, right_expr. span, "EXPR" , & mut applicability) ,
1700
1699
)
1701
1700
} else {
1702
1701
format!(
1703
- ". {replacement}()" ,
1702
+ "{replacement}()" ,
1704
1703
replacement = replacement_method_name,
1705
1704
)
1706
1705
} ;
1707
1706
1708
1707
span_lint_and_sugg(
1709
1708
cx,
1710
1709
UNNECESSARY_FOLD ,
1711
- fold_span ,
1710
+ span . with_hi ( expr . span . hi ( ) ) ,
1712
1711
// TODO #2371 don't suggest e.g., .any(|x| f(x)) if we can suggest .any(f)
1713
1712
"this `.fold` can be written more succinctly using another method" ,
1714
1713
"try" ,
@@ -1732,10 +1731,10 @@ fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args:
1732
1731
// Check if the first argument to .fold is a suitable literal
1733
1732
if let hir:: ExprKind :: Lit ( ref lit) = fold_args[ 1 ] . node {
1734
1733
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 ) ,
1739
1738
_ => ( ) ,
1740
1739
}
1741
1740
}
0 commit comments