Skip to content

Commit 0f87a81

Browse files
committed
Auto merge of #12822 - Alexendoo:for-each-expr, r=dswij
Make `for_each_expr` visit closures by default, rename the old version `for_each_expr_without_closures` A lot of the time `for_each_expr` is picked when closures should be visited so I think it makes sense for this to be the default with the alternative available for when you don't need to visit them. The first commit renames `for_each_expr` to `for_each_expr_without_closures` and `for_each_expr_with_closures` to `for_each_expr` The second commit switches a few uses that I caught over to include closures to fix a few bugs changelog: none
2 parents 0b441d5 + 68e7356 commit 0f87a81

40 files changed

+148
-88
lines changed

clippy_lints/src/blocks_in_conditions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg};
22
use clippy_utils::source::snippet_block_with_applicability;
33
use clippy_utils::ty::implements_trait;
4-
use clippy_utils::visitors::{for_each_expr, Descend};
4+
use clippy_utils::visitors::{for_each_expr_without_closures, Descend};
55
use clippy_utils::{get_parent_expr, higher, is_from_proc_macro};
66
use core::ops::ControlFlow;
77
use rustc_errors::Applicability;
@@ -125,7 +125,7 @@ impl<'tcx> LateLintPass<'tcx> for BlocksInConditions {
125125
}
126126
}
127127
} else {
128-
let _: Option<!> = for_each_expr(cond, |e| {
128+
let _: Option<!> = for_each_expr_without_closures(cond, |e| {
129129
if let ExprKind::Closure(closure) = e.kind {
130130
// do not lint if the closure is called using an iterator (see #1141)
131131
if let Some(parent) = get_parent_expr(cx, e)

clippy_lints/src/casts/cast_sign_loss.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::ops::ControlFlow;
33

44
use clippy_utils::consts::{constant, Constant};
55
use clippy_utils::diagnostics::span_lint;
6-
use clippy_utils::visitors::{for_each_expr, Descend};
6+
use clippy_utils::visitors::{for_each_expr_without_closures, Descend};
77
use clippy_utils::{method_chain_args, sext};
88
use rustc_hir::{BinOpKind, Expr, ExprKind};
99
use rustc_lint::LateContext;
@@ -266,7 +266,7 @@ fn expr_add_sign(cx: &LateContext<'_>, expr: &Expr<'_>) -> Sign {
266266
fn exprs_with_muldiv_binop_peeled<'e>(expr: &'e Expr<'_>) -> Vec<&'e Expr<'e>> {
267267
let mut res = vec![];
268268

269-
for_each_expr(expr, |sub_expr| -> ControlFlow<Infallible, Descend> {
269+
for_each_expr_without_closures(expr, |sub_expr| -> ControlFlow<Infallible, Descend> {
270270
// We don't check for mul/div/rem methods here, but we could.
271271
if let ExprKind::Binary(op, lhs, _rhs) = sub_expr.kind {
272272
if matches!(op.node, BinOpKind::Mul | BinOpKind::Div) {
@@ -315,7 +315,7 @@ fn exprs_with_muldiv_binop_peeled<'e>(expr: &'e Expr<'_>) -> Vec<&'e Expr<'e>> {
315315
fn exprs_with_add_binop_peeled<'e>(expr: &'e Expr<'_>) -> Vec<&'e Expr<'e>> {
316316
let mut res = vec![];
317317

318-
for_each_expr(expr, |sub_expr| -> ControlFlow<Infallible, Descend> {
318+
for_each_expr_without_closures(expr, |sub_expr| -> ControlFlow<Infallible, Descend> {
319319
// We don't check for add methods here, but we could.
320320
if let ExprKind::Binary(op, _lhs, _rhs) = sub_expr.kind {
321321
if matches!(op.node, BinOpKind::Add) {

clippy_lints/src/casts/unnecessary_cast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::numeric_literal::NumericLiteral;
33
use clippy_utils::source::snippet_opt;
4-
use clippy_utils::visitors::{for_each_expr, Visitable};
4+
use clippy_utils::visitors::{for_each_expr_without_closures, Visitable};
55
use clippy_utils::{get_parent_expr, is_hir_ty_cfg_dependant, is_ty_alias, path_to_local};
66
use rustc_ast::{LitFloatType, LitIntType, LitKind};
77
use rustc_errors::Applicability;
@@ -245,7 +245,7 @@ fn fp_ty_mantissa_nbits(typ: Ty<'_>) -> u32 {
245245
/// TODO: Maybe we should move this to `clippy_utils` so others won't need to go down this dark,
246246
/// dark path reimplementing this (or something similar).
247247
fn is_cast_from_ty_alias<'tcx>(cx: &LateContext<'tcx>, expr: impl Visitable<'tcx>, cast_from: Ty<'tcx>) -> bool {
248-
for_each_expr(expr, |expr| {
248+
for_each_expr_without_closures(expr, |expr| {
249249
// Calls are a `Path`, and usage of locals are a `Path`. So, this checks
250250
// - call() as i32
251251
// - local as i32

clippy_lints/src/cognitive_complexity.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use clippy_utils::diagnostics::span_lint_and_help;
44
use clippy_utils::source::snippet_opt;
55
use clippy_utils::ty::is_type_diagnostic_item;
6-
use clippy_utils::visitors::for_each_expr;
6+
use clippy_utils::visitors::for_each_expr_without_closures;
77
use clippy_utils::{get_async_fn_body, is_async_fn, LimitStack};
88
use core::ops::ControlFlow;
99
use rustc_ast::ast::Attribute;
@@ -65,7 +65,7 @@ impl CognitiveComplexity {
6565

6666
let mut cc = 1u64;
6767
let mut returns = 0u64;
68-
let _: Option<!> = for_each_expr(expr, |e| {
68+
let _: Option<!> = for_each_expr_without_closures(expr, |e| {
6969
match e.kind {
7070
ExprKind::If(_, _, _) => {
7171
cc += 1;

clippy_lints/src/collection_is_never_read.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint;
22
use clippy_utils::ty::{is_type_diagnostic_item, is_type_lang_item};
3-
use clippy_utils::visitors::{for_each_expr_with_closures, Visitable};
3+
use clippy_utils::visitors::{for_each_expr, Visitable};
44
use clippy_utils::{get_enclosing_block, path_to_local_id};
55
use core::ops::ControlFlow;
66
use rustc_hir::{Body, ExprKind, HirId, LangItem, LetStmt, Node, PatKind};
@@ -82,7 +82,7 @@ fn has_no_read_access<'tcx, T: Visitable<'tcx>>(cx: &LateContext<'tcx>, id: HirI
8282
let mut has_read_access = false;
8383

8484
// Inspect all expressions and sub-expressions in the block.
85-
for_each_expr_with_closures(cx, block, |expr| {
85+
for_each_expr(cx, block, |expr| {
8686
// Ignore expressions that are not simply `id`.
8787
if !path_to_local_id(expr, id) {
8888
return ControlFlow::Continue(());

clippy_lints/src/copies.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::{span_lint_and_note, span_lint_and_then};
22
use clippy_utils::source::{first_line_of_span, indent_of, reindent_multiline, snippet, snippet_opt};
33
use clippy_utils::ty::{needs_ordered_drop, InteriorMut};
4-
use clippy_utils::visitors::for_each_expr;
4+
use clippy_utils::visitors::for_each_expr_without_closures;
55
use clippy_utils::{
66
capture_local_usage, eq_expr_value, find_binding_init, get_enclosing_block, hash_expr, hash_stmt, if_sequence,
77
is_else_clause, is_lint_allowed, path_to_local, search_same, ContainsName, HirEqInterExpr, SpanlessEq,
@@ -362,7 +362,7 @@ fn eq_binding_names(s: &Stmt<'_>, names: &[(HirId, Symbol)]) -> bool {
362362

363363
/// Checks if the statement modifies or moves any of the given locals.
364364
fn modifies_any_local<'tcx>(cx: &LateContext<'tcx>, s: &'tcx Stmt<'_>, locals: &HirIdSet) -> bool {
365-
for_each_expr(s, |e| {
365+
for_each_expr_without_closures(s, |e| {
366366
if let Some(id) = path_to_local(e)
367367
&& locals.contains(&id)
368368
&& !capture_local_usage(cx, e).is_imm_ref()
@@ -413,7 +413,7 @@ fn scan_block_for_eq<'tcx>(
413413

414414
let mut cond_locals = HirIdSet::default();
415415
for &cond in conds {
416-
let _: Option<!> = for_each_expr(cond, |e| {
416+
let _: Option<!> = for_each_expr_without_closures(cond, |e| {
417417
if let Some(id) = path_to_local(e) {
418418
cond_locals.insert(id);
419419
}

clippy_lints/src/functions/must_use.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use clippy_utils::attrs::is_proc_macro;
1414
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_then};
1515
use clippy_utils::source::snippet_opt;
1616
use clippy_utils::ty::is_must_use_ty;
17-
use clippy_utils::visitors::for_each_expr;
17+
use clippy_utils::visitors::for_each_expr_without_closures;
1818
use clippy_utils::{return_ty, trait_ref_of_method};
1919

2020
use core::ops::ControlFlow;
@@ -226,7 +226,7 @@ fn is_mutated_static(e: &hir::Expr<'_>) -> bool {
226226
}
227227

228228
fn mutates_static<'tcx>(cx: &LateContext<'tcx>, body: &'tcx hir::Body<'_>) -> bool {
229-
for_each_expr(body.value, |e| {
229+
for_each_expr_without_closures(body.value, |e| {
230230
use hir::ExprKind::{AddrOf, Assign, AssignOp, Call, MethodCall};
231231

232232
match e.kind {

clippy_lints/src/functions/not_unsafe_ptr_arg_deref.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_span::def_id::LocalDefId;
55

66
use clippy_utils::diagnostics::span_lint;
77
use clippy_utils::ty::type_is_unsafe_function;
8-
use clippy_utils::visitors::for_each_expr_with_closures;
8+
use clippy_utils::visitors::for_each_expr;
99
use clippy_utils::{iter_input_pats, path_to_local};
1010

1111
use core::ops::ControlFlow;
@@ -49,7 +49,7 @@ fn check_raw_ptr<'tcx>(
4949

5050
if !raw_ptrs.is_empty() {
5151
let typeck = cx.tcx.typeck_body(body.id());
52-
let _: Option<!> = for_each_expr_with_closures(cx, body.value, |e| {
52+
let _: Option<!> = for_each_expr(cx, body.value, |e| {
5353
match e.kind {
5454
hir::ExprKind::Call(f, args) if type_is_unsafe_function(cx, typeck.expr_ty(f)) => {
5555
for arg in args {

clippy_lints/src/implicit_return.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_hir_and_then;
22
use clippy_utils::source::{snippet_with_applicability, snippet_with_context, walk_span_to_context};
3-
use clippy_utils::visitors::for_each_expr;
3+
use clippy_utils::visitors::for_each_expr_without_closures;
44
use clippy_utils::{get_async_fn_body, is_async_fn};
55
use core::ops::ControlFlow;
66
use rustc_errors::Applicability;
@@ -153,7 +153,7 @@ fn lint_implicit_returns(
153153

154154
ExprKind::Loop(block, ..) => {
155155
let mut add_return = false;
156-
let _: Option<!> = for_each_expr(block, |e| {
156+
let _: Option<!> = for_each_expr_without_closures(block, |e| {
157157
if let ExprKind::Break(dest, sub_expr) = e.kind {
158158
if dest.target_id.ok() == Some(expr.hir_id) {
159159
if call_site_span.is_none() && e.span.ctxt() == ctxt {

clippy_lints/src/matches/redundant_guards.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_config::msrvs::Msrv;
22
use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::macros::matching_root_macro_call;
44
use clippy_utils::source::snippet;
5-
use clippy_utils::visitors::{for_each_expr, is_local_used};
5+
use clippy_utils::visitors::{for_each_expr_without_closures, is_local_used};
66
use clippy_utils::{in_constant, path_to_local};
77
use rustc_ast::{BorrowKind, LitKind};
88
use rustc_errors::Applicability;
@@ -249,7 +249,7 @@ fn emit_redundant_guards<'tcx>(
249249
/// an error in the future, and rustc already actively warns against this (see rust#41620),
250250
/// so we don't consider those as usable within patterns for linting purposes.
251251
fn expr_can_be_pat(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
252-
for_each_expr(expr, |expr| {
252+
for_each_expr_without_closures(expr, |expr| {
253253
if match expr.kind {
254254
ExprKind::ConstBlock(..) => cx.tcx.features().inline_const_pat,
255255
ExprKind::Call(c, ..) if let ExprKind::Path(qpath) = c.kind => {

0 commit comments

Comments
 (0)