Skip to content

Commit 26b2733

Browse files
committed
Add a lint for sized integer types in a mutex
1 parent f8aa043 commit 26b2733

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
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 60 lints included in this crate:
9+
There are 61 lints included in this crate:
1010

1111
name | default | meaning
1212
-------------------------------------------------------------------------------------------------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@@ -38,6 +38,7 @@ name
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)
4040
[mutex_atomic](https://github.com/Manishearth/rust-clippy/wiki#mutex_atomic) | warn | using a Mutex where an atomic value could be used instead
41+
[mutex_integer](https://github.com/Manishearth/rust-clippy/wiki#mutex_integer) | allow | using a Mutex for an integer type
4142
[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 }`
4243
[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
4344
[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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
9696
methods::RESULT_UNWRAP_USED,
9797
methods::WRONG_PUB_SELF_CONVENTION,
9898
mut_mut::MUT_MUT,
99+
mutex_atomic::MUTEX_INTEGER,
99100
ptr_arg::PTR_ARG,
100101
shadow::SHADOW_REUSE,
101102
shadow::SHADOW_SAME,

src/mutex_atomic.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,18 @@ declare_lint! {
1717
"using a Mutex where an atomic value could be used instead"
1818
}
1919

20+
declare_lint! {
21+
pub MUTEX_INTEGER,
22+
Allow,
23+
"using a Mutex for an integer type"
24+
}
25+
2026
impl LintPass for MutexAtomic {
2127
fn get_lints(&self) -> LintArray {
22-
lint_array!(MUTEX_ATOMIC)
28+
lint_array!(MUTEX_ATOMIC, MUTEX_INTEGER)
2329
}
2430
}
31+
2532
pub struct MutexAtomic;
2633

2734
impl LateLintPass for MutexAtomic {
@@ -33,7 +40,13 @@ impl LateLintPass for MutexAtomic {
3340
if let Some(atomic_name) = get_atomic_name(mutex_param) {
3441
let msg = format!("Consider using an {} instead of a \
3542
Mutex here.", atomic_name);
36-
span_lint(cx, MUTEX_ATOMIC, expr.span, &msg);
43+
match *mutex_param {
44+
ty::TyUint(t) if t != ast::TyUs =>
45+
span_lint(cx, MUTEX_INTEGER, expr.span, &msg),
46+
ty::TyInt(t) if t != ast::TyIs =>
47+
span_lint(cx, MUTEX_INTEGER, expr.span, &msg),
48+
_ => span_lint(cx, MUTEX_ATOMIC, expr.span, &msg)
49+
}
3750
}
3851
}
3952
}
@@ -43,8 +56,8 @@ impl LateLintPass for MutexAtomic {
4356
fn get_atomic_name(ty: &ty::TypeVariants) -> Option<(&'static str)> {
4457
match *ty {
4558
ty::TyBool => Some("AtomicBool"),
46-
ty::TyUint(ast::TyUs) => Some("AtomicUsize"),
47-
ty::TyInt(ast::TyIs) => Some("AtomicIsize"),
59+
ty::TyUint(_) => Some("AtomicUsize"),
60+
ty::TyInt(_) => Some("AtomicIsize"),
4861
ty::TyRawPtr(_) => Some("AtomicPtr"),
4962
_ => None
5063
}

tests/compile-fail/mutex_atomic.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#![plugin(clippy)]
44
#![deny(clippy)]
5+
#![deny(mutex_integer)]
56

67
fn main() {
78
use std::sync::Mutex;
@@ -11,5 +12,7 @@ fn main() {
1112
let mut x = 4u32;
1213
Mutex::new(&x as *const u32); //~ERROR Consider using an AtomicPtr instead of a Mutex here.
1314
Mutex::new(&mut x as *mut u32); //~ERROR Consider using an AtomicPtr instead of a Mutex here.
15+
Mutex::new(0u32); //~ERROR Consider using an AtomicUsize instead of a Mutex here.
16+
Mutex::new(0i32); //~ERROR Consider using an AtomicIsize instead of a Mutex here.
1417
Mutex::new(0f32); // there are no float atomics, so this should not lint
1518
}

0 commit comments

Comments
 (0)