Skip to content

Commit f8aa043

Browse files
committed
Suggest using an atomic value instead of a Mutex where possible
1 parent 3e475e9 commit f8aa043

File tree

5 files changed

+72
-1
lines changed

5 files changed

+72
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ A collection of lints to catch common mistakes and improve your Rust code.
66
[Jump to usage instructions](#usage)
77

88
##Lints
9-
There are 59 lints included in this crate:
9+
There are 60 lints included in this crate:
1010

1111
name | default | meaning
1212
-------------------------------------------------------------------------------------------------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@@ -37,6 +37,7 @@ name
3737
[min_max](https://github.com/Manishearth/rust-clippy/wiki#min_max) | warn | `min(_, max(_, _))` (or vice versa) with bounds clamping the result to a constant
3838
[modulo_one](https://github.com/Manishearth/rust-clippy/wiki#modulo_one) | warn | taking a number modulo 1, which always returns 0
3939
[mut_mut](https://github.com/Manishearth/rust-clippy/wiki#mut_mut) | allow | usage of double-mut refs, e.g. `&mut &mut ...` (either copy'n'paste error, or shows a fundamental misunderstanding of references)
40+
[mutex_atomic](https://github.com/Manishearth/rust-clippy/wiki#mutex_atomic) | warn | using a Mutex where an atomic value could be used instead
4041
[needless_bool](https://github.com/Manishearth/rust-clippy/wiki#needless_bool) | warn | if-statements with plain booleans in the then- and else-clause, e.g. `if p { true } else { false }`
4142
[needless_lifetimes](https://github.com/Manishearth/rust-clippy/wiki#needless_lifetimes) | warn | using explicit lifetimes for references in function arguments when elision rules would allow omitting them
4243
[needless_range_loop](https://github.com/Manishearth/rust-clippy/wiki#needless_range_loop) | warn | for-looping over a range of indices where an iterator over items would do

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub mod loops;
4747
pub mod ranges;
4848
pub mod matches;
4949
pub mod precedence;
50+
pub mod mutex_atomic;
5051

5152
mod reexport {
5253
pub use syntax::ast::{Name, Ident, NodeId};
@@ -88,6 +89,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
8889
reg.register_late_lint_pass(box matches::MatchPass);
8990
reg.register_late_lint_pass(box misc::PatternPass);
9091
reg.register_late_lint_pass(box minmax::MinMaxPass);
92+
reg.register_late_lint_pass(box mutex_atomic::MutexAtomic);
9193

9294
reg.register_lint_group("clippy_pedantic", vec![
9395
methods::OPTION_UNWRAP_USED,
@@ -141,6 +143,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
141143
misc::REDUNDANT_PATTERN,
142144
misc::TOPLEVEL_REF_ARG,
143145
mut_reference::UNNECESSARY_MUT_PASSED,
146+
mutex_atomic::MUTEX_ATOMIC,
144147
needless_bool::NEEDLESS_BOOL,
145148
precedence::PRECEDENCE,
146149
ranges::RANGE_STEP_BY_ZERO,

src/mutex_atomic.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
declare_lint! {
15+
pub MUTEX_ATOMIC,
16+
Warn,
17+
"using a Mutex where an atomic value could be used instead"
18+
}
19+
20+
impl LintPass for MutexAtomic {
21+
fn get_lints(&self) -> LintArray {
22+
lint_array!(MUTEX_ATOMIC)
23+
}
24+
}
25+
pub struct MutexAtomic;
26+
27+
impl LateLintPass for MutexAtomic {
28+
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
29+
let ty = cx.tcx.expr_ty(expr);
30+
if let &ty::TyStruct(_, subst) = &ty.sty {
31+
if match_type(cx, ty, &MUTEX_PATH) {
32+
let mutex_param = &subst.types.get(ParamSpace::TypeSpace, 0).sty;
33+
if let Some(atomic_name) = get_atomic_name(mutex_param) {
34+
let msg = format!("Consider using an {} instead of a \
35+
Mutex here.", atomic_name);
36+
span_lint(cx, MUTEX_ATOMIC, expr.span, &msg);
37+
}
38+
}
39+
}
40+
}
41+
}
42+
43+
fn get_atomic_name(ty: &ty::TypeVariants) -> Option<(&'static str)> {
44+
match *ty {
45+
ty::TyBool => Some("AtomicBool"),
46+
ty::TyUint(ast::TyUs) => Some("AtomicUsize"),
47+
ty::TyInt(ast::TyIs) => Some("AtomicIsize"),
48+
ty::TyRawPtr(_) => Some("AtomicPtr"),
49+
_ => None
50+
}
51+
}

src/utils.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub const RESULT_PATH: [&'static str; 3] = ["core", "result", "Result"];
1414
pub const STRING_PATH: [&'static str; 3] = ["collections", "string", "String"];
1515
pub const VEC_PATH: [&'static str; 3] = ["collections", "vec", "Vec"];
1616
pub const LL_PATH: [&'static str; 3] = ["collections", "linked_list", "LinkedList"];
17+
pub const MUTEX_PATH: [&'static str; 4] = ["std", "sync", "mutex", "Mutex"];
1718

1819
/// returns true this expn_info was expanded by any macro
1920
pub fn in_macro(cx: &LateContext, span: Span) -> bool {

tests/compile-fail/mutex_atomic.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![feature(plugin)]
2+
3+
#![plugin(clippy)]
4+
#![deny(clippy)]
5+
6+
fn main() {
7+
use std::sync::Mutex;
8+
Mutex::new(true); //~ERROR Consider using an AtomicBool instead of a Mutex here.
9+
Mutex::new(5usize); //~ERROR Consider using an AtomicUsize instead of a Mutex here.
10+
Mutex::new(9isize); //~ERROR Consider using an AtomicIsize instead of a Mutex here.
11+
let mut x = 4u32;
12+
Mutex::new(&x as *const u32); //~ERROR Consider using an AtomicPtr instead of a Mutex here.
13+
Mutex::new(&mut x as *mut u32); //~ERROR Consider using an AtomicPtr instead of a Mutex here.
14+
Mutex::new(0f32); // there are no float atomics, so this should not lint
15+
}

0 commit comments

Comments
 (0)