diff options
| author | bors <bors@rust-lang.org> | 2022-10-03 00:05:05 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-10-03 00:05:05 +0000 |
| commit | 2be6c4ae5b075028a6c61ba75daaf22fb7921c85 (patch) | |
| tree | 6bf52e692593d836420fa05c4d34706cab08d89a /clippy_utils/src/ptr.rs | |
| parent | bef93d3b1464b6d467482d47a53c62aadaa9f41c (diff) | |
| parent | 26bb36636ca9d71958055e05053946d22fcfbdbb (diff) | |
Auto merge of #8762 - Jarcho:visitor, r=Jarcho
Replace `expr_visitor` with `for_each_expr` This is a minor change which uses `ControlFlow` rather than a boolean. This also runs the visitor rather than returning the visitor, which results in a small readability win as well. changelog: None
Diffstat (limited to 'clippy_utils/src/ptr.rs')
| -rw-r--r-- | clippy_utils/src/ptr.rs | 37 |
1 files changed, 16 insertions, 21 deletions
diff --git a/clippy_utils/src/ptr.rs b/clippy_utils/src/ptr.rs index 0226f74906b..88837d8a143 100644 --- a/clippy_utils/src/ptr.rs +++ b/clippy_utils/src/ptr.rs @@ -1,7 +1,7 @@ use crate::source::snippet; -use crate::visitors::expr_visitor_no_bodies; +use crate::visitors::{for_each_expr, Descend}; use crate::{path_to_local_id, strip_pat_refs}; -use rustc_hir::intravisit::Visitor; +use core::ops::ControlFlow; use rustc_hir::{Body, BodyId, ExprKind, HirId, PatKind}; use rustc_lint::LateContext; use rustc_span::Span; @@ -30,28 +30,23 @@ fn extract_clone_suggestions<'tcx>( replace: &[(&'static str, &'static str)], body: &'tcx Body<'_>, ) -> Option<Vec<(Span, Cow<'static, str>)>> { - let mut abort = false; let mut spans = Vec::new(); - expr_visitor_no_bodies(|expr| { - if abort { - return false; - } - if let ExprKind::MethodCall(seg, recv, [], _) = expr.kind { - if path_to_local_id(recv, id) { - if seg.ident.name.as_str() == "capacity" { - abort = true; - return false; - } - for &(fn_name, suffix) in replace { - if seg.ident.name.as_str() == fn_name { - spans.push((expr.span, snippet(cx, recv.span, "_") + suffix)); - return false; - } + for_each_expr(body, |e| { + if let ExprKind::MethodCall(seg, recv, [], _) = e.kind + && path_to_local_id(recv, id) + { + if seg.ident.as_str() == "capacity" { + return ControlFlow::Break(()); + } + for &(fn_name, suffix) in replace { + if seg.ident.as_str() == fn_name { + spans.push((e.span, snippet(cx, recv.span, "_") + suffix)); + return ControlFlow::Continue(Descend::No); } } } - !abort + ControlFlow::Continue(Descend::Yes) }) - .visit_body(body); - if abort { None } else { Some(spans) } + .is_none() + .then_some(spans) } |
