@@ -71,15 +71,22 @@ impl<'tcx> LateLintPass<'tcx> for UnusedPeekable {
71
71
return ;
72
72
}
73
73
74
+ let mut found_peek_call = false ;
75
+
74
76
for stmt in & block. stmts [ idx..] {
75
- vis. visit_stmt ( stmt) ;
77
+ if vis. visit_stmt ( stmt) . is_break ( ) {
78
+ found_peek_call = true ;
79
+ break ;
80
+ }
76
81
}
77
82
78
- if let Some ( expr) = block. expr {
79
- vis. visit_expr ( expr) ;
83
+ if !found_peek_call && let Some ( expr) = block. expr {
84
+ if vis. visit_expr ( expr) . is_break ( ) {
85
+ found_peek_call = true
86
+ }
80
87
}
81
88
82
- if !vis . found_peek_call {
89
+ if !found_peek_call {
83
90
span_lint_hir_and_then (
84
91
cx,
85
92
UNUSED_PEEKABLE ,
@@ -99,16 +106,11 @@ impl<'tcx> LateLintPass<'tcx> for UnusedPeekable {
99
106
struct PeekableVisitor < ' a , ' tcx > {
100
107
cx : & ' a LateContext < ' tcx > ,
101
108
expected_hir_id : HirId ,
102
- found_peek_call : bool ,
103
109
}
104
110
105
111
impl < ' a , ' tcx > PeekableVisitor < ' a , ' tcx > {
106
112
fn new ( cx : & ' a LateContext < ' tcx > , expected_hir_id : HirId ) -> Self {
107
- Self {
108
- cx,
109
- expected_hir_id,
110
- found_peek_call : false ,
111
- }
113
+ Self { cx, expected_hir_id }
112
114
}
113
115
}
114
116
@@ -139,7 +141,6 @@ impl<'tcx> Visitor<'tcx> for PeekableVisitor<'_, 'tcx> {
139
141
}
140
142
141
143
if args. iter ( ) . any ( |arg| arg_is_mut_peekable ( self . cx , arg) ) {
142
- self . found_peek_call = true ;
143
144
return ControlFlow :: Break ( ( ) ) ;
144
145
}
145
146
@@ -161,15 +162,13 @@ impl<'tcx> Visitor<'tcx> for PeekableVisitor<'_, 'tcx> {
161
162
if matches ! ( method_name, "peek" | "peek_mut" | "next_if" | "next_if_eq" )
162
163
&& arg_is_mut_peekable ( self . cx , self_arg)
163
164
{
164
- self . found_peek_call = true ;
165
165
return ControlFlow :: Break ( ( ) ) ;
166
166
}
167
167
168
168
// foo.some_method() excluding Iterator methods
169
169
if remaining_args. iter ( ) . any ( |arg| arg_is_mut_peekable ( self . cx , arg) )
170
170
&& !is_trait_method ( self . cx , expr, sym:: Iterator )
171
171
{
172
- self . found_peek_call = true ;
173
172
return ControlFlow :: Break ( ( ) ) ;
174
173
}
175
174
@@ -184,14 +183,12 @@ impl<'tcx> Visitor<'tcx> for PeekableVisitor<'_, 'tcx> {
184
183
} ,
185
184
ExprKind :: AddrOf ( _, Mutability :: Not , _) => return ControlFlow :: Continue ( ( ) ) ,
186
185
_ => {
187
- self . found_peek_call = true ;
188
186
return ControlFlow :: Break ( ( ) ) ;
189
187
} ,
190
188
}
191
189
} ,
192
190
Node :: LetStmt ( LetStmt { init : Some ( init) , .. } ) => {
193
191
if arg_is_mut_peekable ( self . cx , init) {
194
- self . found_peek_call = true ;
195
192
return ControlFlow :: Break ( ( ) ) ;
196
193
}
197
194
@@ -200,7 +197,6 @@ impl<'tcx> Visitor<'tcx> for PeekableVisitor<'_, 'tcx> {
200
197
Node :: Stmt ( stmt) => {
201
198
match stmt. kind {
202
199
StmtKind :: Let ( _) | StmtKind :: Item ( _) => {
203
- self . found_peek_call = true ;
204
200
return ControlFlow :: Break ( ( ) ) ;
205
201
} ,
206
202
StmtKind :: Expr ( _) | StmtKind :: Semi ( _) => { } ,
0 commit comments