]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/mutex_atomic.rs
Merge pull request #3265 from mikerite/fix-export
[rust.git] / clippy_lints / src / mutex_atomic.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10
11 //! Checks for uses of mutex where an atomic value could be used
12 //!
13 //! This lint is **warn** by default
14
15 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
16 use crate::rustc::{declare_tool_lint, lint_array};
17 use crate::rustc::ty::{self, Ty};
18 use crate::rustc::hir::Expr;
19 use crate::syntax::ast;
20 use crate::utils::{match_type, paths, span_lint};
21
22 /// **What it does:** Checks for usages of `Mutex<X>` where an atomic will do.
23 ///
24 /// **Why is this bad?** Using a mutex just to make access to a plain bool or
25 /// reference sequential is shooting flies with cannons.
26 /// `std::atomic::AtomicBool` and `std::atomic::AtomicPtr` are leaner and
27 /// faster.
28 ///
29 /// **Known problems:** This lint cannot detect if the mutex is actually used
30 /// for waiting before a critical section.
31 ///
32 /// **Example:**
33 /// ```rust
34 /// let x = Mutex::new(&y);
35 /// ```
36 declare_clippy_lint! {
37     pub MUTEX_ATOMIC,
38     perf,
39     "using a mutex where an atomic value could be used instead"
40 }
41
42 /// **What it does:** Checks for usages of `Mutex<X>` where `X` is an integral
43 /// type.
44 ///
45 /// **Why is this bad?** Using a mutex just to make access to a plain integer
46 /// sequential is
47 /// shooting flies with cannons. `std::atomic::usize` is leaner and faster.
48 ///
49 /// **Known problems:** This lint cannot detect if the mutex is actually used
50 /// for waiting before a critical section.
51 ///
52 /// **Example:**
53 /// ```rust
54 /// let x = Mutex::new(0usize);
55 /// ```
56 declare_clippy_lint! {
57     pub MUTEX_INTEGER,
58     nursery,
59     "using a mutex for an integer type"
60 }
61
62 impl LintPass for MutexAtomic {
63     fn get_lints(&self) -> LintArray {
64         lint_array!(MUTEX_ATOMIC, MUTEX_INTEGER)
65     }
66 }
67
68 pub struct MutexAtomic;
69
70 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MutexAtomic {
71     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
72         let ty = cx.tables.expr_ty(expr);
73         if let ty::Adt(_, subst) = ty.sty {
74             if match_type(cx, ty, &paths::MUTEX) {
75                 let mutex_param = subst.type_at(0);
76                 if let Some(atomic_name) = get_atomic_name(mutex_param) {
77                     let msg = format!(
78                         "Consider using an {} instead of a Mutex here. If you just want the locking \
79                          behaviour and not the internal type, consider using Mutex<()>.",
80                         atomic_name
81                     );
82                     match mutex_param.sty {
83                         ty::Uint(t) if t != ast::UintTy::Usize => span_lint(cx, MUTEX_INTEGER, expr.span, &msg),
84                         ty::Int(t) if t != ast::IntTy::Isize => span_lint(cx, MUTEX_INTEGER, expr.span, &msg),
85                         _ => span_lint(cx, MUTEX_ATOMIC, expr.span, &msg),
86                     };
87                 }
88             }
89         }
90     }
91 }
92
93 fn get_atomic_name(ty: Ty<'_>) -> Option<(&'static str)> {
94     match ty.sty {
95         ty::Bool => Some("AtomicBool"),
96         ty::Uint(_) => Some("AtomicUsize"),
97         ty::Int(_) => Some("AtomicIsize"),
98         ty::RawPtr(_) => Some("AtomicPtr"),
99         _ => None,
100     }
101 }