rust/src/librustc/lint/builtin.rs

259 lines
6.0 KiB
Rust
Raw Normal View History

// Copyright 2012-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.
//! Some lints that are built in to the compiler.
//!
//! These are the built-in lints that are emitted direct in the main
//! compiler code, rather than using their own custom pass. Those
//! lints are all available in `rustc_lint::builtin`.
use lint::{LintPass, LateLintPass, LintArray};
declare_lint! {
pub CONST_ERR,
Warn,
"constant evaluation detected erroneous expression"
}
declare_lint! {
pub UNUSED_IMPORTS,
Warn,
"imports that are never used"
}
declare_lint! {
pub UNUSED_EXTERN_CRATES,
Allow,
"extern crates that are never used"
}
2014-09-11 12:14:43 -05:00
declare_lint! {
pub UNUSED_QUALIFICATIONS,
Allow,
"detects unnecessarily qualified names"
}
declare_lint! {
pub UNKNOWN_LINTS,
Warn,
"unrecognized lint attribute"
}
declare_lint! {
pub UNUSED_VARIABLES,
Warn,
"detect variables which are not used in any way"
}
declare_lint! {
pub UNUSED_ASSIGNMENTS,
Warn,
"detect assignments that will never be read"
}
declare_lint! {
pub DEAD_CODE,
Warn,
"detect unused, unexported items"
}
declare_lint! {
pub UNREACHABLE_CODE,
Warn,
"detects unreachable code paths"
}
declare_lint! {
pub WARNINGS,
Warn,
"mass-change the level for lints which produce warnings"
}
declare_lint! {
2015-01-16 12:25:16 -06:00
pub UNUSED_FEATURES,
Warn,
2015-01-16 12:25:16 -06:00
"unused or unknown features found in crate-level #[feature] directives"
}
declare_lint! {
pub STABLE_FEATURES,
Warn,
"stable features found in #[feature] directive"
}
declare_lint! {
pub UNKNOWN_CRATE_TYPES,
Deny,
"unknown crate type found in #[crate_type] directive"
}
declare_lint! {
pub FAT_PTR_TRANSMUTES,
Allow,
"detects transmutes of fat pointers"
}
2014-08-06 04:59:40 -05:00
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
declare_lint! {
2015-03-23 17:23:34 -05:00
pub TRIVIAL_CASTS,
Allow,
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
"detects trivial casts which could be removed"
}
declare_lint! {
2015-03-23 17:23:34 -05:00
pub TRIVIAL_NUMERIC_CASTS,
Allow,
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
"detects trivial casts of numeric types which could be removed"
}
2015-11-26 11:56:20 -06:00
declare_lint! {
pub PRIVATE_IN_PUBLIC,
Warn,
2015-11-26 11:56:20 -06:00
"detect private items in public interfaces not caught by the old implementation"
}
declare_lint! {
pub INACCESSIBLE_EXTERN_CRATE,
Deny,
"use of inaccessible extern crate erroneously allowed"
}
declare_lint! {
pub INVALID_TYPE_PARAM_DEFAULT,
Deny,
"type parameter default erroneously allowed in invalid location"
}
declare_lint! {
pub ILLEGAL_FLOATING_POINT_CONSTANT_PATTERN,
Deny,
"floating-point constants cannot be used in patterns"
}
declare_lint! {
pub ILLEGAL_STRUCT_OR_ENUM_CONSTANT_PATTERN,
Deny,
"constants of struct or enum type can only be used in a pattern if \
2016-03-25 09:02:56 -05:00
the struct or enum has `#[derive(PartialEq, Eq)]`"
}
declare_lint! {
pub RAW_POINTER_DERIVE,
Warn,
"uses of #[derive] with raw pointers are rarely correct"
}
declare_lint! {
pub TRANSMUTE_FROM_FN_ITEM_TYPES,
Deny,
"transmute from function item type to pointer-sized type erroneously allowed"
}
declare_lint! {
pub HR_LIFETIME_IN_ASSOC_TYPE,
Deny,
"binding for associated type references higher-ranked lifetime \
that does not appear in the trait input types"
}
declare_lint! {
pub OVERLAPPING_INHERENT_IMPLS,
Deny,
"two overlapping inherent impls define an item with the same name were erroneously allowed"
}
declare_lint! {
pub RENAMED_AND_REMOVED_LINTS,
Warn,
"lints that have been renamed or removed"
}
declare_lint! {
pub SUPER_OR_SELF_IN_GLOBAL_PATH,
Deny,
"detects super or self keywords at the beginning of global path"
}
declare_lint! {
pub LIFETIME_UNDERSCORE,
Deny,
"lifetimes or labels named `'_` were erroneously allowed"
}
declare_lint! {
pub SAFE_EXTERN_STATICS,
Warn,
"safe access to extern statics was erroneously allowed"
}
declare_lint! {
pub PATTERNS_IN_FNS_WITHOUT_BODY,
Warn,
"patterns in functions without body were erroneously allowed"
}
2016-10-12 15:38:58 -05:00
declare_lint! {
pub EXTRA_REQUIREMENT_IN_IMPL,
Warn,
"detects extra requirements in impls that were erroneously allowed"
}
2016-11-14 03:31:03 -06:00
declare_lint! {
pub LEGACY_DIRECTORY_OWNERSHIP,
Warn,
"non-inline, non-`#[path]` modules (e.g. `mod foo;`) were erroneously allowed in some files \
not named `mod.rs`"
}
/// Does nothing as a lint pass, but registers some `Lint`s
/// which are used by other parts of the compiler.
2015-03-30 08:38:44 -05:00
#[derive(Copy, Clone)]
pub struct HardwiredLints;
impl LintPass for HardwiredLints {
fn get_lints(&self) -> LintArray {
lint_array!(
UNUSED_IMPORTS,
UNUSED_EXTERN_CRATES,
UNUSED_QUALIFICATIONS,
UNKNOWN_LINTS,
UNUSED_VARIABLES,
UNUSED_ASSIGNMENTS,
DEAD_CODE,
UNREACHABLE_CODE,
WARNINGS,
2015-01-16 12:25:16 -06:00
UNUSED_FEATURES,
STABLE_FEATURES,
UNKNOWN_CRATE_TYPES,
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
FAT_PTR_TRANSMUTES,
2015-03-23 17:23:34 -05:00
TRIVIAL_CASTS,
TRIVIAL_NUMERIC_CASTS,
2015-11-26 11:56:20 -06:00
PRIVATE_IN_PUBLIC,
INACCESSIBLE_EXTERN_CRATE,
INVALID_TYPE_PARAM_DEFAULT,
ILLEGAL_FLOATING_POINT_CONSTANT_PATTERN,
ILLEGAL_STRUCT_OR_ENUM_CONSTANT_PATTERN,
CONST_ERR,
RAW_POINTER_DERIVE,
TRANSMUTE_FROM_FN_ITEM_TYPES,
OVERLAPPING_INHERENT_IMPLS,
RENAMED_AND_REMOVED_LINTS,
SUPER_OR_SELF_IN_GLOBAL_PATH,
HR_LIFETIME_IN_ASSOC_TYPE,
LIFETIME_UNDERSCORE,
SAFE_EXTERN_STATICS,
2016-10-12 15:38:58 -05:00
PATTERNS_IN_FNS_WITHOUT_BODY,
2016-11-14 03:31:03 -06:00
EXTRA_REQUIREMENT_IN_IMPL,
LEGACY_DIRECTORY_OWNERSHIP
)
}
}
impl LateLintPass for HardwiredLints {}