@@ -3,7 +3,7 @@ use if_chain::if_chain;
33use itertools:: Itertools ;
44use rustc:: hir:: def:: Def ;
55use rustc:: hir:: def_id;
6- use rustc:: hir:: intravisit:: { walk_block, walk_decl , walk_expr, walk_pat, walk_stmt, NestedVisitorMap , Visitor } ;
6+ use rustc:: hir:: intravisit:: { walk_block, walk_expr, walk_pat, walk_stmt, NestedVisitorMap , Visitor } ;
77use rustc:: hir:: * ;
88use rustc:: lint:: { in_external_macro, LateContext , LateLintPass , LintArray , LintContext , LintPass } ;
99use rustc:: middle:: region;
@@ -565,6 +565,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
565565 && lhs_constructor. ident . name == "Some"
566566 && ( pat_args. is_empty ( )
567567 || !is_refutable ( cx, & pat_args[ 0 ] )
568+ && !is_used_inside ( cx, iter_expr, & arms[ 0 ] . body )
568569 && !is_iterator_used_after_while_let ( cx, iter_expr)
569570 && !is_nested ( cx, expr, & method_args[ 0 ] ) )
570571 {
@@ -596,7 +597,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
596597 }
597598
598599 fn check_stmt ( & mut self , cx : & LateContext < ' a , ' tcx > , stmt : & ' tcx Stmt ) {
599- if let StmtKind :: Semi ( ref expr, _ ) = stmt. node {
600+ if let StmtKind :: Semi ( ref expr) = stmt. node {
600601 if let ExprKind :: MethodCall ( ref method, _, ref args) = expr. node {
601602 if args. len ( ) == 1 && method. ident . name == "collect" && match_trait_method ( cx, expr, & paths:: ITERATOR ) {
602603 span_lint (
@@ -667,13 +668,7 @@ fn never_loop_block(block: &Block, main_loop_id: NodeId) -> NeverLoopResult {
667668fn stmt_to_expr ( stmt : & Stmt ) -> Option < & Expr > {
668669 match stmt. node {
669670 StmtKind :: Semi ( ref e, ..) | StmtKind :: Expr ( ref e, ..) => Some ( e) ,
670- StmtKind :: Decl ( ref d, ..) => decl_to_expr ( d) ,
671- }
672- }
673-
674- fn decl_to_expr ( decl : & Decl ) -> Option < & Expr > {
675- match decl. node {
676- DeclKind :: Local ( ref local) => local. init . as_ref ( ) . map ( |p| & * * p) ,
671+ StmtKind :: Local ( ref local) => local. init . as_ref ( ) . map ( |p| & * * p) ,
677672 _ => None ,
678673 }
679674}
@@ -941,8 +936,8 @@ fn get_indexed_assignments<'a, 'tcx>(
941936 stmts
942937 . iter ( )
943938 . map ( |stmt| match stmt. node {
944- StmtKind :: Decl ( ..) => None ,
945- StmtKind :: Expr ( ref e, _node_id ) | StmtKind :: Semi ( ref e, _node_id ) => Some ( get_assignment ( cx, e, var) ) ,
939+ StmtKind :: Local ( .. ) | StmtKind :: Item ( ..) => None ,
940+ StmtKind :: Expr ( ref e) | StmtKind :: Semi ( ref e) => Some ( get_assignment ( cx, e, var) ) ,
946941 } )
947942 . chain ( expr. as_ref ( ) . into_iter ( ) . map ( |e| Some ( get_assignment ( cx, & * e, var) ) ) )
948943 . filter_map ( |op| op)
@@ -1888,6 +1883,19 @@ impl<'a, 'tcx> Visitor<'tcx> for VarVisitor<'a, 'tcx> {
18881883 }
18891884}
18901885
1886+ fn is_used_inside < ' a , ' tcx : ' a > ( cx : & ' a LateContext < ' a , ' tcx > , expr : & ' tcx Expr , container : & ' tcx Expr ) -> bool {
1887+ let def_id = match var_def_id ( cx, expr) {
1888+ Some ( id) => id,
1889+ None => return false ,
1890+ } ;
1891+ if let Some ( used_mutably) = mutated_variables ( container, cx) {
1892+ if used_mutably. contains ( & def_id) {
1893+ return true ;
1894+ }
1895+ }
1896+ false
1897+ }
1898+
18911899fn is_iterator_used_after_while_let < ' a , ' tcx : ' a > ( cx : & LateContext < ' a , ' tcx > , iter_expr : & ' tcx Expr ) -> bool {
18921900 let def_id = match var_def_id ( cx, iter_expr) {
18931901 Some ( id) => id,
@@ -1962,13 +1970,9 @@ fn extract_expr_from_first_stmt(block: &Block) -> Option<&Expr> {
19621970 if block. stmts . is_empty ( ) {
19631971 return None ;
19641972 }
1965- if let StmtKind :: Decl ( ref decl, _) = block. stmts [ 0 ] . node {
1966- if let DeclKind :: Local ( ref local) = decl. node {
1967- if let Some ( ref expr) = local. init {
1968- Some ( expr)
1969- } else {
1970- None
1971- }
1973+ if let StmtKind :: Local ( ref local) = block. stmts [ 0 ] . node {
1974+ if let Some ( ref expr) = local. init {
1975+ Some ( expr)
19721976 } else {
19731977 None
19741978 }
@@ -1982,8 +1986,8 @@ fn extract_first_expr(block: &Block) -> Option<&Expr> {
19821986 match block. expr {
19831987 Some ( ref expr) if block. stmts . is_empty ( ) => Some ( expr) ,
19841988 None if !block. stmts . is_empty ( ) => match block. stmts [ 0 ] . node {
1985- StmtKind :: Expr ( ref expr, _ ) | StmtKind :: Semi ( ref expr, _ ) => Some ( expr) ,
1986- StmtKind :: Decl ( ..) => None ,
1989+ StmtKind :: Expr ( ref expr) | StmtKind :: Semi ( ref expr) => Some ( expr) ,
1990+ StmtKind :: Local ( .. ) | StmtKind :: Item ( ..) => None ,
19871991 } ,
19881992 _ => None ,
19891993 }
@@ -2081,9 +2085,9 @@ struct InitializeVisitor<'a, 'tcx: 'a> {
20812085}
20822086
20832087impl < ' a , ' tcx > Visitor < ' tcx > for InitializeVisitor < ' a , ' tcx > {
2084- fn visit_decl ( & mut self , decl : & ' tcx Decl ) {
2088+ fn visit_stmt ( & mut self , stmt : & ' tcx Stmt ) {
20852089 // Look for declarations of the variable
2086- if let DeclKind :: Local ( ref local) = decl . node {
2090+ if let StmtKind :: Local ( ref local) = stmt . node {
20872091 if local. pat . id == self . var_id {
20882092 if let PatKind :: Binding ( _, _, ident, _) = local. pat . node {
20892093 self . name = Some ( ident. name ) ;
@@ -2100,7 +2104,7 @@ impl<'a, 'tcx> Visitor<'tcx> for InitializeVisitor<'a, 'tcx> {
21002104 }
21012105 }
21022106 }
2103- walk_decl ( self , decl ) ;
2107+ walk_stmt ( self , stmt ) ;
21042108 }
21052109
21062110 fn visit_expr ( & mut self , expr : & ' tcx Expr ) {
@@ -2247,7 +2251,7 @@ struct LoopNestVisitor {
22472251
22482252impl < ' tcx > Visitor < ' tcx > for LoopNestVisitor {
22492253 fn visit_stmt ( & mut self , stmt : & ' tcx Stmt ) {
2250- if stmt. node . id ( ) == self . id {
2254+ if stmt. id == self . id {
22512255 self . nesting = LookFurther ;
22522256 } else if self . nesting == Unknown {
22532257 walk_stmt ( self , stmt) ;
0 commit comments