[if_let_mutex]: make the mutex check part of the expr visitor
This commit is contained in:
parent
668b659b47
commit
1a1c978f8e
3 changed files with 42 additions and 21 deletions
|
|
@ -1,7 +1,7 @@
|
|||
use clippy_utils::diagnostics::span_lint_and_then;
|
||||
use clippy_utils::ty::is_type_diagnostic_item;
|
||||
use clippy_utils::visitors::for_each_expr_without_closures;
|
||||
use clippy_utils::{higher, SpanlessEq};
|
||||
use clippy_utils::{eq_expr_value, higher};
|
||||
use core::ops::ControlFlow;
|
||||
use rustc_errors::Diag;
|
||||
use rustc_hir::{Expr, ExprKind};
|
||||
|
|
@ -52,20 +52,11 @@ impl<'tcx> LateLintPass<'tcx> for IfLetMutex {
|
|||
..
|
||||
}) = higher::IfLet::hir(cx, expr)
|
||||
{
|
||||
let is_mutex_lock = |e: &'tcx Expr<'tcx>| {
|
||||
if let Some(mutex) = is_mutex_lock_call(cx, e) {
|
||||
ControlFlow::Break(mutex)
|
||||
} else {
|
||||
ControlFlow::Continue(())
|
||||
}
|
||||
};
|
||||
|
||||
let op_mutex = for_each_expr_without_closures(let_expr, is_mutex_lock);
|
||||
let op_mutex = for_each_expr_without_closures(let_expr, |e| mutex_lock_call(cx, e, None));
|
||||
if let Some(op_mutex) = op_mutex {
|
||||
let arm_mutex = for_each_expr_without_closures((if_then, if_else), is_mutex_lock);
|
||||
if let Some(arm_mutex) = arm_mutex
|
||||
&& SpanlessEq::new(cx).eq_expr(op_mutex, arm_mutex)
|
||||
{
|
||||
let arm_mutex =
|
||||
for_each_expr_without_closures((if_then, if_else), |e| mutex_lock_call(cx, e, Some(op_mutex)));
|
||||
if let Some(arm_mutex) = arm_mutex {
|
||||
let diag = |diag: &mut Diag<'_, ()>| {
|
||||
diag.span_label(
|
||||
op_mutex.span,
|
||||
|
|
@ -90,14 +81,19 @@ impl<'tcx> LateLintPass<'tcx> for IfLetMutex {
|
|||
}
|
||||
}
|
||||
|
||||
fn is_mutex_lock_call<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<&'tcx Expr<'tcx>> {
|
||||
fn mutex_lock_call<'tcx>(
|
||||
cx: &LateContext<'tcx>,
|
||||
expr: &'tcx Expr<'_>,
|
||||
op_mutex: Option<&'tcx Expr<'_>>,
|
||||
) -> ControlFlow<&'tcx Expr<'tcx>> {
|
||||
if let ExprKind::MethodCall(path, self_arg, ..) = &expr.kind
|
||||
&& path.ident.as_str() == "lock"
|
||||
&& let ty = cx.typeck_results().expr_ty(self_arg).peel_refs()
|
||||
&& is_type_diagnostic_item(cx, ty, sym::Mutex)
|
||||
&& op_mutex.map_or(true, |op| eq_expr_value(cx, self_arg, op))
|
||||
{
|
||||
Some(self_arg)
|
||||
ControlFlow::Break(self_arg)
|
||||
} else {
|
||||
None
|
||||
ControlFlow::Continue(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue