@@ -1477,17 +1477,22 @@ fn lint_cstring_as_ptr(cx: &LateContext<'_, '_>, expr: &hir::Expr, new: &hir::Ex
1477
1477
}
1478
1478
}
1479
1479
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
+ }
1491
1496
}
1492
1497
}
1493
1498
@@ -1573,7 +1578,7 @@ fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args:
1573
1578
} ;
1574
1579
}
1575
1580
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 ) {
1577
1582
let mut_str = if is_mut { "_mut" } else { "" } ;
1578
1583
let caller_type = if derefs_to_slice ( cx, & iter_args[ 0 ] , cx. tables . expr_ty ( & iter_args[ 0 ] ) ) . is_some ( ) {
1579
1584
"slice"
@@ -1596,7 +1601,7 @@ fn lint_iter_nth(cx: &LateContext<'_, '_>, expr: &hir::Expr, iter_args: &[hir::E
1596
1601
) ;
1597
1602
}
1598
1603
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 ) {
1600
1605
// Note: we don't want to lint `get_mut().unwrap` for HashMap or BTreeMap,
1601
1606
// because they do not implement `IndexMut`
1602
1607
let mut applicability = Applicability :: MachineApplicable ;
@@ -1681,7 +1686,11 @@ fn lint_iter_skip_next(cx: &LateContext<'_, '_>, expr: &hir::Expr) {
1681
1686
}
1682
1687
}
1683
1688
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 > {
1685
1694
fn may_slice ( cx : & LateContext < ' _ , ' _ > , ty : Ty < ' _ > ) -> bool {
1686
1695
match ty. sty {
1687
1696
ty:: Slice ( _) => true ,
@@ -1695,17 +1704,17 @@ fn derefs_to_slice(cx: &LateContext<'_, '_>, expr: &hir::Expr, ty: Ty<'_>) -> Op
1695
1704
1696
1705
if let hir:: ExprKind :: MethodCall ( ref path, _, ref args) = expr. node {
1697
1706
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 ] )
1699
1708
} else {
1700
1709
None
1701
1710
}
1702
1711
} else {
1703
1712
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) ,
1706
1715
ty:: Ref ( _, inner, _) => {
1707
1716
if may_slice ( cx, inner) {
1708
- sugg :: Sugg :: hir_opt ( cx , expr)
1717
+ Some ( expr)
1709
1718
} else {
1710
1719
None
1711
1720
}
0 commit comments