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