Skip to content

Commit cecb4ea

Browse files
committed
Use for_each_expr in place of some visitors
1 parent ef1afe4 commit cecb4ea

9 files changed

+204
-332
lines changed

clippy_lints/src/blocks_in_if_conditions.rs

+28-40
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ use clippy_utils::get_parent_expr;
33
use clippy_utils::higher;
44
use clippy_utils::source::snippet_block_with_applicability;
55
use clippy_utils::ty::implements_trait;
6+
use clippy_utils::visitors::{for_each_expr, Descend};
7+
use core::ops::ControlFlow;
68
use if_chain::if_chain;
79
use rustc_errors::Applicability;
8-
use rustc_hir::intravisit::{walk_expr, Visitor};
9-
use rustc_hir::{BlockCheckMode, Closure, Expr, ExprKind};
10+
use rustc_hir::{BlockCheckMode, Expr, ExprKind};
1011
use rustc_lint::{LateContext, LateLintPass, LintContext};
1112
use rustc_middle::lint::in_external_macro;
1213
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -44,39 +45,6 @@ declare_clippy_lint! {
4445

4546
declare_lint_pass!(BlocksInIfConditions => [BLOCKS_IN_IF_CONDITIONS]);
4647

47-
struct ExVisitor<'a, 'tcx> {
48-
found_block: Option<&'tcx Expr<'tcx>>,
49-
cx: &'a LateContext<'tcx>,
50-
}
51-
52-
impl<'a, 'tcx> Visitor<'tcx> for ExVisitor<'a, 'tcx> {
53-
fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
54-
if let ExprKind::Closure(&Closure { body, .. }) = expr.kind {
55-
// do not lint if the closure is called using an iterator (see #1141)
56-
if_chain! {
57-
if let Some(parent) = get_parent_expr(self.cx, expr);
58-
if let ExprKind::MethodCall(_, self_arg, ..) = &parent.kind;
59-
let caller = self.cx.typeck_results().expr_ty(self_arg);
60-
if let Some(iter_id) = self.cx.tcx.get_diagnostic_item(sym::Iterator);
61-
if implements_trait(self.cx, caller, iter_id, &[]);
62-
then {
63-
return;
64-
}
65-
}
66-
67-
let body = self.cx.tcx.hir().body(body);
68-
let ex = &body.value;
69-
if let ExprKind::Block(block, _) = ex.kind {
70-
if !body.value.span.from_expansion() && !block.stmts.is_empty() {
71-
self.found_block = Some(ex);
72-
return;
73-
}
74-
}
75-
}
76-
walk_expr(self, expr);
77-
}
78-
}
79-
8048
const BRACED_EXPR_MESSAGE: &str = "omit braces around single expression condition";
8149
const COMPLEX_BLOCK_MESSAGE: &str = "in an `if` condition, avoid complex blocks or closures with blocks; \
8250
instead, move the block or closure higher and bind it with a `let`";
@@ -144,11 +112,31 @@ impl<'tcx> LateLintPass<'tcx> for BlocksInIfConditions {
144112
}
145113
}
146114
} else {
147-
let mut visitor = ExVisitor { found_block: None, cx };
148-
walk_expr(&mut visitor, cond);
149-
if let Some(block) = visitor.found_block {
150-
span_lint(cx, BLOCKS_IN_IF_CONDITIONS, block.span, COMPLEX_BLOCK_MESSAGE);
151-
}
115+
let _: Option<!> = for_each_expr(cond, |e| {
116+
if let ExprKind::Closure(closure) = e.kind {
117+
// do not lint if the closure is called using an iterator (see #1141)
118+
if_chain! {
119+
if let Some(parent) = get_parent_expr(cx, e);
120+
if let ExprKind::MethodCall(_, self_arg, _, _) = &parent.kind;
121+
let caller = cx.typeck_results().expr_ty(self_arg);
122+
if let Some(iter_id) = cx.tcx.get_diagnostic_item(sym::Iterator);
123+
if implements_trait(cx, caller, iter_id, &[]);
124+
then {
125+
return ControlFlow::Continue(Descend::No);
126+
}
127+
}
128+
129+
let body = cx.tcx.hir().body(closure.body);
130+
let ex = &body.value;
131+
if let ExprKind::Block(block, _) = ex.kind {
132+
if !body.value.span.from_expansion() && !block.stmts.is_empty() {
133+
span_lint(cx, BLOCKS_IN_IF_CONDITIONS, ex.span, COMPLEX_BLOCK_MESSAGE);
134+
return ControlFlow::Continue(Descend::No);
135+
}
136+
}
137+
}
138+
ControlFlow::Continue(Descend::Yes)
139+
});
152140
}
153141
}
154142
}

clippy_lints/src/cognitive_complexity.rs

+28-35
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
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;
67
use clippy_utils::LimitStack;
8+
use core::ops::ControlFlow;
79
use rustc_ast::ast::Attribute;
8-
use rustc_hir::intravisit::{walk_expr, FnKind, Visitor};
9-
use rustc_hir::{Body, Expr, ExprKind, FnDecl, HirId};
10+
use rustc_hir::intravisit::FnKind;
11+
use rustc_hir::{Body, ExprKind, FnDecl, HirId};
1012
use rustc_lint::{LateContext, LateLintPass, LintContext};
1113
use rustc_session::{declare_tool_lint, impl_lint_pass};
1214
use rustc_span::source_map::Span;
@@ -61,11 +63,27 @@ impl CognitiveComplexity {
6163
return;
6264
}
6365

64-
let expr = &body.value;
66+
let expr = body.value;
67+
68+
let mut cc = 1u64;
69+
let mut returns = 0u64;
70+
let _: Option<!> = for_each_expr(expr, |e| {
71+
match e.kind {
72+
ExprKind::If(_, _, _) => {
73+
cc += 1;
74+
},
75+
ExprKind::Match(_, arms, _) => {
76+
if arms.len() > 1 {
77+
cc += 1;
78+
}
79+
cc += arms.iter().filter(|arm| arm.guard.is_some()).count() as u64;
80+
},
81+
ExprKind::Ret(_) => returns += 1,
82+
_ => {},
83+
}
84+
ControlFlow::Continue(())
85+
});
6586

