@@ -1000,7 +1000,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
1000
1000
return ;
1001
1001
}
1002
1002
1003
- let ( method_names, arg_lists) = method_calls ( expr, 2 ) ;
1003
+ let ( method_names, arg_lists, method_spans ) = method_calls ( expr, 2 ) ;
1004
1004
let method_names: Vec < LocalInternedString > = method_names. iter ( ) . map ( |s| s. as_str ( ) ) . collect ( ) ;
1005
1005
let method_names: Vec < & str > = method_names. iter ( ) . map ( std:: convert:: AsRef :: as_ref) . collect ( ) ;
1006
1006
@@ -1020,7 +1020,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Methods {
1020
1020
[ "map" , "find" ] => lint_find_map ( cx, expr, arg_lists[ 1 ] , arg_lists[ 0 ] ) ,
1021
1021
[ "flat_map" , "filter" ] => lint_filter_flat_map ( cx, expr, arg_lists[ 1 ] , arg_lists[ 0 ] ) ,
1022
1022
[ "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 ] ) ,
1024
1024
[ "flatten" , "map" ] => lint_map_flatten ( cx, expr, arg_lists[ 1 ] ) ,
1025
1025
[ "is_some" , "find" ] => lint_search_is_some ( cx, expr, "find" , arg_lists[ 1 ] , arg_lists[ 0 ] ) ,
1026
1026
[ "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 {
1035
1035
[ "collect" , "cloned" ] => lint_iter_cloned_collect ( cx, expr, arg_lists[ 1 ] ) ,
1036
1036
[ "as_ref" ] => lint_asref ( cx, expr, "as_ref" , arg_lists[ 0 ] ) ,
1037
1037
[ "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 ] ) ,
1039
1039
[ "filter_map" , ..] => unnecessary_filter_map:: lint ( cx, expr, arg_lists[ 0 ] ) ,
1040
1040
[ "count" , "map" ] => lint_suspicious_map ( cx, expr) ,
1041
1041
_ => { } ,
@@ -1712,11 +1712,12 @@ fn lint_iter_cloned_collect<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &hir::Ex
1712
1712
}
1713
1713
}
1714
1714
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 ) {
1716
1716
fn check_fold_with_op (
1717
1717
cx : & LateContext < ' _ , ' _ > ,
1718
1718
expr : & hir:: Expr ,
1719
1719
fold_args : & [ hir:: Expr ] ,
1720
+ fold_span : Span ,
1720
1721
op : hir:: BinOpKind ,
1721
1722
replacement_method_name : & str ,
1722
1723
replacement_has_args : bool ,
@@ -1738,8 +1739,6 @@ fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args:
1738
1739
if match_var( & * left_expr, first_arg_ident) ;
1739
1740
if replacement_has_args || match_var( & * right_expr, second_arg_ident) ;
1740
1741
1741
- if let hir:: ExprKind :: MethodCall ( _, span, _) = & expr. node;
1742
-
1743
1742
then {
1744
1743
let mut applicability = Applicability :: MachineApplicable ;
1745
1744
let sugg = if replacement_has_args {
@@ -1759,7 +1758,7 @@ fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args:
1759
1758
span_lint_and_sugg(
1760
1759
cx,
1761
1760
UNNECESSARY_FOLD ,
1762
- span . with_hi( expr. span. hi( ) ) ,
1761
+ fold_span . with_hi( expr. span. hi( ) ) ,
1763
1762
// TODO #2371 don't suggest e.g., .any(|x| f(x)) if we can suggest .any(f)
1764
1763
"this `.fold` can be written more succinctly using another method" ,
1765
1764
"try" ,
@@ -1783,10 +1782,18 @@ fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args:
1783
1782
// Check if the first argument to .fold is a suitable literal
1784
1783
if let hir:: ExprKind :: Lit ( ref lit) = fold_args[ 1 ] . node {
1785
1784
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
+ } ,
1790
1797
_ => ( ) ,
1791
1798
}
1792
1799
}
@@ -2323,22 +2330,21 @@ fn lint_flat_map_identity<'a, 'tcx>(
2323
2330
cx : & LateContext < ' a , ' tcx > ,
2324
2331
expr : & ' tcx hir:: Expr ,
2325
2332
flat_map_args : & ' tcx [ hir:: Expr ] ,
2333
+ flat_map_span : Span ,
2326
2334
) {
2327
2335
if match_trait_method ( cx, expr, & paths:: ITERATOR ) {
2328
2336
let arg_node = & flat_map_args[ 1 ] . node ;
2329
2337
2330
2338
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
+ ) ;
2342
2348
} ;
2343
2349
2344
2350
if_chain ! {
0 commit comments