]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/mut_mutex_lock.rs
Inline is_mut_mutex_lock_call
[rust.git] / clippy_lints / src / mut_mutex_lock.rs
1 use crate::utils::{is_type_diagnostic_item, span_lint_and_help};
2 use if_chain::if_chain;
3 use rustc_hir::{Expr, ExprKind, Mutability};
4 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_middle::ty;
6 use rustc_session::{declare_lint_pass, declare_tool_lint};
7
8 declare_clippy_lint! {
9     /// **What it does:** Checks for `&mut Mutex::lock` calls
10     ///
11     /// **Why is this bad?** `Mutex::lock` is less efficient than
12     /// calling `Mutex::get_mut`
13     ///
14     /// **Known problems:** None.
15     ///
16     /// **Example:**
17     ///
18     /// ```rust
19     /// use std::sync::{Arc, Mutex};
20     ///
21     /// let mut value_rc = Arc::new(Mutex::new(42_u8));
22     /// let value_mutex = Arc::get_mut(&mut value_rc).unwrap();
23     ///
24     /// let mut value = value_mutex.lock().unwrap();
25     /// *value += 1;
26     /// ```
27     /// Use instead:
28     /// ```rust
29     /// use std::sync::{Arc, Mutex};
30     ///
31     /// let mut value_rc = Arc::new(Mutex::new(42_u8));
32     /// let value_mutex = Arc::get_mut(&mut value_rc).unwrap();
33     ///
34     /// let value = value_mutex.get_mut().unwrap();
35     /// *value += 1;
36     /// ```
37     pub MUT_MUTEX_LOCK,
38     correctness,
39     "`&mut Mutex::lock` does unnecessary locking"
40 }
41
42 declare_lint_pass!(MutMutexLock => [MUT_MUTEX_LOCK]);
43
44 impl<'tcx> LateLintPass<'tcx> for MutMutexLock {
45     fn check_expr(&mut self, cx: &LateContext<'tcx>, ex: &'tcx Expr<'tcx>) {
46         if_chain! {
47             if let ExprKind::MethodCall(path, _span, args, _) = &ex.kind;
48             if path.ident.name == sym!(lock);
49             let ty = cx.typeck_results().expr_ty(&args[0]);
50             if let ty::Ref(_, inner_ty, Mutability::Mut) = ty.kind();
51             if is_type_diagnostic_item(cx, inner_ty, sym!(mutex_type));
52             then {
53                 span_lint_and_help(
54                     cx,
55                     MUT_MUTEX_LOCK,
56                     ex.span,
57                     "calling `&mut Mutex::lock` unnecessarily locks an exclusive (mutable) reference",
58                     None,
59                     "use `&mut Mutex::get_mut` instead",
60                 );
61             }
62         }
63     }
64 }