]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/mutex_atomic.rs
Merge commit '0e87918536b9833bbc6c683d1f9d51ee2bf03ef1' into clippyup
[rust.git] / clippy_lints / src / mutex_atomic.rs
1 //! Checks for uses of mutex where an atomic value could be used
2 //!
3 //! This lint is **warn** by default
4
5 use clippy_utils::diagnostics::span_lint;
6 use clippy_utils::ty::is_type_diagnostic_item;
7 use rustc_hir::Expr;
8 use rustc_lint::{LateContext, LateLintPass};
9 use rustc_middle::ty::{self, Ty};
10 use rustc_session::{declare_lint_pass, declare_tool_lint};
11
12 declare_clippy_lint! {
13     /// **What it does:** Checks for usages of `Mutex<X>` where an atomic will do.
14     ///
15     /// **Why is this bad?** Using a mutex just to make access to a plain bool or
16     /// reference sequential is shooting flies with cannons.
17     /// `std::sync::atomic::AtomicBool` and `std::sync::atomic::AtomicPtr` are leaner and
18     /// faster.
19     ///
20     /// **Known problems:** This lint cannot detect if the mutex is actually used
21     /// for waiting before a critical section.
22     ///
23     /// **Example:**
24     /// ```rust
25     /// # let y = true;
26     ///
27     /// // Bad
28     /// # use std::sync::Mutex;
29     /// let x = Mutex::new(&y);
30     ///
31     /// // Good
32     /// # use std::sync::atomic::AtomicBool;
33     /// let x = AtomicBool::new(y);
34     /// ```
35     pub MUTEX_ATOMIC,
36     perf,
37     "using a mutex where an atomic value could be used instead"
38 }
39
40 declare_clippy_lint! {
41     /// **What it does:** Checks for usages of `Mutex<X>` where `X` is an integral
42     /// type.
43     ///
44     /// **Why is this bad?** Using a mutex just to make access to a plain integer
45     /// sequential is
46     /// shooting flies with cannons. `std::sync::atomic::AtomicUsize` is leaner and faster.
47     ///
48     /// **Known problems:** This lint cannot detect if the mutex is actually used
49     /// for waiting before a critical section.
50     ///
51     /// **Example:**
52     /// ```rust
53     /// # use std::sync::Mutex;
54     /// let x = Mutex::new(0usize);
55     ///
56     /// // Good
57     /// # use std::sync::atomic::AtomicUsize;
58     /// let x = AtomicUsize::new(0usize);
59     /// ```
60     pub MUTEX_INTEGER,
61     nursery,
62     "using a mutex for an integer type"
63 }
64
65 declare_lint_pass!(Mutex => [MUTEX_ATOMIC, MUTEX_INTEGER]);
66
67 impl<'tcx> LateLintPass<'tcx> for Mutex {
68     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
69         let ty = cx.typeck_results().expr_ty(expr);
70         if let ty::Adt(_, subst) = ty.kind() {
71             if is_type_diagnostic_item(cx, ty, sym!(mutex_type)) {
72                 let mutex_param = subst.type_at(0);
73                 if let Some(atomic_name) = get_atomic_name(mutex_param) {
74                     let msg = format!(
75                         "consider using an `{}` instead of a `Mutex` here; if you just want the locking \
76                          behavior and not the internal type, consider using `Mutex<()>`",
77                         atomic_name
78                     );
79                     match *mutex_param.kind() {
80                         ty::Uint(t) if t != ty::UintTy::Usize => span_lint(cx, MUTEX_INTEGER, expr.span, &msg),
81                         ty::Int(t) if t != ty::IntTy::Isize => span_lint(cx, MUTEX_INTEGER, expr.span, &msg),
82                         _ => span_lint(cx, MUTEX_ATOMIC, expr.span, &msg),
83                     };
84                 }
85             }
86         }
87     }
88 }
89
90 fn get_atomic_name(ty: Ty<'_>) -> Option<&'static str> {
91     match ty.kind() {
92         ty::Bool => Some("AtomicBool"),
93         ty::Uint(_) => Some("AtomicUsize"),
94         ty::Int(_) => Some("AtomicIsize"),
95         ty::RawPtr(_) => Some("AtomicPtr"),
96         _ => None,
97     }
98 }