@@ -71,15 +71,22 @@ impl<'tcx> LateLintPass<'tcx> for UnusedPeekable {
7171 return ;
7272 }
7373
74+ let mut found_peek_call = false ;
75+
7476 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+ }
7681 }
7782
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+ }
8087 }
8188
82- if !vis . found_peek_call {
89+ if !found_peek_call {
8390 span_lint_hir_and_then (
8491 cx,
8592 UNUSED_PEEKABLE ,
@@ -99,16 +106,11 @@ impl<'tcx> LateLintPass<'tcx> for UnusedPeekable {
99106struct PeekableVisitor < ' a , ' tcx > {
100107 cx : & ' a LateContext < ' tcx > ,
101108 expected_hir_id : HirId ,
102- found_peek_call : bool ,
103109}
104110
105111impl < ' a , ' tcx > PeekableVisitor < ' a , ' tcx > {
106112 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 }
112114 }
113115}
114116
@@ -139,7 +141,6 @@ impl<'tcx> Visitor<'tcx> for PeekableVisitor<'_, 'tcx> {
139141 }
140142
141143 if args. iter ( ) . any ( |arg| arg_is_mut_peekable ( self . cx , arg) ) {
142- self . found_peek_call = true ;
143144 return ControlFlow :: Break ( ( ) ) ;
144145 }
145146
@@ -161,15 +162,13 @@ impl<'tcx> Visitor<'tcx> for PeekableVisitor<'_, 'tcx> {
161162 if matches ! ( method_name, "peek" | "peek_mut" | "next_if" | "next_if_eq" )
162163 && arg_is_mut_peekable ( self . cx , self_arg)
163164 {
164- self . found_peek_call = true ;
165165 return ControlFlow :: Break ( ( ) ) ;
166166 }
167167
168168 // foo.some_method() excluding Iterator methods
169169 if remaining_args. iter ( ) . any ( |arg| arg_is_mut_peekable ( self . cx , arg) )
170170 && !is_trait_method ( self . cx , expr, sym:: Iterator )
171171 {
172- self . found_peek_call = true ;
173172 return ControlFlow :: Break ( ( ) ) ;
174173 }
175174
@@ -184,14 +183,12 @@ impl<'tcx> Visitor<'tcx> for PeekableVisitor<'_, 'tcx> {
184183 } ,
185184 ExprKind :: AddrOf ( _, Mutability :: Not , _) => return ControlFlow :: Continue ( ( ) ) ,
186185 _ => {
187- self . found_peek_call = true ;
188186 return ControlFlow :: Break ( ( ) ) ;
189187 } ,
190188 }
191189 } ,
192190 Node :: LetStmt ( LetStmt { init : Some ( init) , .. } ) => {
193191 if arg_is_mut_peekable ( self . cx , init) {
194- self . found_peek_call = true ;
195192 return ControlFlow :: Break ( ( ) ) ;
196193 }
197194
@@ -200,7 +197,6 @@ impl<'tcx> Visitor<'tcx> for PeekableVisitor<'_, 'tcx> {
200197 Node :: Stmt ( stmt) => {
201198 match stmt. kind {
202199 StmtKind :: Let ( _) | StmtKind :: Item ( _) => {
203- self . found_peek_call = true ;
204200 return ControlFlow :: Break ( ( ) ) ;
205201 } ,
206202 StmtKind :: Expr ( _) | StmtKind :: Semi ( _) => { } ,
0 commit comments