about summary refs log tree commit diff
path: root/clippy_lints/src
diff options
context:
space:
mode:
authormcarton <cartonmartin+git@gmail.com>2016-08-28 01:52:01 +0200
committermcarton <cartonmartin+git@gmail.com>2016-08-28 19:56:18 +0200
commitd87f13725484fef6342f2fc190353579a3ffffb8 (patch)
tree54de6bf2a1ca34516635ba45d256e1a828c92df7 /clippy_lints/src
parent39d4a1b323da52c680c5b28e673125c0c0bcb543 (diff)
downloadrust-d87f13725484fef6342f2fc190353579a3ffffb8.tar.gz
rust-d87f13725484fef6342f2fc190353579a3ffffb8.zip
Add a `builtin_type_shadow` lint
Diffstat (limited to 'clippy_lints/src')
-rw-r--r--clippy_lints/src/lib.rs1
-rw-r--r--clippy_lints/src/misc_early.rs37
-rw-r--r--clippy_lints/src/utils/constants.rs21
-rw-r--r--clippy_lints/src/utils/mod.rs1
4 files changed, 58 insertions, 2 deletions
diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs
index c5ce53e66e5..12370fb2630 100644
--- a/clippy_lints/src/lib.rs
+++ b/clippy_lints/src/lib.rs
@@ -378,6 +378,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
         misc::MODULO_ONE,
         misc::REDUNDANT_PATTERN,
         misc::TOPLEVEL_REF_ARG,
+        misc_early::BUILTIN_TYPE_SHADOW,
         misc_early::DOUBLE_NEG,
         misc_early::DUPLICATE_UNDERSCORE_ARGUMENT,
         misc_early::MIXED_CASE_HEX_LITERALS,
diff --git a/clippy_lints/src/misc_early.rs b/clippy_lints/src/misc_early.rs
index 83ba980334a..61e4530a1df 100644
--- a/clippy_lints/src/misc_early.rs
+++ b/clippy_lints/src/misc_early.rs
@@ -4,7 +4,7 @@ use std::char;
 use syntax::ast::*;
 use syntax::codemap::Span;
 use syntax::visit::FnKind;
-use utils::{span_lint, span_help_and_lint, snippet, snippet_opt, span_lint_and_then};
+use utils::{constants, span_lint, span_help_and_lint, snippet, snippet_opt, span_lint_and_then};
 
 /// **What it does:** Checks for structure field patterns bound to wildcards.
 ///
@@ -141,6 +141,27 @@ declare_lint! {
     "integer literals starting with `0`"
 }
 
+/// **What it does:** Warns if a generic shadows a built-in type.
+///
+/// **Why is this bad?** This gives surprising type errors.
+///
+/// **Known problems:** None.
+///
+/// **Example:**
+///
+/// ```rust
+/// impl<u32> Foo<u32> {
+///     fn impl_func(&self) -> u32 {
+///         42
+///     }
+/// }
+/// ```
+declare_lint! {
+    pub BUILTIN_TYPE_SHADOW,
+    Warn,
+    "shadowing a builtin type"
+}
+
 
 #[derive(Copy, Clone)]
 pub struct MiscEarly;
@@ -149,11 +170,23 @@ impl LintPass for MiscEarly {
     fn get_lints(&self) -> LintArray {
         lint_array!(UNNEEDED_FIELD_PATTERN, DUPLICATE_UNDERSCORE_ARGUMENT, REDUNDANT_CLOSURE_CALL,
                     DOUBLE_NEG, MIXED_CASE_HEX_LITERALS, UNSEPARATED_LITERAL_SUFFIX,
-                    ZERO_PREFIXED_LITERAL)
+                    ZERO_PREFIXED_LITERAL, BUILTIN_TYPE_SHADOW)
     }
 }
 
 impl EarlyLintPass for MiscEarly {
+    fn check_generics(&mut self, cx: &EarlyContext, gen: &Generics) {
+        for ty in &gen.ty_params {
+            let name = ty.ident.name.as_str();
+            if constants::BUILTIN_TYPES.contains(&&*name) {
+                span_lint(cx,
+                          BUILTIN_TYPE_SHADOW,
+                          ty.span,
+                          &format!("This generic shadows the built-in type `{}`", name));
+            }
+        }
+    }
+
     fn check_pat(&mut self, cx: &EarlyContext, pat: &Pat) {
         if let PatKind::Struct(ref npat, ref pfields, _) = pat.node {
             let mut wilds = 0;
diff --git a/clippy_lints/src/utils/constants.rs b/clippy_lints/src/utils/constants.rs
new file mode 100644
index 00000000000..179c251e322
--- /dev/null
+++ b/clippy_lints/src/utils/constants.rs
@@ -0,0 +1,21 @@
+//! This module contains some useful constants.
+
+#![deny(missing_docs_in_private_items)]
+
+/// List of the built-in types names.
+///
+/// See also [the reference][reference-types] for a list of such types.
+///
+/// [reference-types]: https://doc.rust-lang.org/reference.html#types
+pub const BUILTIN_TYPES: &'static [&'static str] = &[
+    "i8", "u8",
+    "i16", "u16",
+    "i32", "u32",
+    "i64", "u64",
+    "isize", "usize",
+    "f32",
+    "f64",
+    "bool",
+    "str",
+    "char",
+];
diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs
index 9e9ff65ac5e..9e6ee0fbf1b 100644
--- a/clippy_lints/src/utils/mod.rs
+++ b/clippy_lints/src/utils/mod.rs
@@ -22,6 +22,7 @@ use syntax::ptr::P;
 pub mod cargo;
 pub mod comparisons;
 pub mod conf;
+pub mod constants;
 mod hir;
 pub mod paths;
 pub mod sugg;