|  | 
|  | 1 | +use crate::utils::{match_type, paths, span_lint_and_help, SpanlessEq}; | 
|  | 2 | +use if_chain::if_chain; | 
|  | 3 | +use rustc_hir::intravisit::{self as visit, NestedVisitorMap, Visitor}; | 
|  | 4 | +use rustc_hir::{Expr, ExprKind, MatchSource}; | 
|  | 5 | +use rustc_lint::{LateContext, LateLintPass}; | 
|  | 6 | +use rustc_middle::hir::map::Map; | 
|  | 7 | +use rustc_session::{declare_lint_pass, declare_tool_lint}; | 
|  | 8 | + | 
|  | 9 | +declare_clippy_lint! { | 
|  | 10 | +    /// **What it does:** Checks for `Mutex::lock` calls in `if let` expression | 
|  | 11 | +    /// with lock calls in any of the else blocks. | 
|  | 12 | +    /// | 
|  | 13 | +    /// **Why is this bad?** The Mutex lock remains held for the whole | 
|  | 14 | +    /// `if let ... else` block and deadlocks. | 
|  | 15 | +    /// | 
|  | 16 | +    /// **Known problems:** None. | 
|  | 17 | +    /// | 
|  | 18 | +    /// **Example:** | 
|  | 19 | +    /// | 
|  | 20 | +    /// ```rust,ignore | 
|  | 21 | +    /// if let Ok(thing) = mutex.lock() { | 
|  | 22 | +    ///     do_thing(); | 
|  | 23 | +    /// } else { | 
|  | 24 | +    ///     mutex.lock(); | 
|  | 25 | +    /// } | 
|  | 26 | +    /// ``` | 
|  | 27 | +    /// Should be written | 
|  | 28 | +    /// ```rust,ignore | 
|  | 29 | +    /// let locked = mutex.lock(); | 
|  | 30 | +    /// if let Ok(thing) = locked { | 
|  | 31 | +    ///     do_thing(thing); | 
|  | 32 | +    /// } else { | 
|  | 33 | +    ///     use_locked(locked); | 
|  | 34 | +    /// } | 
|  | 35 | +    /// ``` | 
|  | 36 | +    pub IF_LET_MUTEX, | 
|  | 37 | +    correctness, | 
|  | 38 | +    "locking a `Mutex` in an `if let` block can cause deadlocks" | 
|  | 39 | +} | 
|  | 40 | + | 
|  | 41 | +declare_lint_pass!(IfLetMutex => [IF_LET_MUTEX]); | 
|  | 42 | + | 
|  | 43 | +impl LateLintPass<'_, '_> for IfLetMutex { | 
|  | 44 | +    fn check_expr(&mut self, cx: &LateContext<'_, '_>, ex: &'_ Expr<'_>) { | 
|  | 45 | +        let mut arm_visit = ArmVisitor { | 
|  | 46 | +            mutex_lock_called: false, | 
|  | 47 | +            found_mutex: None, | 
|  | 48 | +            cx, | 
|  | 49 | +        }; | 
|  | 50 | +        let mut op_visit = OppVisitor { | 
|  | 51 | +            mutex_lock_called: false, | 
|  | 52 | +            found_mutex: None, | 
|  | 53 | +            cx, | 
|  | 54 | +        }; | 
|  | 55 | +        if let ExprKind::Match( | 
|  | 56 | +            ref op, | 
|  | 57 | +            ref arms, | 
|  | 58 | +            MatchSource::IfLetDesugar { | 
|  | 59 | +                contains_else_clause: true, | 
|  | 60 | +            }, | 
|  | 61 | +        ) = ex.kind | 
|  | 62 | +        { | 
|  | 63 | +            op_visit.visit_expr(op); | 
|  | 64 | +            if op_visit.mutex_lock_called { | 
|  | 65 | +                for arm in *arms { | 
|  | 66 | +                    arm_visit.visit_arm(arm); | 
|  | 67 | +                } | 
|  | 68 | + | 
|  | 69 | +                if arm_visit.mutex_lock_called && arm_visit.same_mutex(cx, op_visit.found_mutex.unwrap()) { | 
|  | 70 | +                    span_lint_and_help( | 
|  | 71 | +                        cx, | 
|  | 72 | +                        IF_LET_MUTEX, | 
|  | 73 | +                        ex.span, | 
|  | 74 | +                        "calling `Mutex::lock` inside the scope of another `Mutex::lock` causes a deadlock", | 
|  | 75 | +                        None, | 
|  | 76 | +                        "move the lock call outside of the `if let ...` expression", | 
|  | 77 | +                    ); | 
|  | 78 | +                } | 
|  | 79 | +            } | 
|  | 80 | +        } | 
|  | 81 | +    } | 
|  | 82 | +} | 
|  | 83 | + | 
|  | 84 | +/// Checks if `Mutex::lock` is called in the `if let _ = expr. | 
|  | 85 | +pub struct OppVisitor<'tcx, 'l> { | 
|  | 86 | +    mutex_lock_called: bool, | 
|  | 87 | +    found_mutex: Option<&'tcx Expr<'tcx>>, | 
|  | 88 | +    cx: &'tcx LateContext<'tcx, 'l>, | 
|  | 89 | +} | 
|  | 90 | + | 
|  | 91 | +impl<'tcx, 'l> Visitor<'tcx> for OppVisitor<'tcx, 'l> { | 
|  | 92 | +    type Map = Map<'tcx>; | 
|  | 93 | + | 
|  | 94 | +    fn visit_expr(&mut self, expr: &'tcx Expr<'_>) { | 
|  | 95 | +        if_chain! { | 
|  | 96 | +            if let Some(mutex) = is_mutex_lock_call(self.cx, expr); | 
|  | 97 | +            then { | 
|  | 98 | +                self.found_mutex = Some(mutex); | 
|  | 99 | +                self.mutex_lock_called = true; | 
|  | 100 | +                return; | 
|  | 101 | +            } | 
|  | 102 | +        } | 
|  | 103 | +        visit::walk_expr(self, expr); | 
|  | 104 | +    } | 
|  | 105 | + | 
|  | 106 | +    fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> { | 
|  | 107 | +        NestedVisitorMap::None | 
|  | 108 | +    } | 
|  | 109 | +} | 
|  | 110 | + | 
|  | 111 | +/// Checks if `Mutex::lock` is called in any of the branches. | 
|  | 112 | +pub struct ArmVisitor<'tcx, 'l> { | 
|  | 113 | +    mutex_lock_called: bool, | 
|  | 114 | +    found_mutex: Option<&'tcx Expr<'tcx>>, | 
|  | 115 | +    cx: &'tcx LateContext<'tcx, 'l>, | 
|  | 116 | +} | 
|  | 117 | + | 
|  | 118 | +impl<'tcx, 'l> Visitor<'tcx> for ArmVisitor<'tcx, 'l> { | 
|  | 119 | +    type Map = Map<'tcx>; | 
|  | 120 | + | 
|  | 121 | +    fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) { | 
|  | 122 | +        if_chain! { | 
|  | 123 | +            if let Some(mutex) = is_mutex_lock_call(self.cx, expr); | 
|  | 124 | +            then { | 
|  | 125 | +                self.found_mutex = Some(mutex); | 
|  | 126 | +                self.mutex_lock_called = true; | 
|  | 127 | +                return; | 
|  | 128 | +            } | 
|  | 129 | +        } | 
|  | 130 | +        visit::walk_expr(self, expr); | 
|  | 131 | +    } | 
|  | 132 | + | 
|  | 133 | +    fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> { | 
|  | 134 | +        NestedVisitorMap::None | 
|  | 135 | +    } | 
|  | 136 | +} | 
|  | 137 | + | 
|  | 138 | +impl<'tcx, 'l> ArmVisitor<'tcx, 'l> { | 
|  | 139 | +    fn same_mutex(&self, cx: &LateContext<'_, '_>, op_mutex: &Expr<'_>) -> bool { | 
|  | 140 | +        if let Some(arm_mutex) = self.found_mutex { | 
|  | 141 | +            SpanlessEq::new(cx).eq_expr(op_mutex, arm_mutex) | 
|  | 142 | +        } else { | 
|  | 143 | +            false | 
|  | 144 | +        } | 
|  | 145 | +    } | 
|  | 146 | +} | 
|  | 147 | + | 
|  | 148 | +fn is_mutex_lock_call<'a>(cx: &LateContext<'a, '_>, expr: &'a Expr<'_>) -> Option<&'a Expr<'a>> { | 
|  | 149 | +    if_chain! { | 
|  | 150 | +        if let ExprKind::MethodCall(path, _span, args) = &expr.kind; | 
|  | 151 | +        if path.ident.to_string() == "lock"; | 
|  | 152 | +        let ty = cx.tables.expr_ty(&args[0]); | 
|  | 153 | +        if match_type(cx, ty, &paths::MUTEX); | 
|  | 154 | +        then { | 
|  | 155 | +            Some(&args[0]) | 
|  | 156 | +        } else { | 
|  | 157 | +            None | 
|  | 158 | +        } | 
|  | 159 | +    } | 
|  | 160 | +} | 
0 commit comments