66-
let mut helper = CcHelper { cc: 1, returns: 0 };
67-
helper.visit_expr(expr);
68-
let CcHelper { cc, returns } = helper;
6987
let ret_ty = cx.typeck_results().node_type(expr.hir_id);
7088
let ret_adjust = if is_type_diagnostic_item(cx, ret_ty, sym::Result) {
7189
returns
@@ -74,13 +92,12 @@ impl CognitiveComplexity {
7492
(returns / 2)
7593
};
7694

77-
let mut rust_cc = cc;
7895
// prevent degenerate cases where unreachable code contains `return` statements
79-
if rust_cc >= ret_adjust {
80-
rust_cc -= ret_adjust;
96+
if cc >= ret_adjust {
97+
cc -= ret_adjust;
8198
}
8299

83-
if rust_cc > self.limit.limit() {
100+
if cc > self.limit.limit() {
84101
let fn_span = match kind {
85102
FnKind::ItemFn(ident, _, _) | FnKind::Method(ident, _) => ident.span,
86103
FnKind::Closure => {
@@ -108,7 +125,7 @@ impl CognitiveComplexity {
108125
fn_span,
109126
&format!(
110127
"the function has a cognitive complexity of ({}/{})",
111-
rust_cc,
128+
cc,
112129
self.limit.limit()
113130
),
114131
None,
@@ -141,27 +158,3 @@ impl<'tcx> LateLintPass<'tcx> for CognitiveComplexity {
141158
self.limit.pop_attrs(cx.sess(), attrs, "cognitive_complexity");
142159
}
143160
}
144-
145-
struct CcHelper {
146-
cc: u64,
147-
returns: u64,
148-
}
149-
150-
impl<'tcx> Visitor<'tcx> for CcHelper {
151-
fn visit_expr(&mut self, e: &'tcx Expr<'_>) {
152-
walk_expr(self, e);
153-
match e.kind {
154-
ExprKind::If(_, _, _) => {
155-
self.cc += 1;
156-
},
157-
ExprKind::Match(_, arms, _) => {
158-
if arms.len() > 1 {
159-
self.cc += 1;
160-
}
161-
self.cc += arms.iter().filter(|arm| arm.guard.is_some()).count() as u64;
162-
},
163-
ExprKind::Ret(_) => self.returns += 1,
164-
_ => {},
165-
}
166-
}
167-
}

clippy_lints/src/functions/must_use.rs

+32-48
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_ast::ast::Attribute;
22
use rustc_errors::Applicability;
33
use rustc_hir::def_id::{DefIdSet, LocalDefId};
4-
use rustc_hir::{self as hir, def::Res, intravisit, QPath};
4+
use rustc_hir::{self as hir, def::Res, QPath};
55
use rustc_lint::{LateContext, LintContext};
66
use rustc_middle::{
77
lint::in_external_macro,
@@ -13,8 +13,11 @@ use clippy_utils::attrs::is_proc_macro;
1313
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_then};
1414
use clippy_utils::source::snippet_opt;
1515
use clippy_utils::ty::is_must_use_ty;
16+
use clippy_utils::visitors::for_each_expr;
1617
use clippy_utils::{match_def_path, return_ty, trait_ref_of_method};
1718

19+
use core::ops::ControlFlow;
20+
1821
use super::{DOUBLE_MUST_USE, MUST_USE_CANDIDATE, MUST_USE_UNIT};
1922

2023
pub(super) fn check_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::Item<'_>) {
@@ -199,79 +202,60 @@ fn is_mutable_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>, span: Span, tys: &m
199202
}
200203
}
201204

202-
struct StaticMutVisitor<'a, 'tcx> {
203-
cx: &'a LateContext<'tcx>,
204-
mutates_static: bool,
205+
fn is_mutated_static(e: &hir::Expr<'_>) -> bool {
206+
use hir::ExprKind::{Field, Index, Path};
207+
208+
match e.kind {
209+
Path(QPath::Resolved(_, path)) => !matches!(path.res, Res::Local(_)),
210+
Path(_) => true,
211+
Field(inner, _) | Index(inner, _) => is_mutated_static(inner),
212+
_ => false,
213+
}
205214
}
206215

207-
impl<'a, 'tcx> intravisit::Visitor<'tcx> for StaticMutVisitor<'a, 'tcx> {
208-
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'_>) {
216+
fn mutates_static<'tcx>(cx: &LateContext<'tcx>, body: &'tcx hir::Body<'_>) -> bool {
217+
for_each_expr(body.value, |e| {
209218
use hir::ExprKind::{AddrOf, Assign, AssignOp, Call, MethodCall};
210219

211-
if self.mutates_static {
212-
return;
213-
}
214-
match expr.kind {
220+
match e.kind {
215221
Call(_, args) => {
216222
let mut tys = DefIdSet::default();
217223
for arg in args {
218-
if self.cx.tcx.has_typeck_results(arg.hir_id.owner.to_def_id())
219-
&& is_mutable_ty(
220-
self.cx,
221-
self.cx.tcx.typeck(arg.hir_id.owner).expr_ty(arg),
222-
arg.span,
223-
&mut tys,
224-
)
224+
if cx.tcx.has_typeck_results(arg.hir_id.owner.to_def_id())
225+
&& is_mutable_ty(cx, cx.tcx.typeck(arg.hir_id.owner).expr_ty(arg), arg.span, &mut tys)
225226
&& is_mutated_static(arg)
226227
{
227-
self.mutates_static = true;
228-
return;
228+
return ControlFlow::Break(());
229229
}
230230
tys.clear();
231231
}
232+
ControlFlow::Continue(())
232233
},
233234
MethodCall(_, receiver, args, _) => {
234235
let mut tys = DefIdSet::default();
235236
for arg in std::iter::once(receiver).chain(args.iter()) {
236-
if self.cx.tcx.has_typeck_results(arg.hir_id.owner.to_def_id())
237+
if cx.tcx.has_typeck_results(arg.hir_id.owner.to_def_id())
237238
&& is_mutable_ty(
238-
self.cx,
239-
self.cx.tcx.typeck(arg.hir_id.owner).expr_ty(arg),
239+
cx,
240+
cx.tcx.typeck(arg.hir_id.owner).expr_ty(arg),
240241
arg.span,
241242
&mut tys,
242243
)
243244
&& is_mutated_static(arg)
244245
{
245-
self.mutates_static = true;
246-
return;
246+
return ControlFlow::Break(());
247247
}
248248
tys.clear();
249249
}
250+
ControlFlow::Continue(())
250251
},
251-
Assign(target, ..) | AssignOp(_, target, _) | AddrOf(_, hir::Mutability::Mut, target) => {
252-
self.mutates_static |= is_mutated_static(target);
252+
Assign(target, ..) | AssignOp(_, target, _) | AddrOf(_, hir::Mutability::Mut, target)
253+
if is_mutated_static(target) =>
254+
{
255+
ControlFlow::Break(())
253256
},
254-
_ => {},
257+
_ => ControlFlow::Continue(()),
255258
}
256-
}
257-
}
258-
259-
fn is_mutated_static(e: &hir::Expr<'_>) -> bool {
260-
use hir::ExprKind::{Field, Index, Path};
261-
262-
match e.kind {
263-
Path(QPath::Resolved(_, path)) => !matches!(path.res, Res::Local(_)),
264-
Path(_) => true,
265-
Field(inner, _) | Index(inner, _) => is_mutated_static(inner),
266-
_ => false,
267-
}
268-
}
269-
270-
fn mutates_static<'tcx>(cx: &LateContext<'tcx>, body: &'tcx hir::Body<'_>) -> bool {
271-
let mut v = StaticMutVisitor {
272-
cx,
273-
mutates_static: false,
274-
};
275-
intravisit::walk_expr(&mut v, body.value);
276-
v.mutates_static
259+
})
260+
.is_some()
277261
}

0 commit comments

Comments
 (0)