Skip to content

Commit 8d6ea83

Browse files
committed
use if chain
1 parent 7b6c16e commit 8d6ea83

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

clippy_lints/src/if_let_mutex.rs

+21-22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use if_chain::if_chain;
12
use crate::utils::{match_type, paths, span_lint_and_help};
23
use rustc_hir::intravisit::{self as visit, NestedVisitorMap, Visitor};
34
use rustc_hir::{Arm, Expr, ExprKind, MatchSource, StmtKind};
@@ -42,13 +43,11 @@ declare_lint_pass!(IfLetMutex => [IF_LET_MUTEX]);
4243
impl LateLintPass<'_, '_> for IfLetMutex {
4344
fn check_expr(&mut self, cx: &LateContext<'_, '_>, ex: &'_ Expr<'_>) {
4445
let mut arm_visit = ArmVisitor {
45-
arm_mutex: false,
46-
arm_lock: false,
46+
mutex_lock_called: false,
4747
cx,
4848
};
4949
let mut op_visit = OppVisitor {
50-
op_mutex: false,
51-
op_lock: false,
50+
mutex_lock_called: false,
5251
cx,
5352
};
5453
if let ExprKind::Match(
@@ -60,12 +59,12 @@ impl LateLintPass<'_, '_> for IfLetMutex {
6059
) = ex.kind
6160
{
6261
op_visit.visit_expr(op);
63-
if op_visit.op_mutex && op_visit.op_lock {
62+
if op_visit.mutex_lock_called {
6463
for arm in *arms {
6564
arm_visit.visit_arm(arm);
6665
}
6766

68-
if arm_visit.arm_mutex && arm_visit.arm_lock {
67+
if arm_visit.mutex_lock_called {
6968
span_lint_and_help(
7069
cx,
7170
IF_LET_MUTEX,
@@ -81,22 +80,22 @@ impl LateLintPass<'_, '_> for IfLetMutex {
8180

8281
/// Checks if `Mutex::lock` is called in the `if let _ = expr.
8382
pub struct OppVisitor<'tcx, 'l> {
84-
pub op_mutex: bool,
85-
pub op_lock: bool,
83+
pub mutex_lock_called: bool,
8684
pub cx: &'tcx LateContext<'tcx, 'l>,
8785
}
8886

8987
impl<'tcx, 'l> Visitor<'tcx> for OppVisitor<'tcx, 'l> {
9088
type Map = Map<'tcx>;
9189

9290
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
93-
if let ExprKind::MethodCall(path, _span, args) = &expr.kind {
94-
if path.ident.to_string() == "lock" {
95-
self.op_lock = true;
96-
}
91+
if_chain! {
92+
if let ExprKind::MethodCall(path, _span, args) = &expr.kind;
93+
if path.ident.to_string() == "lock";
9794
let ty = self.cx.tables.expr_ty(&args[0]);
98-
if match_type(self.cx, ty, &paths::MUTEX) {
99-
self.op_mutex = true;
95+
if match_type(self.cx, ty, &paths::MUTEX);
96+
then {
97+
self.mutex_lock_called = true;
98+
return;
10099
}
101100
}
102101
visit::walk_expr(self, expr);
@@ -109,22 +108,22 @@ impl<'tcx, 'l> Visitor<'tcx> for OppVisitor<'tcx, 'l> {
109108

110109
/// Checks if `Mutex::lock` is called in any of the branches.
111110
pub struct ArmVisitor<'tcx, 'l> {
112-
pub arm_mutex: bool,
113-
pub arm_lock: bool,
111+
pub mutex_lock_called: bool,
114112
pub cx: &'tcx LateContext<'tcx, 'l>,
115113
}
116114

117115
impl<'tcx, 'l> Visitor<'tcx> for ArmVisitor<'tcx, 'l> {
118116
type Map = Map<'tcx>;
119117

120118
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
121-
if let ExprKind::MethodCall(path, _span, args) = &expr.kind {
122-
if path.ident.to_string() == "lock" {
123-
self.arm_lock = true;
124-
}
119+
if_chain! {
120+
if let ExprKind::MethodCall(path, _span, args) = &expr.kind;
121+
if path.ident.to_string() == "lock";
125122
let ty = self.cx.tables.expr_ty(&args[0]);
126-
if match_type(self.cx, ty, &paths::MUTEX) {
127-
self.arm_mutex = true;
123+
if match_type(self.cx, ty, &paths::MUTEX);
124+
then {
125+
self.mutex_lock_called = true;
126+
return;
128127
}
129128
}
130129
visit::walk_expr(self, expr);

doc/adding_lints.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ Please note that, we should run `TESTNAME=foo_functions cargo uitest`
102102
every time before running `tests/ui/update-all-references.sh`.
103103
Running `TESTNAME=foo_functions cargo uitest` should pass then. When we commit
104104
our lint, we need to commit the generated `.stderr` files, too. In general you
105-
should only run `tests/ui/update-all-references.sh` for the specific lint you are
106-
creating/editing.
105+
should only commit changed files by `tests/ui/update-all-references.sh` for the
106+
specific lint you are creating/editing.
107107

108108
## Rustfix tests
109109

0 commit comments

Comments
 (0)