@@ -1477,17 +1477,22 @@ fn lint_cstring_as_ptr(cx: &LateContext<'_, '_>, expr: &hir::Expr, new: &hir::Ex
14771477 }
14781478}
14791479
1480- fn lint_iter_cloned_collect ( cx : & LateContext < ' _ , ' _ > , expr : & hir:: Expr , iter_args : & [ hir:: Expr ] ) {
1481- if match_type ( cx, cx. tables . expr_ty ( expr) , & paths:: VEC )
1482- && derefs_to_slice ( cx, & iter_args[ 0 ] , cx. tables . expr_ty ( & iter_args[ 0 ] ) ) . is_some ( )
1483- {
1484- span_lint (
1485- cx,
1486- ITER_CLONED_COLLECT ,
1487- expr. span ,
1488- "called `cloned().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and \
1489- more readable",
1490- ) ;
1480+ fn lint_iter_cloned_collect < ' a , ' tcx > ( cx : & LateContext < ' a , ' tcx > , expr : & hir:: Expr , iter_args : & ' tcx [ hir:: Expr ] ) {
1481+ if match_type ( cx, cx. tables . expr_ty ( expr) , & paths:: VEC ) {
1482+ if let Some ( slice) = derefs_to_slice ( cx, & iter_args[ 0 ] , cx. tables . expr_ty ( & iter_args[ 0 ] ) ) {
1483+ if let Some ( to_replace) = expr. span . trim_start ( slice. span . source_callsite ( ) ) {
1484+ span_lint_and_sugg (
1485+ cx,
1486+ ITER_CLONED_COLLECT ,
1487+ to_replace,
1488+ "called `iter().cloned().collect()` on a slice to create a `Vec`. Calling `to_vec()` is both faster and \
1489+ more readable",
1490+ "try" ,
1491+ ".to_vec()" . to_string ( ) ,
1492+ Applicability :: MachineApplicable ,
1493+ ) ;
1494+ }
1495+ }
14911496 }
14921497}
14931498
@@ -1573,7 +1578,7 @@ fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args:
15731578 } ;
15741579}
15751580
1576- fn lint_iter_nth ( cx : & LateContext < ' _ , ' _ > , expr : & hir:: Expr , iter_args : & [ hir:: Expr ] , is_mut : bool ) {
1581+ fn lint_iter_nth < ' a , ' tcx > ( cx : & LateContext < ' a , ' tcx > , expr : & hir:: Expr , iter_args : & ' tcx [ hir:: Expr ] , is_mut : bool ) {
15771582 let mut_str = if is_mut { "_mut" } else { "" } ;
15781583 let caller_type = if derefs_to_slice ( cx, & iter_args[ 0 ] , cx. tables . expr_ty ( & iter_args[ 0 ] ) ) . is_some ( ) {
15791584 "slice"
@@ -1596,7 +1601,7 @@ fn lint_iter_nth(cx: &LateContext<'_, '_>, expr: &hir::Expr, iter_args: &[hir::E
15961601 ) ;
15971602}
15981603
1599- fn lint_get_unwrap ( cx : & LateContext < ' _ , ' _ > , expr : & hir:: Expr , get_args : & [ hir:: Expr ] , is_mut : bool ) {
1604+ fn lint_get_unwrap < ' a , ' tcx > ( cx : & LateContext < ' a , ' tcx > , expr : & hir:: Expr , get_args : & ' tcx [ hir:: Expr ] , is_mut : bool ) {
16001605 // Note: we don't want to lint `get_mut().unwrap` for HashMap or BTreeMap,
16011606 // because they do not implement `IndexMut`
16021607 let mut applicability = Applicability :: MachineApplicable ;
@@ -1681,7 +1686,11 @@ fn lint_iter_skip_next(cx: &LateContext<'_, '_>, expr: &hir::Expr) {
16811686 }
16821687}
16831688
1684- fn derefs_to_slice ( cx : & LateContext < ' _ , ' _ > , expr : & hir:: Expr , ty : Ty < ' _ > ) -> Option < sugg:: Sugg < ' static > > {
1689+ fn derefs_to_slice < ' a , ' tcx > (
1690+ cx : & LateContext < ' a , ' tcx > ,
1691+ expr : & ' tcx hir:: Expr ,
1692+ ty : Ty < ' tcx > ,
1693+ ) -> Option < & ' tcx hir:: Expr > {
16851694 fn may_slice ( cx : & LateContext < ' _ , ' _ > , ty : Ty < ' _ > ) -> bool {
16861695 match ty. sty {
16871696 ty:: Slice ( _) => true ,
@@ -1695,17 +1704,17 @@ fn derefs_to_slice(cx: &LateContext<'_, '_>, expr: &hir::Expr, ty: Ty<'_>) -> Op
16951704
16961705 if let hir:: ExprKind :: MethodCall ( ref path, _, ref args) = expr. node {
16971706 if path. ident . name == "iter" && may_slice ( cx, cx. tables . expr_ty ( & args[ 0 ] ) ) {
1698- sugg :: Sugg :: hir_opt ( cx , & args[ 0 ] ) . map ( sugg :: Sugg :: addr )
1707+ Some ( & args[ 0 ] )
16991708 } else {
17001709 None
17011710 }
17021711 } else {
17031712 match ty. sty {
1704- ty:: Slice ( _) => sugg :: Sugg :: hir_opt ( cx , expr) ,
1705- ty:: Adt ( def, _) if def. is_box ( ) && may_slice ( cx, ty. boxed_ty ( ) ) => sugg :: Sugg :: hir_opt ( cx , expr) ,
1713+ ty:: Slice ( _) => Some ( expr) ,
1714+ ty:: Adt ( def, _) if def. is_box ( ) && may_slice ( cx, ty. boxed_ty ( ) ) => Some ( expr) ,
17061715 ty:: Ref ( _, inner, _) => {
17071716 if may_slice ( cx, inner) {
1708- sugg :: Sugg :: hir_opt ( cx , expr)
1717+ Some ( expr)
17091718 } else {
17101719 None
17111720 }
0 commit comments