]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/clippy_lints/src/mutex_atomic.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / mutex_atomic.rs
CommitLineData
ea8adc8c
XL
1//! Checks for uses of mutex where an atomic value could be used
2//!
3//! This lint is **warn** by default
4
abe05a73 5use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
ea8adc8c
XL
6use rustc::ty::{self, Ty};
7use rustc::hir::Expr;
8use syntax::ast;
9use 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/// ```
25declare_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
32/// type.
33///
34/// **Why is this bad?** Using a mutex just to make access to a plain integer
35/// sequential is
36/// shooting flies with cannons. `std::atomic::usize` is leaner and faster.
37///
38/// **Known problems:** This lint cannot detect if the mutex is actually used
39/// for waiting before a critical section.
40///
41/// **Example:**
42/// ```rust
43/// let x = Mutex::new(0usize);
44/// ```
45declare_lint! {
46 pub MUTEX_INTEGER,
47 Allow,
48 "using a mutex for an integer type"
49}
50
51impl LintPass for MutexAtomic {
52 fn get_lints(&self) -> LintArray {
53 lint_array!(MUTEX_ATOMIC, MUTEX_INTEGER)
54 }
55}
56
57pub struct MutexAtomic;
58
59impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MutexAtomic {
60 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
61 let ty = cx.tables.expr_ty(expr);
62 if let ty::TyAdt(_, subst) = ty.sty {
63 if match_type(cx, ty, &paths::MUTEX) {
64 let mutex_param = subst.type_at(0);
65 if let Some(atomic_name) = get_atomic_name(mutex_param) {
66 let msg = format!(
67 "Consider using an {} instead of a Mutex here. If you just want the locking \
abe05a73 68 behaviour and not the internal type, consider using Mutex<()>.",
ea8adc8c
XL
69 atomic_name
70 );
71 match mutex_param.sty {
72 ty::TyUint(t) if t != ast::UintTy::Us => span_lint(cx, MUTEX_INTEGER, expr.span, &msg),
73 ty::TyInt(t) if t != ast::IntTy::Is => span_lint(cx, MUTEX_INTEGER, expr.span, &msg),
74 _ => span_lint(cx, MUTEX_ATOMIC, expr.span, &msg),
75 };
76 }
77 }
78 }
79 }
80}
81
82fn get_atomic_name(ty: Ty) -> Option<(&'static str)> {
83 match ty.sty {
84 ty::TyBool => Some("AtomicBool"),
85 ty::TyUint(_) => Some("AtomicUsize"),
86 ty::TyInt(_) => Some("AtomicIsize"),
87 ty::TyRawPtr(_) => Some("AtomicPtr"),
88 _ => None,
89 }
90}