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