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