]> git.lizzy.rs Git - rust.git/commitdiff
Add a `BLACKLISTED_NAME` lint
authormcarton <cartonmartin+git@gmail.com>
Mon, 22 Feb 2016 14:42:24 +0000 (15:42 +0100)
committermcarton <cartonmartin+git@gmail.com>
Sat, 12 Mar 2016 13:51:45 +0000 (14:51 +0100)
12 files changed:
README.md
src/blacklisted_name.rs [new file with mode: 0644]
src/lib.rs
tests/compile-fail/blacklisted_name.rs [new file with mode: 0755]
tests/compile-fail/box_vec.rs
tests/compile-fail/conf_french_blacklisted_name.rs [new file with mode: 0755]
tests/compile-fail/conf_french_blacklisted_name.toml [new file with mode: 0644]
tests/compile-fail/copies.rs
tests/compile-fail/dlist.rs
tests/compile-fail/methods.rs
tests/compile-fail/mut_reference.rs
tests/compile-fail/used_underscore_binding.rs

index 48a106e3591d3a8b6f8d8c573eccef5efc6fc26a..1b18a62f7a4a497795f3dc2be132c0febcd87053 100644 (file)
--- 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 (file)
index 0000000..2d62cc4
--- /dev/null
@@ -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));
+            }
+        }
+    }
+}
index b69fdf70c9985e8f967fad1255527ff3719117d1..66ae815d7e00c91c2faedda3409d7113be89337a 100644 (file)
@@ -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 (executable)
index 0000000..efcb810
--- /dev/null
@@ -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`
+        _ => (),
+    }
+}
index 044e7dffa7944d405436dfe43b07b958eea05943..071945a81b23c5d804780b54fc9e7042bed24b1a 100644 (file)
@@ -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 (executable)
index 0000000..b7e29ee
--- /dev/null
@@ -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 (file)
index 0000000..6abe5a3
--- /dev/null
@@ -0,0 +1 @@
+blacklisted-names = ["toto", "tata", "titi"]
index c1e1ba68b3e71d2659ff670ed8da045f91dd666d..66457e77f476422104b577461ae664150f7fa099 100755 (executable)
@@ -6,6 +6,7 @@
 #![allow(needless_return)]
 #![allow(unused_variables)]
 #![allow(cyclomatic_complexity)]
+#![allow(blacklisted_name)]
 
 fn bar<T>(_: T) {}
 fn foo() -> bool { unimplemented!() }
index a800c045a502a8e6843792e30fab3b3d4b3bca73..e7919619121297f353abfaad2983f7246d6d1ee3 100644 (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(){
index 46f14d5d921c83d8f5c9e8f147bd4fce5ea7514e..344016a3b909d53a4cd341e20445a756e91a50cf 100644 (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;
index 1d81ed14e4edce3791a3368090cfed1c761b7129..0bb59a318b8349a91ec84f884a8df4e8b2b48fc4 100644 (file)
@@ -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);
index 281d92c46df8114048e22c7efb173ae33ff2f887..6bf4324e623756a825ee48ca3b60d6fb504f58c0 100644 (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