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