]> git.lizzy.rs Git - rust.git/commitdiff
Add a `builtin_type_shadow` lint
authormcarton <cartonmartin+git@gmail.com>
Sat, 27 Aug 2016 23:52:01 +0000 (01:52 +0200)
committermcarton <cartonmartin+git@gmail.com>
Sun, 28 Aug 2016 17:56:18 +0000 (19:56 +0200)
CHANGELOG.md
README.md
clippy_lints/src/lib.rs
clippy_lints/src/misc_early.rs
clippy_lints/src/utils/constants.rs [new file with mode: 0644]
clippy_lints/src/utils/mod.rs
tests/compile-fail/builtin-type-shadow.rs [new file with mode: 0644]

index 636ca3f4f2bebe12a175cf3902a7a4bb143eecf5..6ef4e596a08c5d38b95170ed2501efaa07c80710 100644 (file)
@@ -2,6 +2,7 @@
 All notable changes to this project will be documented in this file.
 
 ## 0.0.87 — ??
+* New lints: [`builtin_type_shadow`]
 * Fix FP in [`zero_prefixed_literal`] and `0b`/`Oo`
 
 ## 0.0.86 — 2016-08-28
@@ -178,6 +179,7 @@ All notable changes to this project will be documented in this file.
 [`bool_comparison`]: https://github.com/Manishearth/rust-clippy/wiki#bool_comparison
 [`box_vec`]: https://github.com/Manishearth/rust-clippy/wiki#box_vec
 [`boxed_local`]: https://github.com/Manishearth/rust-clippy/wiki#boxed_local
+[`builtin_type_shadow`]: https://github.com/Manishearth/rust-clippy/wiki#builtin_type_shadow
 [`cast_possible_truncation`]: https://github.com/Manishearth/rust-clippy/wiki#cast_possible_truncation
 [`cast_possible_wrap`]: https://github.com/Manishearth/rust-clippy/wiki#cast_possible_wrap
 [`cast_precision_loss`]: https://github.com/Manishearth/rust-clippy/wiki#cast_precision_loss
index 64d2932ed668e7b4a4aa81399b427feee24ebf8c..d305908ea627146a774949330c0cc141540f20d8 100644 (file)
--- a/README.md
+++ b/README.md
@@ -17,7 +17,7 @@ Table of contents:
 
 ## Lints
 
-There are 169 lints included in this crate:
+There are 170 lints included in this crate:
 
 name                                                                                                                 | default | triggers on
 ---------------------------------------------------------------------------------------------------------------------|---------|----------------------------------------------------------------------------------------------------------------------------------
@@ -33,6 +33,7 @@ name
 [bool_comparison](https://github.com/Manishearth/rust-clippy/wiki#bool_comparison)                                   | warn    | comparing a variable to a boolean, e.g. `if x == true`
 [box_vec](https://github.com/Manishearth/rust-clippy/wiki#box_vec)                                                   | warn    | usage of `Box<Vec<T>>`, vector elements are already on the heap
 [boxed_local](https://github.com/Manishearth/rust-clippy/wiki#boxed_local)                                           | warn    | using `Box<T>` where unnecessary
+[builtin_type_shadow](https://github.com/Manishearth/rust-clippy/wiki#builtin_type_shadow)                           | warn    | shadowing a builtin type
 [cast_possible_truncation](https://github.com/Manishearth/rust-clippy/wiki#cast_possible_truncation)                 | allow   | casts that may cause truncation of the value, e.g `x as u8` where `x: u32`, or `x as i32` where `x: f32`
 [cast_possible_wrap](https://github.com/Manishearth/rust-clippy/wiki#cast_possible_wrap)                             | allow   | casts that may cause wrapping around the value, e.g `x as i32` where `x: u32` and `x > i32::MAX`
 [cast_precision_loss](https://github.com/Manishearth/rust-clippy/wiki#cast_precision_loss)                           | allow   | casts that cause loss of precision, e.g `x as f32` where `x: u64`
index c5ce53e66e5b5620c94f058fac2195c18871590e..12370fb2630092613b33405780a797cf25d3fe24 100644 (file)
@@ -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,
index 83ba980334af4612111375f425665b3d9f67f9e9..61e4530a1df42e846cbaf9a61190c253899760f6 100644 (file)
@@ -4,7 +4,7 @@
 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.
 ///
     "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 (file)
index 0000000..179c251
--- /dev/null
@@ -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",
+];
index 9e9ff65ac5ed3b773369aa0a96f385d9210decd9..9e6ee0fbf1b76f8326383e61e00c555eec8c9e97 100644 (file)
@@ -22,6 +22,7 @@
 pub mod cargo;
 pub mod comparisons;
 pub mod conf;
+pub mod constants;
 mod hir;
 pub mod paths;
 pub mod sugg;
diff --git a/tests/compile-fail/builtin-type-shadow.rs b/tests/compile-fail/builtin-type-shadow.rs
new file mode 100644 (file)
index 0000000..172875a
--- /dev/null
@@ -0,0 +1,11 @@
+#![feature(plugin)]
+#![plugin(clippy)]
+#![deny(builtin_type_shadow)]
+
+fn foo<u32>(a: u32) -> u32 { //~ERROR shadows the built-in type `u32`
+    42  //~ERROR E0308
+    // ^ rustc's type error
+}
+
+fn main() {
+}