Skip to content

Commit

Permalink
Replace expr_visitor with for_each_expr
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarcho committed Sep 10, 2022
1 parent c8c2a23 commit ef1afe4
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 268 deletions.
12 changes: 6 additions & 6 deletions clippy_lints/src/implicit_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use clippy_utils::{
diagnostics::span_lint_hir_and_then,
get_async_fn_body, is_async_fn,
source::{snippet_with_applicability, snippet_with_context, walk_span_to_context},
visitors::expr_visitor_no_bodies,
visitors::for_each_expr,
};
use core::ops::ControlFlow;
use rustc_errors::Applicability;
use rustc_hir::intravisit::{FnKind, Visitor};
use rustc_hir::intravisit::FnKind;
use rustc_hir::{Block, Body, Expr, ExprKind, FnDecl, FnRetTy, HirId};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
Expand Down Expand Up @@ -152,7 +153,7 @@ fn lint_implicit_returns(

ExprKind::Loop(block, ..) => {
let mut add_return = false;
expr_visitor_no_bodies(|e| {
let _: Option<!> = for_each_expr(block, |e| {
if let ExprKind::Break(dest, sub_expr) = e.kind {
if dest.target_id.ok() == Some(expr.hir_id) {
if call_site_span.is_none() && e.span.ctxt() == ctxt {
Expand All @@ -167,9 +168,8 @@ fn lint_implicit_returns(
}
}
}
true
})
.visit_block(block);
ControlFlow::Continue(())
});
if add_return {
#[expect(clippy::option_if_let_else)]
if let Some(span) = call_site_span {
Expand Down
20 changes: 9 additions & 11 deletions clippy_lints/src/methods/str_splitn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use clippy_utils::consts::{constant, Constant};
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
use clippy_utils::source::snippet_with_context;
use clippy_utils::usage::local_used_after_expr;
use clippy_utils::visitors::expr_visitor;
use clippy_utils::visitors::{for_each_expr_with_closures, Descend};
use clippy_utils::{is_diag_item_method, match_def_path, meets_msrv, msrvs, path_to_local_id, paths};
use core::ops::ControlFlow;
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::intravisit::Visitor;
use rustc_hir::{
BindingAnnotation, Expr, ExprKind, HirId, LangItem, Local, MatchSource, Node, Pat, PatKind, QPath, Stmt, StmtKind,
};
Expand Down Expand Up @@ -211,7 +211,7 @@ fn indirect_usage<'tcx>(
binding: HirId,
ctxt: SyntaxContext,
) -> Option<IndirectUsage<'tcx>> {
if let StmtKind::Local(Local {
if let StmtKind::Local(&Local {
pat: Pat {
kind: PatKind::Binding(BindingAnnotation::NONE, _, ident, None),
..
Expand All @@ -222,14 +222,12 @@ fn indirect_usage<'tcx>(
}) = stmt.kind
{
let mut path_to_binding = None;
expr_visitor(cx, |expr| {
if path_to_local_id(expr, binding) {
path_to_binding = Some(expr);
let _: Option<!> = for_each_expr_with_closures(cx, init_expr, |e| {
if path_to_local_id(e, binding) {
path_to_binding = Some(e);
}

path_to_binding.is_none()
})
.visit_expr(init_expr);
ControlFlow::Continue(Descend::from(path_to_binding.is_none()))
});

let mut parents = cx.tcx.hir().parent_iter(path_to_binding?.hir_id);
let iter_usage = parse_iter_usage(cx, ctxt, &mut parents)?;
Expand All @@ -250,7 +248,7 @@ fn indirect_usage<'tcx>(
..
} = iter_usage
{
if parent_id == *local_hir_id {
if parent_id == local_hir_id {
return Some(IndirectUsage {
name: ident.name,
span: stmt.span,
Expand Down
34 changes: 14 additions & 20 deletions clippy_lints/src/needless_late_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::path_to_local;
use clippy_utils::source::snippet_opt;
use clippy_utils::ty::needs_ordered_drop;
use clippy_utils::visitors::{expr_visitor, expr_visitor_no_bodies, is_local_used};
use clippy_utils::visitors::{for_each_expr, for_each_expr_with_closures, is_local_used};
use core::ops::ControlFlow;
use rustc_errors::{Applicability, MultiSpan};
use rustc_hir::intravisit::Visitor;
use rustc_hir::{
BindingAnnotation, Block, Expr, ExprKind, HirId, Local, LocalSource, MatchSource, Node, Pat, PatKind, Stmt,
StmtKind,
Expand Down Expand Up @@ -64,31 +64,25 @@ declare_clippy_lint! {
declare_lint_pass!(NeedlessLateInit => [NEEDLESS_LATE_INIT]);

fn contains_assign_expr<'tcx>(cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'tcx>) -> bool {
let mut seen = false;
expr_visitor(cx, |expr| {
if let ExprKind::Assign(..) = expr.kind {
seen = true;
for_each_expr_with_closures(cx, stmt, |e| {
if matches!(e.kind, ExprKind::Assign(..)) {
ControlFlow::Break(())
} else {
ControlFlow::Continue(())
}

!seen
})
.visit_stmt(stmt);

seen
.is_some()
}

fn contains_let(cond: &Expr<'_>) -> bool {
let mut seen = false;
expr_visitor_no_bodies(|expr| {
if let ExprKind::Let(_) = expr.kind {
seen = true;
for_each_expr(cond, |e| {
if matches!(e.kind, ExprKind::Let(_)) {
ControlFlow::Break(())
} else {
ControlFlow::Continue(())
}

!seen
})
.visit_expr(cond);

seen
.is_some()
}

fn stmt_needs_ordered_drop(cx: &LateContext<'_>, stmt: &Stmt<'_>) -> bool {
Expand Down
19 changes: 11 additions & 8 deletions clippy_lints/src/panic_in_result_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::macros::root_macro_call_first_node;
use clippy_utils::return_ty;
use clippy_utils::ty::is_type_diagnostic_item;
use clippy_utils::visitors::expr_visitor_no_bodies;
use clippy_utils::visitors::{for_each_expr, Descend};
use core::ops::ControlFlow;
use rustc_hir as hir;
use rustc_hir::intravisit::{FnKind, Visitor};
use rustc_hir::intravisit::FnKind;
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::{sym, Span};
Expand Down Expand Up @@ -58,18 +59,20 @@ impl<'tcx> LateLintPass<'tcx> for PanicInResultFn {

fn lint_impl_body<'tcx>(cx: &LateContext<'tcx>, impl_span: Span, body: &'tcx hir::Body<'tcx>) {
let mut panics = Vec::new();
expr_visitor_no_bodies(|expr| {
let Some(macro_call) = root_macro_call_first_node(cx, expr) else { return true };
let _: Option<!> = for_each_expr(body.value, |e| {
let Some(macro_call) = root_macro_call_first_node(cx, e) else {
return ControlFlow::Continue(Descend::Yes);
};
if matches!(
cx.tcx.item_name(macro_call.def_id).as_str(),
"unimplemented" | "unreachable" | "panic" | "todo" | "assert" | "assert_eq" | "assert_ne"
) {
panics.push(macro_call.span);
return false;
ControlFlow::Continue(Descend::No)
} else {
ControlFlow::Continue(Descend::Yes)
}
true
})
.visit_expr(body.value);
});
if !panics.is_empty() {
span_lint_and_then(
cx,
Expand Down
40 changes: 17 additions & 23 deletions clippy_lints/src/read_zero_byte_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use clippy_utils::{
diagnostics::{span_lint, span_lint_and_sugg},
higher::{get_vec_init_kind, VecInitKind},
source::snippet,
visitors::expr_visitor_no_bodies,
visitors::for_each_expr,
};
use hir::{intravisit::Visitor, ExprKind, Local, PatKind, PathSegment, QPath, StmtKind};
use core::ops::ControlFlow;
use hir::{Expr, ExprKind, Local, PatKind, PathSegment, QPath, StmtKind};
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_lint::{LateContext, LateLintPass};
Expand Down Expand Up @@ -58,38 +59,31 @@ impl<'tcx> LateLintPass<'tcx> for ReadZeroByteVec {
&& let PatKind::Binding(_, _, ident, _) = pat.kind
&& let Some(vec_init_kind) = get_vec_init_kind(cx, init)
{
// finds use of `_.read(&mut v)`
let mut read_found = false;
let mut visitor = expr_visitor_no_bodies(|expr| {
if let ExprKind::MethodCall(path, _self, [arg], _) = expr.kind
let visitor = |expr: &Expr<'_>| {
if let ExprKind::MethodCall(path, _, [arg], _) = expr.kind
&& let PathSegment { ident: read_or_read_exact, .. } = *path
&& matches!(read_or_read_exact.as_str(), "read" | "read_exact")
&& let ExprKind::AddrOf(_, hir::Mutability::Mut, inner) = arg.kind
&& let ExprKind::Path(QPath::Resolved(None, inner_path)) = inner.kind
&& let [inner_seg] = inner_path.segments
&& ident.name == inner_seg.ident.name
{
read_found = true;
ControlFlow::Break(())
} else {
ControlFlow::Continue(())
}
!read_found
});
};

let next_stmt_span;
if idx == block.stmts.len() - 1 {
let (read_found, next_stmt_span) =
if let Some(next_stmt) = block.stmts.get(idx + 1) {
// case { .. stmt; stmt; .. }
(for_each_expr(next_stmt, visitor).is_some(), next_stmt.span)
} else if let Some(e) = block.expr {
// case { .. stmt; expr }
if let Some(e) = block.expr {
visitor.visit_expr(e);
next_stmt_span = e.span;
} else {
return;
}
(for_each_expr(e, visitor).is_some(), e.span)
} else {
// case { .. stmt; stmt; .. }
let next_stmt = &block.stmts[idx + 1];
visitor.visit_stmt(next_stmt);
next_stmt_span = next_stmt.span;
}
drop(visitor);
return
};

if read_found && !next_stmt_span.from_expansion() {
let applicability = Applicability::MaybeIncorrect;
Expand Down
19 changes: 9 additions & 10 deletions clippy_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#![feature(let_else)]
#![feature(let_chains)]
#![feature(lint_reasons)]
#![feature(never_type)]
#![feature(once_cell)]
#![feature(rustc_private)]
#![recursion_limit = "512"]
Expand Down Expand Up @@ -66,6 +67,7 @@ pub use self::hir_utils::{
both, count_eq, eq_expr_value, hash_expr, hash_stmt, over, HirEqInterExpr, SpanlessEq, SpanlessHash,
};

use core::ops::ControlFlow;
use std::collections::hash_map::Entry;
use std::hash::BuildHasherDefault;
use std::sync::OnceLock;
Expand Down Expand Up @@ -114,7 +116,7 @@ use rustc_target::abi::Integer;

use crate::consts::{constant, Constant};
use crate::ty::{can_partially_move_ty, expr_sig, is_copy, is_recursively_primitive_type, ty_is_fn_once_param};
use crate::visitors::expr_visitor_no_bodies;
use crate::visitors::for_each_expr;

pub fn parse_msrv(msrv: &str, sess: Option<&Session>, span: Option<Span>) -> Option<RustcVersion> {
if let Ok(version) = RustcVersion::parse(msrv) {
Expand Down Expand Up @@ -1137,17 +1139,14 @@ pub fn contains_name(name: Symbol, expr: &Expr<'_>) -> bool {

/// Returns `true` if `expr` contains a return expression
pub fn contains_return(expr: &hir::Expr<'_>) -> bool {
let mut found = false;
expr_visitor_no_bodies(|expr| {
if !found {
if let hir::ExprKind::Ret(..) = &expr.kind {
found = true;
}
for_each_expr(expr, |e| {
if matches!(e.kind, hir::ExprKind::Ret(..)) {
ControlFlow::Break(())
} else {
ControlFlow::Continue(())
}
!found
})
.visit_expr(expr);
found
.is_some()
}

/// Extends the span to the beginning of the spans line, incl. whitespaces.
Expand Down
Loading

0 comments on commit ef1afe4

Please sign in to comment.