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