Suggest using an atomic value instead of a Mutex where possible
This commit is contained in:
parent
3e475e9588
commit
f8aa0431bd
5 changed files with 72 additions and 1 deletions
|
|
@ -47,6 +47,7 @@ pub mod loops;
|
|||
pub mod ranges;
|
||||
pub mod matches;
|
||||
pub mod precedence;
|
||||
pub mod mutex_atomic;
|
||||
|
||||
mod reexport {
|
||||
pub use syntax::ast::{Name, Ident, NodeId};
|
||||
|
|
@ -88,6 +89,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
|
|||
reg.register_late_lint_pass(box matches::MatchPass);
|
||||
reg.register_late_lint_pass(box misc::PatternPass);
|
||||
reg.register_late_lint_pass(box minmax::MinMaxPass);
|
||||
reg.register_late_lint_pass(box mutex_atomic::MutexAtomic);
|
||||
|
||||
reg.register_lint_group("clippy_pedantic", vec![
|
||||
methods::OPTION_UNWRAP_USED,
|
||||
|
|
@ -141,6 +143,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
|
|||
misc::REDUNDANT_PATTERN,
|
||||
misc::TOPLEVEL_REF_ARG,
|
||||
mut_reference::UNNECESSARY_MUT_PASSED,
|
||||
mutex_atomic::MUTEX_ATOMIC,
|
||||
needless_bool::NEEDLESS_BOOL,
|
||||
precedence::PRECEDENCE,
|
||||
ranges::RANGE_STEP_BY_ZERO,
|
||||
|
|
|
|||
51
src/mutex_atomic.rs
Normal file
51
src/mutex_atomic.rs
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
//! Checks for uses of Mutex where an atomic value could be used
|
||||
//!
|
||||
//! This lint is **warn** by default
|
||||
|
||||
use rustc::lint::{LintPass, LintArray, LateLintPass, LateContext};
|
||||
use rustc_front::hir::Expr;
|
||||
|
||||
use syntax::ast;
|
||||
use rustc::middle::ty;
|
||||
use rustc::middle::subst::ParamSpace;
|
||||
|
||||
use utils::{span_lint, MUTEX_PATH, match_type};
|
||||
|
||||
declare_lint! {
|
||||
pub MUTEX_ATOMIC,
|
||||
Warn,
|
||||
"using a Mutex where an atomic value could be used instead"
|
||||
}
|
||||
|
||||
impl LintPass for MutexAtomic {
|
||||
fn get_lints(&self) -> LintArray {
|
||||
lint_array!(MUTEX_ATOMIC)
|
||||
}
|
||||
}
|
||||
pub struct MutexAtomic;
|
||||
|
||||
impl LateLintPass for MutexAtomic {
|
||||
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
|
||||
let ty = cx.tcx.expr_ty(expr);
|
||||
if let &ty::TyStruct(_, subst) = &ty.sty {
|
||||
if match_type(cx, ty, &MUTEX_PATH) {
|
||||
let mutex_param = &subst.types.get(ParamSpace::TypeSpace, 0).sty;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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::TyRawPtr(_) => Some("AtomicPtr"),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
|
@ -14,6 +14,7 @@ pub const RESULT_PATH: [&'static str; 3] = ["core", "result", "Result"];
|
|||
pub const STRING_PATH: [&'static str; 3] = ["collections", "string", "String"];
|
||||
pub const VEC_PATH: [&'static str; 3] = ["collections", "vec", "Vec"];
|
||||
pub const LL_PATH: [&'static str; 3] = ["collections", "linked_list", "LinkedList"];
|
||||
pub const MUTEX_PATH: [&'static str; 4] = ["std", "sync", "mutex", "Mutex"];
|
||||
|
||||
/// returns true this expn_info was expanded by any macro
|
||||
pub fn in_macro(cx: &LateContext, span: Span) -> bool {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue