@@ -10,6 +10,7 @@ use rustc_hir::{ExprKind, HirId, Node, PatKind, Path, QPath};
10
10
use rustc_lint:: LateContext ;
11
11
use rustc_middle:: hir:: nested_filter;
12
12
use rustc_span:: { sym, Span } ;
13
+ use std:: ops:: ControlFlow ;
13
14
14
15
use super :: MAP_UNWRAP_OR ;
15
16
@@ -54,15 +55,14 @@ pub(super) fn check<'tcx>(
54
55
let mut reference_visitor = ReferenceVisitor {
55
56
cx,
56
57
identifiers : unwrap_visitor. identifiers ,
57
- found_reference : false ,
58
58
unwrap_or_span : unwrap_arg. span ,
59
59
} ;
60
60
61
61
let map = cx. tcx . hir ( ) ;
62
62
let body = map. body_owned_by ( map. enclosing_body_owner ( expr. hir_id ) ) ;
63
- reference_visitor. visit_body ( body) ;
64
63
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 ( ) {
66
66
return ;
67
67
}
68
68
}
@@ -151,29 +151,27 @@ impl<'a, 'tcx> Visitor<'tcx> for UnwrapVisitor<'a, 'tcx> {
151
151
struct ReferenceVisitor < ' a , ' tcx > {
152
152
cx : & ' a LateContext < ' tcx > ,
153
153
identifiers : FxHashSet < HirId > ,
154
- found_reference : bool ,
155
154
unwrap_or_span : Span ,
156
155
}
157
156
158
157
impl < ' a , ' tcx > Visitor < ' tcx > for ReferenceVisitor < ' a , ' tcx > {
159
158
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 < ( ) > {
161
161
// If we haven't found a reference yet, check if this references
162
162
// one of the locals that was moved in the `unwrap_or` argument.
163
163
// 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 ( ( ) ) ;
176
173
}
174
+ rustc_hir:: intravisit:: walk_expr ( self , expr)
177
175
}
178
176
179
177
fn nested_visit_map ( & mut self ) -> Self :: Map {
0 commit comments