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