Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
use if chain
  • Loading branch information
DevinR528 committed Apr 20, 2020
commit ae820924c4f39c5a83287ba1cb6f9af572cabf55
43 changes: 21 additions & 22 deletions clippy_lints/src/if_let_mutex.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use if_chain::if_chain;
use crate::utils::{match_type, paths, span_lint_and_help};
use rustc_hir::intravisit::{self as visit, NestedVisitorMap, Visitor};
use rustc_hir::{Arm, Expr, ExprKind, MatchSource, StmtKind};
Expand Down Expand Up @@ -42,13 +43,11 @@ declare_lint_pass!(IfLetMutex => [IF_LET_MUTEX]);
impl LateLintPass<'_, '_> for IfLetMutex {
fn check_expr(&mut self, cx: &LateContext<'_, '_>, ex: &'_ Expr<'_>) {
let mut arm_visit = ArmVisitor {
arm_mutex: false,
arm_lock: false,
mutex_lock_called: false,
cx,
};
let mut op_visit = OppVisitor {
op_mutex: false,
op_lock: false,
mutex_lock_called: false,
cx,
};
if let ExprKind::Match(
Expand All @@ -60,12 +59,12 @@ impl LateLintPass<'_, '_> for IfLetMutex {
) = ex.kind
{
op_visit.visit_expr(op);
if op_visit.op_mutex && op_visit.op_lock {
if op_visit.mutex_lock_called {
for arm in *arms {
arm_visit.visit_arm(arm);
}

if arm_visit.arm_mutex && arm_visit.arm_lock {
if arm_visit.mutex_lock_called {
span_lint_and_help(
cx,
IF_LET_MUTEX,
Expand All @@ -81,22 +80,22 @@ impl LateLintPass<'_, '_> for IfLetMutex {

/// Checks if `Mutex::lock` is called in the `if let _ = expr.
pub struct OppVisitor<'tcx, 'l> {
pub op_mutex: bool,
pub op_lock: bool,
pub mutex_lock_called: bool,
pub cx: &'tcx LateContext<'tcx, 'l>,
}

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

fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
if let ExprKind::MethodCall(path, _span, args) = &expr.kind {
if path.ident.to_string() == "lock" {
self.op_lock = true;
}
if_chain! {
if let ExprKind::MethodCall(path, _span, args) = &expr.kind;
if path.ident.to_string() == "lock";
let ty = self.cx.tables.expr_ty(&args[0]);
if match_type(self.cx, ty, &paths::MUTEX) {
self.op_mutex = true;
if match_type(self.cx, ty, &paths::MUTEX);
then {
self.mutex_lock_called = true;
return;
}
}
visit::walk_expr(self, expr);
Expand All @@ -109,22 +108,22 @@ impl<'tcx, 'l> Visitor<'tcx> for OppVisitor<'tcx, 'l> {

/// Checks if `Mutex::lock` is called in any of the branches.
pub struct ArmVisitor<'tcx, 'l> {
pub arm_mutex: bool,
pub arm_lock: bool,
pub mutex_lock_called: bool,
pub cx: &'tcx LateContext<'tcx, 'l>,
}

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

fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
if let ExprKind::MethodCall(path, _span, args) = &expr.kind {
if path.ident.to_string() == "lock" {
self.arm_lock = true;
}
if_chain! {
if let ExprKind::MethodCall(path, _span, args) = &expr.kind;
if path.ident.to_string() == "lock";
let ty = self.cx.tables.expr_ty(&args[0]);
if match_type(self.cx, ty, &paths::MUTEX) {
self.arm_mutex = true;
if match_type(self.cx, ty, &paths::MUTEX);
then {
self.mutex_lock_called = true;
return;
}
}
visit::walk_expr(self, expr);
Expand Down
4 changes: 2 additions & 2 deletions doc/adding_lints.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ Please note that, we should run `TESTNAME=foo_functions cargo uitest`
every time before running `tests/ui/update-all-references.sh`.
Running `TESTNAME=foo_functions cargo uitest` should pass then. When we commit
our lint, we need to commit the generated `.stderr` files, too. In general you
should only run `tests/ui/update-all-references.sh` for the specific lint you are
creating/editing.
should only commit changed files by `tests/ui/update-all-references.sh` for the
specific lint you are creating/editing.

## Rustfix tests

Expand Down