@@ -10,6 +10,7 @@ use rustc_hir::{ExprKind, HirId, Node, PatKind, Path, QPath};
1010use rustc_lint:: LateContext ;
1111use rustc_middle:: hir:: nested_filter;
1212use rustc_span:: { sym, Span } ;
13+ use std:: ops:: ControlFlow ;
1314
1415use super :: MAP_UNWRAP_OR ;
1516
@@ -54,15 +55,14 @@ pub(super) fn check<'tcx>(
5455 let mut reference_visitor = ReferenceVisitor {
5556 cx,
5657 identifiers : unwrap_visitor. identifiers ,
57- found_reference : false ,
5858 unwrap_or_span : unwrap_arg. span ,
5959 } ;
6060
6161 let map = cx. tcx . hir ( ) ;
6262 let body = map. body_owned_by ( map. enclosing_body_owner ( expr. hir_id ) ) ;
63- reference_visitor. visit_body ( body) ;
6463
65- if reference_visitor. found_reference {
64+ // Visit the body, and return if we've found a reference
65+ if reference_visitor. visit_body ( body) . is_break ( ) {
6666 return ;
6767 }
6868 }
@@ -151,29 +151,27 @@ impl<'a, 'tcx> Visitor<'tcx> for UnwrapVisitor<'a, 'tcx> {
151151struct ReferenceVisitor < ' a , ' tcx > {
152152 cx : & ' a LateContext < ' tcx > ,
153153 identifiers : FxHashSet < HirId > ,
154- found_reference : bool ,
155154 unwrap_or_span : Span ,
156155}
157156
158157impl < ' a , ' tcx > Visitor < ' tcx > for ReferenceVisitor < ' a , ' tcx > {
159158 type NestedFilter = nested_filter:: All ;
160- fn visit_expr ( & mut self , expr : & ' tcx rustc_hir:: Expr < ' _ > ) {
159+ type Result = ControlFlow < ( ) > ;
160+ fn visit_expr ( & mut self , expr : & ' tcx rustc_hir:: Expr < ' _ > ) -> ControlFlow < ( ) > {
161161 // If we haven't found a reference yet, check if this references
162162 // one of the locals that was moved in the `unwrap_or` argument.
163163 // We are only interested in exprs that appear before the `unwrap_or` call.
164- if !self . found_reference {
165- if expr. span < self . unwrap_or_span
166- && let ExprKind :: Path ( ref path) = expr. kind
167- && let QPath :: Resolved ( _, path) = path
168- && let Res :: Local ( local_id) = path. res
169- && let Node :: Pat ( pat) = self . cx . tcx . hir_node ( local_id)
170- && let PatKind :: Binding ( _, local_id, ..) = pat. kind
171- && self . identifiers . contains ( & local_id)
172- {
173- self . found_reference = true ;
174- }
175- rustc_hir:: intravisit:: walk_expr ( self , expr) ;
164+ if expr. span < self . unwrap_or_span
165+ && let ExprKind :: Path ( ref path) = expr. kind
166+ && let QPath :: Resolved ( _, path) = path
167+ && let Res :: Local ( local_id) = path. res
168+ && let Node :: Pat ( pat) = self . cx . tcx . hir_node ( local_id)
169+ && let PatKind :: Binding ( _, local_id, ..) = pat. kind
170+ && self . identifiers . contains ( & local_id)
171+ {
172+ return ControlFlow :: Break ( ( ) ) ;
176173 }
174+ rustc_hir:: intravisit:: walk_expr ( self , expr)
177175 }
178176
179177 fn nested_visit_map ( & mut self ) -> Self :: Map {
0 commit comments