Add a lint for sized integer types in a mutex
This commit is contained in:
parent
f8aa0431bd
commit
26b2733b15
@ -6,7 +6,7 @@ A collection of lints to catch common mistakes and improve your Rust code.
|
||||
[Jump to usage instructions](#usage)
|
||||
|
||||
##Lints
|
||||
There are 60 lints included in this crate:
|
||||
There are 61 lints included in this crate:
|
||||
|
||||
name | default | meaning
|
||||
-------------------------------------------------------------------------------------------------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
@ -38,6 +38,7 @@ name
|
||||
[modulo_one](https://github.com/Manishearth/rust-clippy/wiki#modulo_one) | warn | taking a number modulo 1, which always returns 0
|
||||
[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)
|
||||
[mutex_atomic](https://github.com/Manishearth/rust-clippy/wiki#mutex_atomic) | warn | using a Mutex where an atomic value could be used instead
|
||||
[mutex_integer](https://github.com/Manishearth/rust-clippy/wiki#mutex_integer) | allow | using a Mutex for an integer type
|
||||
[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 }`
|
||||
[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
|
||||
[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
|
||||
|
@ -96,6 +96,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
|
||||
methods::RESULT_UNWRAP_USED,
|
||||
methods::WRONG_PUB_SELF_CONVENTION,
|
||||
mut_mut::MUT_MUT,
|
||||
mutex_atomic::MUTEX_INTEGER,
|
||||
ptr_arg::PTR_ARG,
|
||||
shadow::SHADOW_REUSE,
|
||||
shadow::SHADOW_SAME,
|
||||
|
@ -17,11 +17,18 @@ declare_lint! {
|
||||
"using a Mutex where an atomic value could be used instead"
|
||||
}
|
||||
|
||||
declare_lint! {
|
||||
pub MUTEX_INTEGER,
|
||||
Allow,
|
||||
"using a Mutex for an integer type"
|
||||
}
|
||||
|
||||
impl LintPass for MutexAtomic {
|
||||
fn get_lints(&self) -> LintArray {
|
||||
lint_array!(MUTEX_ATOMIC)
|
||||
lint_array!(MUTEX_ATOMIC, MUTEX_INTEGER)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MutexAtomic;
|
||||
|
||||
impl LateLintPass for MutexAtomic {
|
||||
@ -33,7 +40,13 @@ impl LateLintPass for MutexAtomic {
|
||||
if let Some(atomic_name) = get_atomic_name(mutex_param) {
|
||||
let msg = format!("Consider using an {} instead of a \
|
||||
Mutex here.", atomic_name);
|
||||
span_lint(cx, MUTEX_ATOMIC, expr.span, &msg);
|
||||
match *mutex_param {
|
||||
ty::TyUint(t) if t != ast::TyUs =>
|
||||
span_lint(cx, MUTEX_INTEGER, expr.span, &msg),
|
||||
ty::TyInt(t) if t != ast::TyIs =>
|
||||
span_lint(cx, MUTEX_INTEGER, expr.span, &msg),
|
||||
_ => span_lint(cx, MUTEX_ATOMIC, expr.span, &msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -43,8 +56,8 @@ impl LateLintPass for MutexAtomic {
|
||||
fn get_atomic_name(ty: &ty::TypeVariants) -> Option<(&'static str)> {
|
||||
match *ty {
|
||||
ty::TyBool => Some("AtomicBool"),
|
||||
ty::TyUint(ast::TyUs) => Some("AtomicUsize"),
|
||||
ty::TyInt(ast::TyIs) => Some("AtomicIsize"),
|
||||
ty::TyUint(_) => Some("AtomicUsize"),
|
||||
ty::TyInt(_) => Some("AtomicIsize"),
|
||||
ty::TyRawPtr(_) => Some("AtomicPtr"),
|
||||
_ => None
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
#![plugin(clippy)]
|
||||
#![deny(clippy)]
|
||||
#![deny(mutex_integer)]
|
||||
|
||||
fn main() {
|
||||
use std::sync::Mutex;
|
||||
@ -11,5 +12,7 @@ fn main() {
|
||||
let mut x = 4u32;
|
||||
Mutex::new(&x as *const u32); //~ERROR Consider using an AtomicPtr instead of a Mutex here.
|
||||
Mutex::new(&mut x as *mut u32); //~ERROR Consider using an AtomicPtr instead of a Mutex here.
|
||||
Mutex::new(0u32); //~ERROR Consider using an AtomicUsize instead of a Mutex here.
|
||||
Mutex::new(0i32); //~ERROR Consider using an AtomicIsize instead of a Mutex here.
|
||||
Mutex::new(0f32); // there are no float atomics, so this should not lint
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user