@@ -61,11 +61,19 @@ declare_clippy_lint! {
6161 /// **Known problems:** None.
6262 ///
6363 /// **Example:**
64- /// ```ignore
64+ /// ```rust
65+ /// let vec = vec!['a', 'b', 'c'];
6566 /// for i in 0..vec.len() {
6667 /// println!("{}", vec[i]);
6768 /// }
6869 /// ```
70+ /// Could be written as:
71+ /// ```rust
72+ /// let vec = vec!['a', 'b', 'c'];
73+ /// for i in vec {
74+ /// println!("{}", i);
75+ /// }
76+ /// ```
6977 pub NEEDLESS_RANGE_LOOP ,
7078 style,
7179 "for-looping over a range of indices where an iterator over items would do"
@@ -1662,7 +1670,16 @@ fn check_for_mutation(
16621670 } ;
16631671 let def_id = def_id:: DefId :: local ( body. hir_id . owner ) ;
16641672 let region_scope_tree = & cx. tcx . region_scope_tree ( def_id) ;
1665- ExprUseVisitor :: new ( & mut delegate, cx. tcx , cx. param_env , region_scope_tree, cx. tables , None ) . walk_expr ( body) ;
1673+ ExprUseVisitor :: new (
1674+ & mut delegate,
1675+ cx. tcx ,
1676+ def_id,
1677+ cx. param_env ,
1678+ region_scope_tree,
1679+ cx. tables ,
1680+ None ,
1681+ )
1682+ . walk_expr ( body) ;
16661683 delegate. mutation_span ( )
16671684}
16681685
@@ -1769,7 +1786,7 @@ impl<'a, 'tcx> VarVisitor<'a, 'tcx> {
17691786 }
17701787 let res = self . cx. tables. qpath_res( seqpath, seqexpr. hir_id) ;
17711788 match res {
1772- Res :: Local ( hir_id) | Res :: Upvar ( hir_id , .. ) => {
1789+ Res :: Local ( hir_id) => {
17731790 let parent_id = self . cx. tcx. hir( ) . get_parent_item( expr. hir_id) ;
17741791 let parent_def_id = self . cx. tcx. hir( ) . local_def_id_from_hir_id( parent_id) ;
17751792 let extent = self . cx. tcx. region_scope_tree( parent_def_id) . var_scope( hir_id. local_id) ;
@@ -1829,24 +1846,13 @@ impl<'a, 'tcx> Visitor<'tcx> for VarVisitor<'a, 'tcx> {
18291846 if let QPath :: Resolved ( None , ref path) = * qpath;
18301847 if path. segments. len( ) == 1 ;
18311848 then {
1832- match self . cx. tables. qpath_res( qpath, expr. hir_id) {
1833- Res :: Upvar ( local_id, ..) => {
1834- if local_id == self . var {
1835- // we are not indexing anything, record that
1836- self . nonindex = true ;
1837- }
1838- }
1839- Res :: Local ( local_id) =>
1840- {
1841-
1842- if local_id == self . var {
1843- self . nonindex = true ;
1844- } else {
1845- // not the correct variable, but still a variable
1846- self . referenced. insert( path. segments[ 0 ] . ident. name) ;
1847- }
1849+ if let Res :: Local ( local_id) = self . cx. tables. qpath_res( qpath, expr. hir_id) {
1850+ if local_id == self . var {
1851+ self . nonindex = true ;
1852+ } else {
1853+ // not the correct variable, but still a variable
1854+ self . referenced. insert( path. segments[ 0 ] . ident. name) ;
18481855 }
1849- _ => { }
18501856 }
18511857 }
18521858 }
@@ -2378,7 +2384,7 @@ impl<'a, 'tcx> VarCollectorVisitor<'a, 'tcx> {
23782384 let res = self . cx. tables. qpath_res( qpath, ex. hir_id) ;
23792385 then {
23802386 match res {
2381- Res :: Local ( node_id) | Res :: Upvar ( node_id , .. ) => {
2387+ Res :: Local ( node_id) => {
23822388 self . ids. insert( node_id) ;
23832389 } ,
23842390 Res :: Def ( DefKind :: Static , def_id) => {
0 commit comments