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