use Visitor api to find Mutex::lock calls

This commit is contained in:
Devin R 2020-03-18 18:13:06 -04:00
parent fca3537fa3
commit c6c77d9a42
2 changed files with 101 additions and 85 deletions

View file

@ -1,6 +1,7 @@
use crate::utils::{match_type, paths, span_lint_and_help}; use crate::utils::{match_type, paths, span_lint_and_help};
use if_chain::if_chain; use rustc::hir::map::Map;
use rustc_hir::{Arm, Expr, ExprKind, MatchSource, Stmt, StmtKind}; use rustc_hir::intravisit::{self as visit, NestedVisitorMap, Visitor};
use rustc_hir::{Arm, Expr, ExprKind, MatchSource, StmtKind};
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_session::{declare_lint_pass, declare_tool_lint};
@ -40,100 +41,115 @@ declare_lint_pass!(IfLetMutex => [IF_LET_MUTEX]);
impl LateLintPass<'_, '_> for IfLetMutex { impl LateLintPass<'_, '_> for IfLetMutex {
fn check_expr(&mut self, cx: &LateContext<'_, '_>, ex: &'_ Expr<'_>) { fn check_expr(&mut self, cx: &LateContext<'_, '_>, ex: &'_ Expr<'_>) {
if_chain! { let mut arm_visit = ArmVisitor {
if let ExprKind::Match(ref op, ref arms, MatchSource::IfLetDesugar { arm_mutex: false,
arm_lock: false,
cx,
};
let mut op_visit = IfLetMutexVisitor {
op_mutex: false,
op_lock: false,
cx,
};
if let ExprKind::Match(
ref op,
ref arms,
MatchSource::IfLetDesugar {
contains_else_clause: true, contains_else_clause: true,
}) = ex.kind; // if let ... {} else {} },
if let ExprKind::MethodCall(_, _, ref args) = op.kind; ) = ex.kind
let ty = cx.tables.expr_ty(&args[0]); {
if match_type(cx, ty, &paths::MUTEX); // make sure receiver is Mutex op_visit.visit_expr(op);
if method_chain_names(op, 10).iter().any(|s| s == "lock"); // and lock is called if op_visit.op_mutex && op_visit.op_lock {
for arm in *arms {
arm_visit.visit_arm(arm);
}
if arms.iter().any(|arm| matching_arm(arm, op, ex, cx)); if arm_visit.arm_mutex && arm_visit.arm_lock {
then { span_lint_and_help(
span_lint_and_help( cx,
cx, IF_LET_MUTEX,
IF_LET_MUTEX, ex.span,
ex.span, "calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a deadlock",
"calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a deadlock", "move the lock call outside of the `if let ...` expression",
"move the lock call outside of the `if let ...` expression", );
); }
} }
} }
} }
} }
fn matching_arm(arm: &Arm<'_>, op: &Expr<'_>, ex: &Expr<'_>, cx: &LateContext<'_, '_>) -> bool { /// Checks if `Mutex::lock` is called in the `if let _ = expr.
if let ExprKind::Block(ref block, _l) = arm.body.kind { pub struct IfLetMutexVisitor<'tcx, 'l> {
block.stmts.iter().any(|stmt| matching_stmt(stmt, op, ex, cx)) pub op_mutex: bool,
} else { pub op_lock: bool,
false pub cx: &'tcx LateContext<'tcx, 'l>,
}
} }
fn matching_stmt(stmt: &Stmt<'_>, op: &Expr<'_>, ex: &Expr<'_>, cx: &LateContext<'_, '_>) -> bool { impl<'tcx, 'l> Visitor<'tcx> for IfLetMutexVisitor<'tcx, 'l> {
match stmt.kind { type Map = Map<'tcx>;
StmtKind::Local(l) => if_chain! {
if let Some(ex) = l.init;
if let ExprKind::MethodCall(_, _, _) = op.kind;
if method_chain_names(ex, 10).iter().any(|s| s == "lock"); // and lock is called
then {
match_type_method_chain(cx, ex, 5)
} else {
false
}
},
StmtKind::Expr(e) => if_chain! {
if let ExprKind::MethodCall(_, _, _) = e.kind;
if method_chain_names(e, 10).iter().any(|s| s == "lock"); // and lock is called
then {
match_type_method_chain(cx, ex, 5)
} else {
false
}
},
StmtKind::Semi(e) => if_chain! {
if let ExprKind::MethodCall(_, _, _) = e.kind;
if method_chain_names(e, 10).iter().any(|s| s == "lock"); // and lock is called
then {
match_type_method_chain(cx, ex, 5)
} else {
false
}
},
_ => false,
}
}
/// Return the names of `max_depth` number of methods called in the chain. fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
fn method_chain_names<'tcx>(expr: &'tcx Expr<'tcx>, max_depth: usize) -> Vec<String> { if let ExprKind::MethodCall(path, _span, args) = &expr.kind {
let mut method_names = Vec::with_capacity(max_depth); if path.ident.to_string() == "lock" {
let mut current = expr; self.op_lock = true;
for _ in 0..max_depth { }
if let ExprKind::MethodCall(path, _, args) = &current.kind { let ty = self.cx.tables.expr_ty(&args[0]);
if args.iter().any(|e| e.span.from_expansion()) { if match_type(self.cx, ty, &paths::MUTEX) {
break; self.op_mutex = true;
} }
method_names.push(path.ident.to_string());
current = &args[0];
} else {
break;
} }
visit::walk_expr(self, expr);
}
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
NestedVisitorMap::None
} }
method_names
} }
/// Check that lock is called on a `Mutex`. /// Checks if `Mutex::lock` is called in any of the branches.
fn match_type_method_chain<'tcx>(cx: &LateContext<'_, '_>, expr: &'tcx Expr<'tcx>, max_depth: usize) -> bool { pub struct ArmVisitor<'tcx, 'l> {
let mut current = expr; pub arm_mutex: bool,
for _ in 0..max_depth { pub arm_lock: bool,
if let ExprKind::MethodCall(_, _, args) = &current.kind { pub cx: &'tcx LateContext<'tcx, 'l>,
let ty = cx.tables.expr_ty(&args[0]); }
if match_type(cx, ty, &paths::MUTEX) {
return true; impl<'tcx, 'l> Visitor<'tcx> for ArmVisitor<'tcx, 'l> {
} type Map = Map<'tcx>;
current = &args[0];
} fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
} if let ExprKind::MethodCall(path, _span, args) = &expr.kind {
false if path.ident.to_string() == "lock" {
self.arm_lock = true;
}
let ty = self.cx.tables.expr_ty(&args[0]);
if match_type(self.cx, ty, &paths::MUTEX) {
self.arm_mutex = true;
}
}
visit::walk_expr(self, expr);
}
fn visit_arm(&mut self, arm: &'tcx Arm<'_>) {
if let ExprKind::Block(ref block, _l) = arm.body.kind {
for stmt in block.stmts {
match stmt.kind {
StmtKind::Local(loc) => {
if let Some(expr) = loc.init {
self.visit_expr(expr)
}
},
StmtKind::Expr(expr) => self.visit_expr(expr),
StmtKind::Semi(expr) => self.visit_expr(expr),
// we don't care about `Item`
_ => {},
}
}
};
visit::walk_arm(self, arm);
}
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
NestedVisitorMap::None
}
} }

View file

@ -3,9 +3,9 @@
use std::sync::Mutex; use std::sync::Mutex;
fn do_stuff<T>(_: T) {} fn do_stuff<T>(_: T) {}
fn foo() {
let m = Mutex::new(1u8);
fn if_let() {
let m = Mutex::new(1u8);
if let Err(locked) = m.lock() { if let Err(locked) = m.lock() {
do_stuff(locked); do_stuff(locked);
} else { } else {