Skip to content

Commit c54457c

Browse files
authored
Merge pull request #1199 from Manishearth/shadow-builtin
Add a `builtin_type_shadow` lint
2 parents 39d4a1b + d87f137 commit c54457c

File tree

7 files changed

+73
-3
lines changed

7 files changed

+73
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
All notable changes to this project will be documented in this file.
33

44
## 0.0.87 — ??
5+
* New lints: [`builtin_type_shadow`]
56
* Fix FP in [`zero_prefixed_literal`] and `0b`/`Oo`
67

78
## 0.0.86 — 2016-08-28
@@ -178,6 +179,7 @@ All notable changes to this project will be documented in this file.
178179
[`bool_comparison`]: https://github.com/Manishearth/rust-clippy/wiki#bool_comparison
179180
[`box_vec`]: https://github.com/Manishearth/rust-clippy/wiki#box_vec
180181
[`boxed_local`]: https://github.com/Manishearth/rust-clippy/wiki#boxed_local
182+
[`builtin_type_shadow`]: https://github.com/Manishearth/rust-clippy/wiki#builtin_type_shadow
181183
[`cast_possible_truncation`]: https://github.com/Manishearth/rust-clippy/wiki#cast_possible_truncation
182184
[`cast_possible_wrap`]: https://github.com/Manishearth/rust-clippy/wiki#cast_possible_wrap
183185
[`cast_precision_loss`]: https://github.com/Manishearth/rust-clippy/wiki#cast_precision_loss

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Table of contents:
1717

1818
## Lints
1919

20-
There are 169 lints included in this crate:
20+
There are 170 lints included in this crate:
2121

2222
name | default | triggers on
2323
---------------------------------------------------------------------------------------------------------------------|---------|----------------------------------------------------------------------------------------------------------------------------------
@@ -33,6 +33,7 @@ name
3333
[bool_comparison](https://github.com/Manishearth/rust-clippy/wiki#bool_comparison) | warn | comparing a variable to a boolean, e.g. `if x == true`
3434
[box_vec](https://github.com/Manishearth/rust-clippy/wiki#box_vec) | warn | usage of `Box<Vec<T>>`, vector elements are already on the heap
3535
[boxed_local](https://github.com/Manishearth/rust-clippy/wiki#boxed_local) | warn | using `Box<T>` where unnecessary
36+
[builtin_type_shadow](https://github.com/Manishearth/rust-clippy/wiki#builtin_type_shadow) | warn | shadowing a builtin type
3637
[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`
3738
[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`
3839
[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`

clippy_lints/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
378378
misc::MODULO_ONE,
379379
misc::REDUNDANT_PATTERN,
380380
misc::TOPLEVEL_REF_ARG,
381+
misc_early::BUILTIN_TYPE_SHADOW,
381382
misc_early::DOUBLE_NEG,
382383
misc_early::DUPLICATE_UNDERSCORE_ARGUMENT,
383384
misc_early::MIXED_CASE_HEX_LITERALS,

clippy_lints/src/misc_early.rs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::char;
44
use syntax::ast::*;
55
use syntax::codemap::Span;
66
use syntax::visit::FnKind;
7-
use utils::{span_lint, span_help_and_lint, snippet, snippet_opt, span_lint_and_then};
7+
use utils::{constants, span_lint, span_help_and_lint, snippet, snippet_opt, span_lint_and_then};
88

99
/// **What it does:** Checks for structure field patterns bound to wildcards.
1010
///
@@ -141,6 +141,27 @@ declare_lint! {
141141
"integer literals starting with `0`"
142142
}
143143

144+
/// **What it does:** Warns if a generic shadows a built-in type.
145+
///
146+
/// **Why is this bad?** This gives surprising type errors.
147+
///
148+
/// **Known problems:** None.
149+
///
150+
/// **Example:**
151+
///
152+
/// ```rust
153+
/// impl<u32> Foo<u32> {
154+
/// fn impl_func(&self) -> u32 {
155+
/// 42
156+
/// }
157+
/// }
158+
/// ```
159+
declare_lint! {
160+
pub BUILTIN_TYPE_SHADOW,
161+
Warn,
162+
"shadowing a builtin type"
163+
}
164+
144165

145166
#[derive(Copy, Clone)]
146167
pub struct MiscEarly;
@@ -149,11 +170,23 @@ impl LintPass for MiscEarly {
149170
fn get_lints(&self) -> LintArray {
150171
lint_array!(UNNEEDED_FIELD_PATTERN, DUPLICATE_UNDERSCORE_ARGUMENT, REDUNDANT_CLOSURE_CALL,
151172
DOUBLE_NEG, MIXED_CASE_HEX_LITERALS, UNSEPARATED_LITERAL_SUFFIX,
152-
ZERO_PREFIXED_LITERAL)
173+
ZERO_PREFIXED_LITERAL, BUILTIN_TYPE_SHADOW)
153174
}
154175
}
155176

156177
impl EarlyLintPass for MiscEarly {
178+
fn check_generics(&mut self, cx: &EarlyContext, gen: &Generics) {
179+
for ty in &gen.ty_params {
180+
let name = ty.ident.name.as_str();
181+
if constants::BUILTIN_TYPES.contains(&&*name) {
182+
span_lint(cx,
183+
BUILTIN_TYPE_SHADOW,
184+
ty.span,
185+
&format!("This generic shadows the built-in type `{}`", name));
186+
}
187+
}
188+
}
189+
157190
fn check_pat(&mut self, cx: &EarlyContext, pat: &Pat) {
158191
if let PatKind::Struct(ref npat, ref pfields, _) = pat.node {
159192
let mut wilds = 0;

clippy_lints/src/utils/constants.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//! This module contains some useful constants.
2+
3+
#![deny(missing_docs_in_private_items)]
4+
5+
/// List of the built-in types names.
6+
///
7+
/// See also [the reference][reference-types] for a list of such types.
8+
///
9+
/// [reference-types]: https://doc.rust-lang.org/reference.html#types
10+
pub const BUILTIN_TYPES: &'static [&'static str] = &[
11+
"i8", "u8",
12+
"i16", "u16",
13+
"i32", "u32",
14+
"i64", "u64",
15+
"isize", "usize",
16+
"f32",
17+
"f64",
18+
"bool",
19+
"str",
20+
"char",
21+
];

clippy_lints/src/utils/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use syntax::ptr::P;
2222
pub mod cargo;
2323
pub mod comparisons;
2424
pub mod conf;
25+
pub mod constants;
2526
mod hir;
2627
pub mod paths;
2728
pub mod sugg;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(plugin)]
2+
#![plugin(clippy)]
3+
#![deny(builtin_type_shadow)]
4+
5+
fn foo<u32>(a: u32) -> u32 { //~ERROR shadows the built-in type `u32`
6+
42 //~ERROR E0308
7+
// ^ rustc's type error
8+
}
9+
10+
fn main() {
11+
}

0 commit comments

Comments
 (0)