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

View file

@ -11,7 +11,7 @@ A collection of lints to catch common mistakes and improve your Rust code.
[Jump to link with clippy-service](#link-with-clippy-service)
##Lints
There are 134 lints included in this crate:
There are 135 lints included in this crate:
name | default | meaning
---------------------------------------------------------------------------------------------------------------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@ -19,6 +19,7 @@ name
[almost_swapped](https://github.com/Manishearth/rust-clippy/wiki#almost_swapped) | warn | `foo = bar; bar = foo` sequence
[approx_constant](https://github.com/Manishearth/rust-clippy/wiki#approx_constant) | warn | the approximate of a known float constant (in `std::f64::consts` or `std::f32::consts`) is found; suggests to use the constant
[bad_bit_mask](https://github.com/Manishearth/rust-clippy/wiki#bad_bit_mask) | warn | expressions of the form `_ & mask == select` that will only ever return `true` or `false` (because in the example `select` containing bits that `mask` doesn't have)
[blacklisted_name](https://github.com/Manishearth/rust-clippy/wiki#blacklisted_name) | warn | usage of a blacklisted/placeholder name
[block_in_if_condition_expr](https://github.com/Manishearth/rust-clippy/wiki#block_in_if_condition_expr) | warn | braces can be eliminated in conditions that are expressions, e.g `if { true } ...`
[block_in_if_condition_stmt](https://github.com/Manishearth/rust-clippy/wiki#block_in_if_condition_stmt) | warn | avoid complex blocks in conditions, instead move the block higher and bind it with 'let'; e.g: `if { let x = true; x } ...`
[bool_comparison](https://github.com/Manishearth/rust-clippy/wiki#bool_comparison) | warn | comparing a variable to a boolean, e.g. `if x == true`

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,

View file

@ -0,0 +1,26 @@
#![feature(plugin)]
#![plugin(clippy)]
#![allow(dead_code)]
#![allow(single_match)]
#![allow(unused_variables)]
#![deny(blacklisted_name)]
fn test(foo: ()) {} //~ERROR use of a blacklisted/placeholder name `foo`
fn main() {
let foo = 42; //~ERROR use of a blacklisted/placeholder name `foo`
let bar = 42; //~ERROR use of a blacklisted/placeholder name `bar`
let baz = 42; //~ERROR use of a blacklisted/placeholder name `baz`
let barb = 42;
let barbaric = 42;
match (42, Some(1337), Some(0)) {
(foo, Some(bar), baz @ Some(_)) => (),
//~^ ERROR use of a blacklisted/placeholder name `foo`
//~| ERROR use of a blacklisted/placeholder name `bar`
//~| ERROR use of a blacklisted/placeholder name `baz`
_ => (),
}
}

View file

@ -3,6 +3,7 @@
#![deny(clippy)]
#![allow(boxed_local)]
#![allow(blacklisted_name)]
macro_rules! boxit {
($init:expr, $x:ty) => {

View file

@ -0,0 +1,26 @@
#![feature(plugin)]
#![plugin(clippy(conf_file="./tests/compile-fail/conf_french_blacklisted_name.toml"))]
#![allow(dead_code)]
#![allow(single_match)]
#![allow(unused_variables)]
#![deny(blacklisted_name)]
fn test(toto: ()) {} //~ERROR use of a blacklisted/placeholder name `toto`
fn main() {
let toto = 42; //~ERROR use of a blacklisted/placeholder name `toto`
let tata = 42; //~ERROR use of a blacklisted/placeholder name `tata`
let titi = 42; //~ERROR use of a blacklisted/placeholder name `titi`
let tatab = 42;
let tatatataic = 42;
match (42, Some(1337), Some(0)) {
(toto, Some(tata), titi @ Some(_)) => (),
//~^ ERROR use of a blacklisted/placeholder name `toto`
//~| ERROR use of a blacklisted/placeholder name `tata`
//~| ERROR use of a blacklisted/placeholder name `titi`
_ => (),
}
}

View file

@ -0,0 +1 @@
blacklisted-names = ["toto", "tata", "titi"]

View file

@ -6,6 +6,7 @@
#![allow(needless_return)]
#![allow(unused_variables)]
#![allow(cyclomatic_complexity)]
#![allow(blacklisted_name)]
fn bar<T>(_: T) {}
fn foo() -> bool { unimplemented!() }

View file

@ -6,8 +6,7 @@
extern crate collections;
use collections::linked_list::LinkedList;
pub fn test(foo: LinkedList<u8>) { //~ ERROR I see you're using a LinkedList!
println!("{:?}", foo)
pub fn test(_: LinkedList<u8>) { //~ ERROR I see you're using a LinkedList!
}
fn main(){

View file

@ -2,7 +2,7 @@
#![plugin(clippy)]
#![deny(clippy, clippy_pedantic)]
#![allow(unused, print_stdout, non_ascii_literal, new_without_default)]
#![allow(blacklisted_name, unused, print_stdout, non_ascii_literal, new_without_default)]
use std::collections::BTreeMap;
use std::collections::HashMap;

View file

@ -20,8 +20,8 @@ impl MyStruct {
fn main() {
// Functions
takes_an_immutable_reference(&mut 42); //~ERROR The function/method "takes_an_immutable_reference" doesn't need a mutable reference
let foo: fn(&i32) = takes_an_immutable_reference;
foo(&mut 42); //~ERROR The function/method "foo" doesn't need a mutable reference
let as_ptr: fn(&i32) = takes_an_immutable_reference;
as_ptr(&mut 42); //~ERROR The function/method "as_ptr" doesn't need a mutable reference
// Methods
let my_struct = MyStruct;
@ -32,12 +32,12 @@ fn main() {
// Functions
takes_an_immutable_reference(&42);
let foo: fn(&i32) = takes_an_immutable_reference;
foo(&42);
let as_ptr: fn(&i32) = takes_an_immutable_reference;
as_ptr(&42);
takes_a_mutable_reference(&mut 42);
let foo: fn(&mut i32) = takes_a_mutable_reference;
foo(&mut 42);
let as_ptr: fn(&mut i32) = takes_a_mutable_reference;
as_ptr(&mut 42);
let a = &mut 42;
takes_an_immutable_reference(a);

View file

@ -2,6 +2,8 @@
#![plugin(clippy)]
#![deny(clippy)]
#![allow(blacklisted_name)]
/// Test that we lint if we use a binding with a single leading underscore
fn prefix_underscore(_foo: u32) -> u32 {
_foo + 1 //~ ERROR used binding which is prefixed with an underscore