Add a BLACKLISTED_NAME lint

This commit is contained in:
mcarton 2016-02-22 15:42:24 +01:00
parent 232710cd43
commit a3031e34f9
12 changed files with 115 additions and 10 deletions

45
src/blacklisted_name.rs Normal file
View file

@ -0,0 +1,45 @@
use rustc::lint::*;
use rustc_front::hir::*;
use utils::span_lint;
/// **What it does:** This lints about usage of blacklisted names.
///
/// **Why is this bad?** These names are usually placeholder names and should be avoided.
///
/// **Known problems:** None.
///
/// **Example:** `let foo = 3.14;`
declare_lint! {
pub BLACKLISTED_NAME,
Warn,
"usage of a blacklisted/placeholder name"
}
#[derive(Clone, Debug)]
pub struct BlackListedName {
blacklist: Vec<String>,
}
impl BlackListedName {
pub fn new(blacklist: Vec<String>) -> BlackListedName {
BlackListedName {
blacklist: blacklist
}
}
}
impl LintPass for BlackListedName {
fn get_lints(&self) -> LintArray {
lint_array!(BLACKLISTED_NAME)
}
}
impl LateLintPass for BlackListedName {
fn check_pat(&mut self, cx: &LateContext, pat: &Pat) {
if let PatKind::Ident(_, ref ident, _) = pat.node {
if self.blacklist.iter().any(|s| s == &*ident.node.name.as_str()) {
span_lint(cx, BLACKLISTED_NAME, pat.span, &format!("use of a blacklisted/placeholder name `{}`", ident.node.name));
}
}
}
}

View file

@ -48,6 +48,7 @@ pub mod approx_const;
pub mod array_indexing;
pub mod attrs;
pub mod bit_mask;
pub mod blacklisted_name;
pub mod block_in_if_condition;
pub mod collapsible_if;
pub mod copies;
@ -204,6 +205,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
reg.register_late_lint_pass(box overflow_check_conditional::OverflowCheckConditional);
reg.register_late_lint_pass(box unused_label::UnusedLabel);
reg.register_late_lint_pass(box new_without_default::NewWithoutDefault);
reg.register_late_lint_pass(box blacklisted_name::BlackListedName::new(conf.blacklisted_names));
reg.register_lint_group("clippy_pedantic", vec![
array_indexing::INDEXING_SLICING,
@ -236,6 +238,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
attrs::INLINE_ALWAYS,
bit_mask::BAD_BIT_MASK,
bit_mask::INEFFECTIVE_BIT_MASK,
blacklisted_name::BLACKLISTED_NAME,
block_in_if_condition::BLOCK_IN_IF_CONDITION_EXPR,
block_in_if_condition::BLOCK_IN_IF_CONDITION_STMT,
collapsible_if::COLLAPSIBLE_IF,