2015-02-25 05:44:44 -06:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-02-25 06:03:44 -06:00
|
|
|
//! Lints in the Rust compiler.
|
|
|
|
//!
|
|
|
|
//! This currently only contains the definitions and implementations
|
|
|
|
//! of most of the lints that `rustc` supports directly, it does not
|
|
|
|
//! contain the infrastructure for defining/registering lints. That is
|
|
|
|
//! available in `rustc::lint` and `rustc::plugin` respectively.
|
2015-02-25 05:44:44 -06:00
|
|
|
//!
|
|
|
|
//! # Note
|
|
|
|
//!
|
|
|
|
//! This API is completely unstable and subject to change.
|
|
|
|
|
2015-03-05 10:53:51 -06:00
|
|
|
// Do not remove on snapshot creation. Needed for bootstrap. (Issue #22364)
|
|
|
|
#![cfg_attr(stage0, feature(custom_attribute))]
|
2015-02-25 05:44:44 -06:00
|
|
|
#![crate_name = "rustc_lint"]
|
|
|
|
#![unstable(feature = "rustc_private")]
|
|
|
|
#![staged_api]
|
|
|
|
#![crate_type = "dylib"]
|
|
|
|
#![crate_type = "rlib"]
|
|
|
|
#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
|
|
|
|
html_favicon_url = "http://www.rust-lang.org/favicon.ico",
|
|
|
|
html_root_url = "http://doc.rust-lang.org/nightly/")]
|
|
|
|
|
|
|
|
#![feature(box_patterns)]
|
|
|
|
#![feature(box_syntax)]
|
|
|
|
#![feature(collections)]
|
|
|
|
#![feature(core)]
|
|
|
|
#![feature(quote)]
|
|
|
|
#![feature(rustc_diagnostic_macros)]
|
|
|
|
#![feature(rustc_private)]
|
|
|
|
#![feature(staged_api)]
|
2015-03-10 18:29:02 -05:00
|
|
|
#![feature(str_char)]
|
2015-02-25 05:44:44 -06:00
|
|
|
#![cfg_attr(test, feature(test))]
|
|
|
|
|
|
|
|
extern crate syntax;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate rustc;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
|
|
|
|
|
|
|
pub use rustc::lint as lint;
|
|
|
|
pub use rustc::metadata as metadata;
|
|
|
|
pub use rustc::middle as middle;
|
|
|
|
pub use rustc::session as session;
|
|
|
|
pub use rustc::util as util;
|
|
|
|
|
|
|
|
use session::Session;
|
Add trivial cast lints.
This permits all coercions to be performed in casts, but adds lints to warn in those cases.
Part of this patch moves cast checking to a later stage of type checking. We acquire obligations to check casts as part of type checking where we previously checked them. Once we have type checked a function or module, then we check any cast obligations which have been acquired. That means we have more type information available to check casts (this was crucial to making coercions work properly in place of some casts), but it means that casts cannot feed input into type inference.
[breaking change]
* Adds two new lints for trivial casts and trivial numeric casts, these are warn by default, but can cause errors if you build with warnings as errors. Previously, trivial numeric casts and casts to trait objects were allowed.
* The unused casts lint has gone.
* Interactions between casting and type inference have changed in subtle ways. Two ways this might manifest are:
- You may need to 'direct' casts more with extra type information, for example, in some cases where `foo as _ as T` succeeded, you may now need to specify the type for `_`
- Casts do not influence inference of integer types. E.g., the following used to type check:
```
let x = 42;
let y = &x as *const u32;
```
Because the cast would inform inference that `x` must have type `u32`. This no longer applies and the compiler will fallback to `i32` for `x` and thus there will be a type error in the cast. The solution is to add more type information:
```
let x: u32 = 42;
let y = &x as *const u32;
```
2015-03-19 23:15:27 -05:00
|
|
|
use lint::LintId;
|
2015-02-25 05:44:44 -06:00
|
|
|
|
|
|
|
mod builtin;
|
|
|
|
|
2015-02-25 06:03:44 -06:00
|
|
|
/// Tell the `LintStore` about all the built-in lints (the ones
|
|
|
|
/// defined in this crate and the ones defined in
|
|
|
|
/// `rustc::lint::builtin`).
|
2015-02-25 05:44:44 -06:00
|
|
|
pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
|
|
|
|
macro_rules! add_builtin {
|
|
|
|
($sess:ident, $($name:ident),*,) => (
|
|
|
|
{$(
|
Add trivial cast lints.
This permits all coercions to be performed in casts, but adds lints to warn in those cases.
Part of this patch moves cast checking to a later stage of type checking. We acquire obligations to check casts as part of type checking where we previously checked them. Once we have type checked a function or module, then we check any cast obligations which have been acquired. That means we have more type information available to check casts (this was crucial to making coercions work properly in place of some casts), but it means that casts cannot feed input into type inference.
[breaking change]
* Adds two new lints for trivial casts and trivial numeric casts, these are warn by default, but can cause errors if you build with warnings as errors. Previously, trivial numeric casts and casts to trait objects were allowed.
* The unused casts lint has gone.
* Interactions between casting and type inference have changed in subtle ways. Two ways this might manifest are:
- You may need to 'direct' casts more with extra type information, for example, in some cases where `foo as _ as T` succeeded, you may now need to specify the type for `_`
- Casts do not influence inference of integer types. E.g., the following used to type check:
```
let x = 42;
let y = &x as *const u32;
```
Because the cast would inform inference that `x` must have type `u32`. This no longer applies and the compiler will fallback to `i32` for `x` and thus there will be a type error in the cast. The solution is to add more type information:
```
let x: u32 = 42;
let y = &x as *const u32;
```
2015-03-19 23:15:27 -05:00
|
|
|
store.register_pass($sess, false, box builtin::$name);
|
2015-02-25 05:44:44 -06:00
|
|
|
)*}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! add_builtin_with_new {
|
|
|
|
($sess:ident, $($name:ident),*,) => (
|
|
|
|
{$(
|
Add trivial cast lints.
This permits all coercions to be performed in casts, but adds lints to warn in those cases.
Part of this patch moves cast checking to a later stage of type checking. We acquire obligations to check casts as part of type checking where we previously checked them. Once we have type checked a function or module, then we check any cast obligations which have been acquired. That means we have more type information available to check casts (this was crucial to making coercions work properly in place of some casts), but it means that casts cannot feed input into type inference.
[breaking change]
* Adds two new lints for trivial casts and trivial numeric casts, these are warn by default, but can cause errors if you build with warnings as errors. Previously, trivial numeric casts and casts to trait objects were allowed.
* The unused casts lint has gone.
* Interactions between casting and type inference have changed in subtle ways. Two ways this might manifest are:
- You may need to 'direct' casts more with extra type information, for example, in some cases where `foo as _ as T` succeeded, you may now need to specify the type for `_`
- Casts do not influence inference of integer types. E.g., the following used to type check:
```
let x = 42;
let y = &x as *const u32;
```
Because the cast would inform inference that `x` must have type `u32`. This no longer applies and the compiler will fallback to `i32` for `x` and thus there will be a type error in the cast. The solution is to add more type information:
```
let x: u32 = 42;
let y = &x as *const u32;
```
2015-03-19 23:15:27 -05:00
|
|
|
store.register_pass($sess, false, box builtin::$name::new());
|
2015-02-25 05:44:44 -06:00
|
|
|
)*}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! add_lint_group {
|
|
|
|
($sess:ident, $name:expr, $($lint:ident),*) => (
|
|
|
|
store.register_group($sess, false, $name, vec![$(LintId::of(builtin::$lint)),*]);
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
add_builtin!(sess,
|
|
|
|
HardwiredLints,
|
|
|
|
WhileTrue,
|
|
|
|
ImproperCTypes,
|
|
|
|
BoxPointers,
|
|
|
|
UnusedAttributes,
|
|
|
|
PathStatements,
|
|
|
|
UnusedResults,
|
|
|
|
NonCamelCaseTypes,
|
|
|
|
NonSnakeCase,
|
|
|
|
NonUpperCaseGlobals,
|
|
|
|
UnusedParens,
|
|
|
|
UnusedImportBraces,
|
|
|
|
NonShorthandFieldPatterns,
|
|
|
|
UnusedUnsafe,
|
|
|
|
UnsafeCode,
|
|
|
|
UnusedMut,
|
|
|
|
UnusedAllocation,
|
|
|
|
MissingCopyImplementations,
|
|
|
|
UnstableFeatures,
|
|
|
|
Stability,
|
|
|
|
UnconditionalRecursion,
|
|
|
|
InvalidNoMangleItems,
|
|
|
|
PluginAsLibrary,
|
2015-04-29 04:37:19 -05:00
|
|
|
DropWithReprExtern,
|
2015-04-13 16:49:10 -05:00
|
|
|
MutableTransmutes,
|
2015-02-25 05:44:44 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
add_builtin_with_new!(sess,
|
|
|
|
TypeLimits,
|
|
|
|
RawPointerDerive,
|
|
|
|
MissingDoc,
|
|
|
|
MissingDebugImplementations,
|
|
|
|
);
|
|
|
|
|
|
|
|
add_lint_group!(sess, "bad_style",
|
|
|
|
NON_CAMEL_CASE_TYPES, NON_SNAKE_CASE, NON_UPPER_CASE_GLOBALS);
|
|
|
|
|
|
|
|
add_lint_group!(sess, "unused",
|
|
|
|
UNUSED_IMPORTS, UNUSED_VARIABLES, UNUSED_ASSIGNMENTS, DEAD_CODE,
|
|
|
|
UNUSED_MUT, UNREACHABLE_CODE, UNUSED_MUST_USE,
|
|
|
|
UNUSED_UNSAFE, PATH_STATEMENTS);
|
|
|
|
|
|
|
|
// We have one lint pass defined specially
|
Add trivial cast lints.
This permits all coercions to be performed in casts, but adds lints to warn in those cases.
Part of this patch moves cast checking to a later stage of type checking. We acquire obligations to check casts as part of type checking where we previously checked them. Once we have type checked a function or module, then we check any cast obligations which have been acquired. That means we have more type information available to check casts (this was crucial to making coercions work properly in place of some casts), but it means that casts cannot feed input into type inference.
[breaking change]
* Adds two new lints for trivial casts and trivial numeric casts, these are warn by default, but can cause errors if you build with warnings as errors. Previously, trivial numeric casts and casts to trait objects were allowed.
* The unused casts lint has gone.
* Interactions between casting and type inference have changed in subtle ways. Two ways this might manifest are:
- You may need to 'direct' casts more with extra type information, for example, in some cases where `foo as _ as T` succeeded, you may now need to specify the type for `_`
- Casts do not influence inference of integer types. E.g., the following used to type check:
```
let x = 42;
let y = &x as *const u32;
```
Because the cast would inform inference that `x` must have type `u32`. This no longer applies and the compiler will fallback to `i32` for `x` and thus there will be a type error in the cast. The solution is to add more type information:
```
let x: u32 = 42;
let y = &x as *const u32;
```
2015-03-19 23:15:27 -05:00
|
|
|
store.register_pass(sess, false, box lint::GatherNodeLevels);
|
2015-02-25 05:44:44 -06:00
|
|
|
|
|
|
|
// Insert temporary renamings for a one-time deprecation
|
|
|
|
store.register_renamed("raw_pointer_deriving", "raw_pointer_derive");
|
|
|
|
|
|
|
|
store.register_renamed("unknown_features", "unused_features");
|
|
|
|
}
|