From: mcarton Date: Mon, 22 Feb 2016 14:42:24 +0000 (+0100) Subject: Add a `BLACKLISTED_NAME` lint X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=a3031e34f9db46f172d955cec607c6f4ef226ab4;p=rust.git Add a `BLACKLISTED_NAME` lint --- diff --git a/README.md b/README.md index 48a106e3591..1b18a62f7a4 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/src/blacklisted_name.rs b/src/blacklisted_name.rs new file mode 100644 index 00000000000..2d62cc44d26 --- /dev/null +++ b/src/blacklisted_name.rs @@ -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, +} + +impl BlackListedName { + pub fn new(blacklist: Vec) -> 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)); + } + } + } +} diff --git a/src/lib.rs b/src/lib.rs index b69fdf70c99..66ae815d7e0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -48,6 +48,7 @@ fn main() { 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, diff --git a/tests/compile-fail/blacklisted_name.rs b/tests/compile-fail/blacklisted_name.rs new file mode 100755 index 00000000000..efcb810a30e --- /dev/null +++ b/tests/compile-fail/blacklisted_name.rs @@ -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` + _ => (), + } +} diff --git a/tests/compile-fail/box_vec.rs b/tests/compile-fail/box_vec.rs index 044e7dffa79..071945a81b2 100644 --- a/tests/compile-fail/box_vec.rs +++ b/tests/compile-fail/box_vec.rs @@ -3,6 +3,7 @@ #![deny(clippy)] #![allow(boxed_local)] +#![allow(blacklisted_name)] macro_rules! boxit { ($init:expr, $x:ty) => { diff --git a/tests/compile-fail/conf_french_blacklisted_name.rs b/tests/compile-fail/conf_french_blacklisted_name.rs new file mode 100755 index 00000000000..b7e29eeef1f --- /dev/null +++ b/tests/compile-fail/conf_french_blacklisted_name.rs @@ -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` + _ => (), + } +} diff --git a/tests/compile-fail/conf_french_blacklisted_name.toml b/tests/compile-fail/conf_french_blacklisted_name.toml new file mode 100644 index 00000000000..6abe5a3bbc2 --- /dev/null +++ b/tests/compile-fail/conf_french_blacklisted_name.toml @@ -0,0 +1 @@ +blacklisted-names = ["toto", "tata", "titi"] diff --git a/tests/compile-fail/copies.rs b/tests/compile-fail/copies.rs index c1e1ba68b3e..66457e77f47 100755 --- a/tests/compile-fail/copies.rs +++ b/tests/compile-fail/copies.rs @@ -6,6 +6,7 @@ #![allow(needless_return)] #![allow(unused_variables)] #![allow(cyclomatic_complexity)] +#![allow(blacklisted_name)] fn bar(_: T) {} fn foo() -> bool { unimplemented!() } diff --git a/tests/compile-fail/dlist.rs b/tests/compile-fail/dlist.rs index a800c045a50..e7919619121 100644 --- a/tests/compile-fail/dlist.rs +++ b/tests/compile-fail/dlist.rs @@ -6,8 +6,7 @@ extern crate collections; use collections::linked_list::LinkedList; -pub fn test(foo: LinkedList) { //~ ERROR I see you're using a LinkedList! - println!("{:?}", foo) +pub fn test(_: LinkedList) { //~ ERROR I see you're using a LinkedList! } fn main(){ diff --git a/tests/compile-fail/methods.rs b/tests/compile-fail/methods.rs index 46f14d5d921..344016a3b90 100644 --- a/tests/compile-fail/methods.rs +++ b/tests/compile-fail/methods.rs @@ -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; diff --git a/tests/compile-fail/mut_reference.rs b/tests/compile-fail/mut_reference.rs index 1d81ed14e4e..0bb59a318b8 100644 --- a/tests/compile-fail/mut_reference.rs +++ b/tests/compile-fail/mut_reference.rs @@ -20,8 +20,8 @@ fn takes_a_mutable_reference(&self, a: &mut i32) { 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); diff --git a/tests/compile-fail/used_underscore_binding.rs b/tests/compile-fail/used_underscore_binding.rs index 281d92c46df..6bf4324e623 100644 --- a/tests/compile-fail/used_underscore_binding.rs +++ b/tests/compile-fail/used_underscore_binding.rs @@ -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