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