]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/mutex_atomic.rs
Merge pull request #2962 from phansch/further_automate_pre_publish
[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 rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
6 use rustc::{declare_lint, lint_array};
7 use rustc::ty::{self, Ty};
8 use rustc::hir::Expr;
9 use syntax::ast;
10 use crate::utils::{match_type, paths, span_lint};
11
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::atomic::AtomicBool` and `std::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 x = Mutex::new(&y);
25 /// ```
26 declare_clippy_lint! {
27     pub MUTEX_ATOMIC,
28     perf,
29     "using a mutex where an atomic value could be used instead"
30 }
31
32 /// **What it does:** Checks for usages of `Mutex<X>` where `X` is an integral
33 /// type.
34 ///
35 /// **Why is this bad?** Using a mutex just to make access to a plain integer
36 /// sequential is
37 /// shooting flies with cannons. `std::atomic::usize` is leaner and faster.
38 ///
39 /// **Known problems:** This lint cannot detect if the mutex is actually used
40 /// for waiting before a critical section.
41 ///
42 /// **Example:**
43 /// ```rust
44 /// let x = Mutex::new(0usize);
45 /// ```
46 declare_clippy_lint! {
47     pub MUTEX_INTEGER,
48     nursery,
49     "using a mutex for an integer type"
50 }
51
52 impl LintPass for MutexAtomic {
53     fn get_lints(&self) -> LintArray {
54         lint_array!(MUTEX_ATOMIC, MUTEX_INTEGER)
55     }
56 }
57
58 pub struct MutexAtomic;
59
60 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MutexAtomic {
61     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
62         let ty = cx.tables.expr_ty(expr);
63         if let ty::Adt(_, subst) = ty.sty {
64             if match_type(cx, ty, &paths::MUTEX) {
65                 let mutex_param = subst.type_at(0);
66                 if let Some(atomic_name) = get_atomic_name(mutex_param) {
67                     let msg = format!(
68                         "Consider using an {} instead of a Mutex here. If you just want the locking \
69                          behaviour and not the internal type, consider using Mutex<()>.",
70                         atomic_name
71                     );
72                     match mutex_param.sty {
73                         ty::Uint(t) if t != ast::UintTy::Usize => span_lint(cx, MUTEX_INTEGER, expr.span, &msg),
74                         ty::Int(t) if t != ast::IntTy::Isize => span_lint(cx, MUTEX_INTEGER, expr.span, &msg),
75                         _ => span_lint(cx, MUTEX_ATOMIC, expr.span, &msg),
76                     };
77                 }
78             }
79         }
80     }
81 }
82
83 fn get_atomic_name(ty: Ty<'_>) -> Option<(&'static str)> {
84     match ty.sty {
85         ty::Bool => Some("AtomicBool"),
86         ty::Uint(_) => Some("AtomicUsize"),
87         ty::Int(_) => Some("AtomicIsize"),
88         ty::RawPtr(_) => Some("AtomicPtr"),
89         _ => None,
90     }
91 }