@@ -3,7 +3,7 @@ use if_chain::if_chain;
3
3
use itertools:: Itertools ;
4
4
use rustc:: hir:: def:: Def ;
5
5
use 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 } ;
7
7
use rustc:: hir:: * ;
8
8
use rustc:: lint:: { in_external_macro, LateContext , LateLintPass , LintArray , LintContext , LintPass } ;
9
9
use rustc:: middle:: region;
@@ -565,6 +565,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
565
565
&& lhs_constructor. ident . name == "Some"
566
566
&& ( pat_args. is_empty ( )
567
567
|| !is_refutable ( cx, & pat_args[ 0 ] )
568
+ && !is_used_inside ( cx, iter_expr, & arms[ 0 ] . body )
568
569
&& !is_iterator_used_after_while_let ( cx, iter_expr)
569
570
&& !is_nested ( cx, expr, & method_args[ 0 ] ) )
570
571
{
@@ -596,7 +597,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
596
597
}
597
598
598
599
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 {
600
601
if let ExprKind :: MethodCall ( ref method, _, ref args) = expr. node {
601
602
if args. len ( ) == 1 && method. ident . name == "collect" && match_trait_method ( cx, expr, & paths:: ITERATOR ) {
602
603
span_lint (
@@ -667,13 +668,7 @@ fn never_loop_block(block: &Block, main_loop_id: NodeId) -> NeverLoopResult {
667
668
fn stmt_to_expr ( stmt : & Stmt ) -> Option < & Expr > {
668
669
match stmt. node {
669
670
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) ,
677
672
_ => None ,
678
673
}
679
674
}
@@ -941,8 +936,8 @@ fn get_indexed_assignments<'a, 'tcx>(
941
936
stmts
942
937
. iter ( )
943
938
. 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) ) ,
946
941
} )
947
942
. chain ( expr. as_ref ( ) . into_iter ( ) . map ( |e| Some ( get_assignment ( cx, & * e, var) ) ) )
948
943
. filter_map ( |op| op)
@@ -1888,6 +1883,19 @@ impl<'a, 'tcx> Visitor<'tcx> for VarVisitor<'a, 'tcx> {
1888
1883
}
1889
1884
}
1890
1885
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
+
1891
1899
fn is_iterator_used_after_while_let < ' a , ' tcx : ' a > ( cx : & LateContext < ' a , ' tcx > , iter_expr : & ' tcx Expr ) -> bool {
1892
1900
let def_id = match var_def_id ( cx, iter_expr) {
1893
1901
Some ( id) => id,
@@ -1962,13 +1970,9 @@ fn extract_expr_from_first_stmt(block: &Block) -> Option<&Expr> {
1962
1970
if block. stmts . is_empty ( ) {
1963
1971
return None ;
1964
1972
}
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)
1972
1976
} else {
1973
1977
None
1974
1978
}
@@ -1982,8 +1986,8 @@ fn extract_first_expr(block: &Block) -> Option<&Expr> {
1982
1986
match block. expr {
1983
1987
Some ( ref expr) if block. stmts . is_empty ( ) => Some ( expr) ,
1984
1988
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 ,
1987
1991
} ,
1988
1992
_ => None ,
1989
1993
}
@@ -2081,9 +2085,9 @@ struct InitializeVisitor<'a, 'tcx: 'a> {
2081
2085
}
2082
2086
2083
2087
impl < ' 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 ) {
2085
2089
// Look for declarations of the variable
2086
- if let DeclKind :: Local ( ref local) = decl . node {
2090
+ if let StmtKind :: Local ( ref local) = stmt . node {
2087
2091
if local. pat . id == self . var_id {
2088
2092
if let PatKind :: Binding ( _, _, ident, _) = local. pat . node {
2089
2093
self . name = Some ( ident. name ) ;
@@ -2100,7 +2104,7 @@ impl<'a, 'tcx> Visitor<'tcx> for InitializeVisitor<'a, 'tcx> {
2100
2104
}
2101
2105
}
2102
2106
}
2103
- walk_decl ( self , decl ) ;
2107
+ walk_stmt ( self , stmt ) ;
2104
2108
}
2105
2109
2106
2110
fn visit_expr ( & mut self , expr : & ' tcx Expr ) {
@@ -2247,7 +2251,7 @@ struct LoopNestVisitor {
2247
2251
2248
2252
impl < ' tcx > Visitor < ' tcx > for LoopNestVisitor {
2249
2253
fn visit_stmt ( & mut self , stmt : & ' tcx Stmt ) {
2250
- if stmt. node . id ( ) == self . id {
2254
+ if stmt. id == self . id {
2251
2255
self . nesting = LookFurther ;
2252
2256
} else if self . nesting == Unknown {
2253
2257
walk_stmt ( self , stmt) ;
0 commit comments