@@ -2,9 +2,10 @@ use clippy_utils::{
22 diagnostics:: { span_lint, span_lint_and_sugg} ,
33 higher:: { get_vec_init_kind, VecInitKind } ,
44 source:: snippet,
5- visitors:: expr_visitor_no_bodies ,
5+ visitors:: for_each_expr ,
66} ;
7- use hir:: { intravisit:: Visitor , ExprKind , Local , PatKind , PathSegment , QPath , StmtKind } ;
7+ use core:: ops:: ControlFlow ;
8+ use hir:: { Expr , ExprKind , Local , PatKind , PathSegment , QPath , StmtKind } ;
89use rustc_errors:: Applicability ;
910use rustc_hir as hir;
1011use rustc_lint:: { LateContext , LateLintPass } ;
@@ -58,9 +59,7 @@ impl<'tcx> LateLintPass<'tcx> for ReadZeroByteVec {
5859 && let PatKind :: Binding ( _, _, ident, _) = pat. kind
5960 && let Some ( vec_init_kind) = get_vec_init_kind ( cx, init)
6061 {
61- // finds use of `_.read(&mut v)`
62- let mut read_found = false ;
63- let mut visitor = expr_visitor_no_bodies ( |expr| {
62+ let visitor = |expr : & Expr < ' _ > | {
6463 if let ExprKind :: MethodCall ( path, [ _self, arg] , _) = expr. kind
6564 && let PathSegment { ident : read_or_read_exact, .. } = * path
6665 && matches ! ( read_or_read_exact. as_str( ) , "read" | "read_exact" )
@@ -69,27 +68,22 @@ impl<'tcx> LateLintPass<'tcx> for ReadZeroByteVec {
6968 && let [ inner_seg] = inner_path. segments
7069 && ident. name == inner_seg. ident . name
7170 {
72- read_found = true ;
71+ ControlFlow :: Break ( ( ) )
72+ } else {
73+ ControlFlow :: Continue ( ( ) )
7374 }
74- !read_found
75- } ) ;
75+ } ;
7676
77- let next_stmt_span;
78- if idx == block. stmts . len ( ) - 1 {
77+ let ( read_found, next_stmt_span) =
78+ if let Some ( next_stmt) = block. stmts . get ( idx + 1 ) {
79+ // case { .. stmt; stmt; .. }
80+ ( for_each_expr ( next_stmt, visitor) . is_some ( ) , next_stmt. span )
81+ } else if let Some ( e) = block. expr {
7982 // case { .. stmt; expr }
80- if let Some ( e) = block. expr {
81- visitor. visit_expr ( e) ;
82- next_stmt_span = e. span ;
83- } else {
84- return ;
85- }
83+ ( for_each_expr ( e, visitor) . is_some ( ) , e. span )
8684 } else {
87- // case { .. stmt; stmt; .. }
88- let next_stmt = & block. stmts [ idx + 1 ] ;
89- visitor. visit_stmt ( next_stmt) ;
90- next_stmt_span = next_stmt. span ;
91- }
92- drop ( visitor) ;
85+ return
86+ } ;
9387
9488 if read_found && !next_stmt_span. from_expansion ( ) {
9589 let applicability = Applicability :: MaybeIncorrect ;
0 commit comments