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