Skip to content

Commit 2022ed2

Browse files
committed
Apply suggestions
1 parent 764d4bc commit 2022ed2

8 files changed

+33
-56
lines changed

clippy_lints/src/if_let_mutex.rs

+19-23
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,23 @@ declare_lint_pass!(IfLetMutex => [IF_LET_MUTEX]);
4545

4646
impl<'tcx> LateLintPass<'tcx> for IfLetMutex {
4747
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
48-
let mut arm_visit = ArmVisitor { found_mutex: None, cx };
49-
let mut op_visit = OppVisitor { found_mutex: None, cx };
48+
let mut arm_visit = ArmVisitor { cx };
49+
let mut op_visit = OppVisitor { cx };
5050
if let Some(higher::IfLet {
5151
let_expr,
5252
if_then,
5353
if_else: Some(if_else),
5454
..
5555
}) = higher::IfLet::hir(cx, expr)
5656
{
57-
op_visit.visit_expr(let_expr);
58-
if let Some(op_mutex) = op_visit.found_mutex {
59-
arm_visit.visit_expr(if_then);
60-
arm_visit.visit_expr(if_else);
57+
let found_op_mutex = op_visit.visit_expr(let_expr).break_value();
58+
if let Some(op_mutex) = found_op_mutex {
59+
let mut found_mutex = arm_visit.visit_expr(if_then).break_value();
60+
if found_mutex.is_none() {
61+
found_mutex = arm_visit.visit_expr(if_else).break_value();
62+
};
6163

62-
if let Some(arm_mutex) = arm_visit.found_mutex_if_same_as(op_mutex) {
64+
if let Some(arm_mutex) = arm_visit.found_mutex_if_same_as(op_mutex, found_mutex) {
6365
let diag = |diag: &mut Diag<'_, ()>| {
6466
diag.span_label(
6567
op_mutex.span,
@@ -86,43 +88,37 @@ impl<'tcx> LateLintPass<'tcx> for IfLetMutex {
8688

8789
/// Checks if `Mutex::lock` is called in the `if let` expr.
8890
pub struct OppVisitor<'a, 'tcx> {
89-
found_mutex: Option<&'tcx Expr<'tcx>>,
9091
cx: &'a LateContext<'tcx>,
9192
}
9293

9394
impl<'tcx> Visitor<'tcx> for OppVisitor<'_, 'tcx> {
94-
type Result = ControlFlow<()>;
95-
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) -> ControlFlow<()> {
95+
type Result = ControlFlow<&'tcx Expr<'tcx>>;
96+
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) -> ControlFlow<&'tcx Expr<'tcx>> {
9697
if let Some(mutex) = is_mutex_lock_call(self.cx, expr) {
97-
self.found_mutex = Some(mutex);
98-
return ControlFlow::Break(());
98+
return ControlFlow::Break(mutex);
9999
}
100-
visit::walk_expr(self, expr);
101-
ControlFlow::Continue(())
100+
visit::walk_expr(self, expr)
102101
}
103102
}
104103

105104
/// Checks if `Mutex::lock` is called in any of the branches.
106105
pub struct ArmVisitor<'a, 'tcx> {
107-
found_mutex: Option<&'tcx Expr<'tcx>>,
108106
cx: &'a LateContext<'tcx>,
109107
}
110108

111109
impl<'tcx> Visitor<'tcx> for ArmVisitor<'_, 'tcx> {
112-
type Result = ControlFlow<()>;
113-
fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) -> ControlFlow<()> {
110+
type Result = ControlFlow<&'tcx Expr<'tcx>>;
111+
fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) -> ControlFlow<&'tcx Expr<'tcx>> {
114112
if let Some(mutex) = is_mutex_lock_call(self.cx, expr) {
115-
self.found_mutex = Some(mutex);
116-
return ControlFlow::Break(());
113+
return ControlFlow::Break(mutex);
117114
}
118-
visit::walk_expr(self, expr);
119-
ControlFlow::Continue(())
115+
visit::walk_expr(self, expr)
120116
}
121117
}
122118

123119
impl<'tcx, 'l> ArmVisitor<'tcx, 'l> {
124-
fn found_mutex_if_same_as(&self, op_mutex: &Expr<'_>) -> Option<&Expr<'_>> {
125-
self.found_mutex.and_then(|arm_mutex| {
120+
fn found_mutex_if_same_as(&self, op_mutex: &Expr<'_>, found_mutex: Option<&'tcx Expr<'tcx>>) -> Option<&Expr<'_>> {
121+
found_mutex.and_then(|arm_mutex| {
126122
SpanlessEq::new(self.cx)
127123
.eq_expr(op_mutex, arm_mutex)
128124
.then_some(arm_mutex)

clippy_lints/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#![feature(array_windows)]
22
#![feature(binary_heap_into_iter_sorted)]
33
#![feature(box_patterns)]
4+
#![feature(control_flow_enum)]
45
#![feature(f128)]
56
#![feature(f16)]
67
#![feature(if_let_guard)]

clippy_lints/src/lifetimes.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -381,11 +381,8 @@ fn could_use_elision<'tcx>(
381381
return None;
382382
}
383383

384-
let mut checker = BodyLifetimeChecker {
385-
lifetimes_used_in_body: false,
386-
};
387-
checker.visit_expr(body.value);
388-
if checker.lifetimes_used_in_body {
384+
let mut checker = BodyLifetimeChecker;
385+
if checker.visit_expr(body.value).is_break() {
389386
return None;
390387
}
391388
}
@@ -695,16 +692,13 @@ fn report_extra_impl_lifetimes<'tcx>(cx: &LateContext<'tcx>, impl_: &'tcx Impl<'
695692
}
696693
}
697694

698-
struct BodyLifetimeChecker {
699-
lifetimes_used_in_body: bool,
700-
}
695+
struct BodyLifetimeChecker;
701696

702697
impl<'tcx> Visitor<'tcx> for BodyLifetimeChecker {
703698
type Result = ControlFlow<()>;
704699
// for lifetimes as parameters of generics
705700
fn visit_lifetime(&mut self, lifetime: &'tcx Lifetime) -> ControlFlow<()> {
706701
if !lifetime.is_anonymous() && lifetime.ident.name != kw::StaticLifetime {
707-
self.lifetimes_used_in_body = true;
708702
return ControlFlow::Break(());
709703
}
710704
ControlFlow::Continue(())

clippy_lints/src/loops/mut_range_bound.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ impl<'tcx> Visitor<'tcx> for BreakAfterExprVisitor {
145145

146146
return ControlFlow::Break(());
147147
}
148-
intravisit::walk_expr(self, expr);
149-
ControlFlow::Continue(())
148+
intravisit::walk_expr(self, expr)
150149
}
151150
}

clippy_lints/src/loops/while_immutable_condition.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,8 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, cond: &'tcx Expr<'_>, expr: &'
3636
};
3737
let mutable_static_in_cond = var_visitor.def_ids.items().any(|(_, v)| *v);
3838

39-
let mut has_break_or_return_visitor = HasBreakOrReturnVisitor {
40-
has_break_or_return: false,
41-
};
42-
has_break_or_return_visitor.visit_expr(expr);
43-
let has_break_or_return = has_break_or_return_visitor.has_break_or_return;
39+
let mut has_break_or_return_visitor = HasBreakOrReturnVisitor;
40+
let has_break_or_return = has_break_or_return_visitor.visit_expr(expr).is_break();
4441

4542
if no_cond_variable_mutated && !mutable_static_in_cond {
4643
span_lint_and_then(
@@ -60,23 +57,19 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, cond: &'tcx Expr<'_>, expr: &'
6057
}
6158
}
6259

63-
struct HasBreakOrReturnVisitor {
64-
has_break_or_return: bool,
65-
}
60+
struct HasBreakOrReturnVisitor;
6661

6762
impl<'tcx> Visitor<'tcx> for HasBreakOrReturnVisitor {
6863
type Result = ControlFlow<()>;
6964
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) -> ControlFlow<()> {
7065
match expr.kind {
7166
ExprKind::Ret(_) | ExprKind::Break(_, _) => {
72-
self.has_break_or_return = true;
7367
return ControlFlow::Break(());
7468
},
7569
_ => {},
7670
}
7771

78-
walk_expr(self, expr);
79-
ControlFlow::Continue(())
72+
walk_expr(self, expr)
8073
}
8174
}
8275

clippy_lints/src/methods/option_map_unwrap_or.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,16 @@ pub(super) fn check<'tcx>(
5555
let mut reference_visitor = ReferenceVisitor {
5656
cx,
5757
identifiers: unwrap_visitor.identifiers,
58-
found_reference: false,
5958
unwrap_or_span: unwrap_arg.span,
6059
};
6160

6261
let map = cx.tcx.hir();
6362
let body = map.body_owned_by(map.enclosing_body_owner(expr.hir_id));
6463
reference_visitor.visit_body(body);
6564

66-
if reference_visitor.found_reference {
65+
if reference_visitor.visit_body(body);.is_break() {
6766
return;
68-
}
67+
};
6968
}
7069

7170
if !unwrap_arg.span.eq_ctxt(map_span) {
@@ -152,7 +151,6 @@ impl<'a, 'tcx> Visitor<'tcx> for UnwrapVisitor<'a, 'tcx> {
152151
struct ReferenceVisitor<'a, 'tcx> {
153152
cx: &'a LateContext<'tcx>,
154153
identifiers: FxHashSet<HirId>,
155-
found_reference: bool,
156154
unwrap_or_span: Span,
157155
}
158156

@@ -171,11 +169,9 @@ impl<'a, 'tcx> Visitor<'tcx> for ReferenceVisitor<'a, 'tcx> {
171169
&& let PatKind::Binding(_, local_id, ..) = pat.kind
172170
&& self.identifiers.contains(&local_id)
173171
{
174-
self.found_reference = true;
175172
return ControlFlow::Break(());
176173
}
177-
rustc_hir::intravisit::walk_expr(self, expr);
178-
ControlFlow::Continue(())
174+
rustc_hir::intravisit::walk_expr(self, expr)
179175
}
180176

181177
fn nested_visit_map(&mut self) -> Self::Map {

clippy_lints/src/redundant_closure_call.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ impl<'tcx> Visitor<'tcx> for ReturnVisitor {
6161
self.found_return = true;
6262
return ControlFlow::Break(());
6363
}
64-
hir_visit::walk_expr(self, ex);
65-
ControlFlow::Continue(())
64+
hir_visit::walk_expr(self, ex)
6665
}
6766
}
6867

clippy_lints/src/unused_peekable.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,7 @@ impl<'tcx> Visitor<'tcx> for PeekableVisitor<'_, 'tcx> {
216216
}
217217
}
218218

219-
walk_expr(self, ex);
220-
ControlFlow::Continue(())
219+
walk_expr(self, ex)
221220
}
222221
}
223222

0 commit comments

Comments
 (0)