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
|
2015-11-22 14:14:09 -06:00
|
|
|
//! 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.
|
|
|
|
|
|
|
|
#![crate_name = "rustc_lint"]
|
2015-08-13 12:21:36 -05:00
|
|
|
#![unstable(feature = "rustc_private", issue = "27812")]
|
2015-02-25 05:44:44 -06:00
|
|
|
#![crate_type = "dylib"]
|
|
|
|
#![crate_type = "rlib"]
|
2015-08-09 16:15:05 -05:00
|
|
|
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
|
2015-05-15 18:04:01 -05:00
|
|
|
html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
|
2015-08-09 16:15:05 -05:00
|
|
|
html_root_url = "https://doc.rust-lang.org/nightly/")]
|
2016-01-21 17:26:19 -06:00
|
|
|
#![cfg_attr(not(stage0), deny(warnings))]
|
2015-02-25 05:44:44 -06:00
|
|
|
|
2015-06-09 16:39:23 -05:00
|
|
|
#![cfg_attr(test, feature(test))]
|
2015-02-25 05:44:44 -06:00
|
|
|
#![feature(box_patterns)]
|
|
|
|
#![feature(box_syntax)]
|
|
|
|
#![feature(quote)]
|
|
|
|
#![feature(rustc_diagnostic_macros)]
|
|
|
|
#![feature(rustc_private)]
|
2015-06-25 16:48:36 -05:00
|
|
|
#![feature(slice_patterns)]
|
2015-02-25 05:44:44 -06:00
|
|
|
#![feature(staged_api)]
|
|
|
|
|
2016-01-11 05:31:46 -06:00
|
|
|
#[macro_use]
|
2015-02-25 05:44:44 -06:00
|
|
|
extern crate syntax;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate rustc;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate log;
|
std: Stabilize library APIs for 1.5
This commit stabilizes and deprecates library APIs whose FCP has closed in the
last cycle, specifically:
Stabilized APIs:
* `fs::canonicalize`
* `Path::{metadata, symlink_metadata, canonicalize, read_link, read_dir, exists,
is_file, is_dir}` - all moved to inherent methods from the `PathExt` trait.
* `Formatter::fill`
* `Formatter::width`
* `Formatter::precision`
* `Formatter::sign_plus`
* `Formatter::sign_minus`
* `Formatter::alternate`
* `Formatter::sign_aware_zero_pad`
* `string::ParseError`
* `Utf8Error::valid_up_to`
* `Iterator::{cmp, partial_cmp, eq, ne, lt, le, gt, ge}`
* `<[T]>::split_{first,last}{,_mut}`
* `Condvar::wait_timeout` - note that `wait_timeout_ms` is not yet deprecated
but will be once 1.5 is released.
* `str::{R,}MatchIndices`
* `str::{r,}match_indices`
* `char::from_u32_unchecked`
* `VecDeque::insert`
* `VecDeque::shrink_to_fit`
* `VecDeque::as_slices`
* `VecDeque::as_mut_slices`
* `VecDeque::swap_remove_front` - (renamed from `swap_front_remove`)
* `VecDeque::swap_remove_back` - (renamed from `swap_back_remove`)
* `Vec::resize`
* `str::slice_mut_unchecked`
* `FileTypeExt`
* `FileTypeExt::{is_block_device, is_char_device, is_fifo, is_socket}`
* `BinaryHeap::from` - `from_vec` deprecated in favor of this
* `BinaryHeap::into_vec` - plus a `Into` impl
* `BinaryHeap::into_sorted_vec`
Deprecated APIs
* `slice::ref_slice`
* `slice::mut_ref_slice`
* `iter::{range_inclusive, RangeInclusive}`
* `std::dynamic_lib`
Closes #27706
Closes #27725
cc #27726 (align not stabilized yet)
Closes #27734
Closes #27737
Closes #27742
Closes #27743
Closes #27772
Closes #27774
Closes #27777
Closes #27781
cc #27788 (a few remaining methods though)
Closes #27790
Closes #27793
Closes #27796
Closes #27810
cc #28147 (not all parts stabilized)
2015-10-22 18:28:45 -05:00
|
|
|
extern crate rustc_back;
|
2016-03-30 06:43:36 -05:00
|
|
|
extern crate rustc_const_eval;
|
2015-02-25 05:44:44 -06:00
|
|
|
|
|
|
|
pub use rustc::lint as lint;
|
|
|
|
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;
|
2016-01-08 16:53:44 -06:00
|
|
|
use lint::FutureIncompatibleInfo;
|
2015-02-25 05:44:44 -06:00
|
|
|
|
2015-09-14 21:36:39 -05:00
|
|
|
mod bad_style;
|
2015-02-25 05:44:44 -06:00
|
|
|
mod builtin;
|
2015-09-21 19:58:57 -05:00
|
|
|
mod types;
|
2015-09-15 17:58:19 -05:00
|
|
|
mod unused;
|
2015-02-25 05:44:44 -06:00
|
|
|
|
2015-09-14 21:36:39 -05:00
|
|
|
use bad_style::*;
|
|
|
|
use builtin::*;
|
2015-09-21 19:58:57 -05:00
|
|
|
use types::*;
|
2015-09-15 17:58:19 -05:00
|
|
|
use unused::*;
|
2015-09-14 21:36:39 -05:00
|
|
|
|
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),*,) => (
|
|
|
|
{$(
|
2015-09-14 21:36:39 -05:00
|
|
|
store.register_late_pass($sess, false, box $name);
|
2015-09-14 18:35:25 -05:00
|
|
|
)*}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! add_early_builtin {
|
|
|
|
($sess:ident, $($name:ident),*,) => (
|
|
|
|
{$(
|
2015-09-14 21:36:39 -05:00
|
|
|
store.register_early_pass($sess, false, box $name);
|
2015-02-25 05:44:44 -06:00
|
|
|
)*}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! add_builtin_with_new {
|
|
|
|
($sess:ident, $($name:ident),*,) => (
|
|
|
|
{$(
|
2015-09-14 21:36:39 -05:00
|
|
|
store.register_late_pass($sess, false, box $name::new());
|
2015-02-25 05:44:44 -06:00
|
|
|
)*}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! add_lint_group {
|
|
|
|
($sess:ident, $name:expr, $($lint:ident),*) => (
|
2015-09-14 21:36:39 -05:00
|
|
|
store.register_group($sess, false, $name, vec![$(LintId::of($lint)),*]);
|
2015-02-25 05:44:44 -06:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2015-09-14 18:35:25 -05:00
|
|
|
add_early_builtin!(sess,
|
|
|
|
UnusedParens,
|
|
|
|
);
|
|
|
|
|
2015-02-25 05:44:44 -06:00
|
|
|
add_builtin!(sess,
|
|
|
|
HardwiredLints,
|
|
|
|
WhileTrue,
|
|
|
|
ImproperCTypes,
|
|
|
|
BoxPointers,
|
|
|
|
UnusedAttributes,
|
|
|
|
PathStatements,
|
|
|
|
UnusedResults,
|
|
|
|
NonCamelCaseTypes,
|
|
|
|
NonSnakeCase,
|
|
|
|
NonUpperCaseGlobals,
|
|
|
|
UnusedImportBraces,
|
|
|
|
NonShorthandFieldPatterns,
|
|
|
|
UnusedUnsafe,
|
|
|
|
UnsafeCode,
|
|
|
|
UnusedMut,
|
|
|
|
UnusedAllocation,
|
|
|
|
MissingCopyImplementations,
|
|
|
|
UnstableFeatures,
|
2015-12-04 10:34:28 -06:00
|
|
|
Deprecated,
|
2015-02-25 05:44:44 -06:00
|
|
|
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,
|
|
|
|
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,
|
2015-09-29 10:44:26 -05:00
|
|
|
UNUSED_UNSAFE, PATH_STATEMENTS, UNUSED_ATTRIBUTES);
|
2015-02-25 05:44:44 -06:00
|
|
|
|
2016-01-08 16:53:44 -06:00
|
|
|
// Guidelines for creating a future incompatibility lint:
|
|
|
|
//
|
|
|
|
// - Create a lint defaulting to warn as normal, with ideally the same error
|
|
|
|
// message you would normally give
|
|
|
|
// - Add a suitable reference, typically an RFC or tracking issue. Go ahead
|
|
|
|
// and include the full URL.
|
|
|
|
// - Later, change lint to error
|
|
|
|
// - Eventually, remove lint
|
|
|
|
store.register_future_incompatible(sess, vec![
|
|
|
|
FutureIncompatibleInfo {
|
|
|
|
id: LintId::of(PRIVATE_IN_PUBLIC),
|
|
|
|
reference: "the explanation for E0446 (`--explain E0446`)",
|
|
|
|
},
|
2016-02-12 00:42:44 -06:00
|
|
|
FutureIncompatibleInfo {
|
|
|
|
id: LintId::of(INACCESSIBLE_EXTERN_CRATE),
|
|
|
|
reference: "PR 31362 <https://github.com/rust-lang/rust/pull/31362>",
|
|
|
|
},
|
2016-01-08 16:53:44 -06:00
|
|
|
FutureIncompatibleInfo {
|
|
|
|
id: LintId::of(INVALID_TYPE_PARAM_DEFAULT),
|
|
|
|
reference: "PR 30742 <https://github.com/rust-lang/rust/pull/30724>",
|
|
|
|
},
|
2016-03-19 19:04:12 -05:00
|
|
|
FutureIncompatibleInfo {
|
|
|
|
id: LintId::of(SUPER_OR_SELF_IN_GLOBAL_PATH),
|
|
|
|
reference: "PR #32403 <https://github.com/rust-lang/rust/pull/32403>",
|
|
|
|
},
|
2016-01-08 16:53:44 -06:00
|
|
|
FutureIncompatibleInfo {
|
|
|
|
id: LintId::of(MATCH_OF_UNIT_VARIANT_VIA_PAREN_DOTDOT),
|
2016-01-12 16:12:29 -06:00
|
|
|
reference: "RFC 218 <https://github.com/rust-lang/rfcs/blob/\
|
|
|
|
master/text/0218-empty-struct-with-braces.md>",
|
2016-01-08 16:53:44 -06:00
|
|
|
},
|
2016-03-09 10:35:27 -06:00
|
|
|
FutureIncompatibleInfo {
|
|
|
|
id: LintId::of(TRANSMUTE_FROM_FN_ITEM_TYPES),
|
|
|
|
reference: "issue #19925 <https://github.com/rust-lang/rust/issues/19925>",
|
|
|
|
},
|
2016-03-17 11:05:24 -05:00
|
|
|
FutureIncompatibleInfo {
|
|
|
|
id: LintId::of(OVERLAPPING_INHERENT_IMPLS),
|
|
|
|
reference: "issue #22889 <https://github.com/rust-lang/rust/issues/22889>",
|
|
|
|
},
|
2016-03-11 12:30:32 -06:00
|
|
|
FutureIncompatibleInfo {
|
|
|
|
id: LintId::of(ILLEGAL_FLOATING_POINT_CONSTANT_PATTERN),
|
|
|
|
reference: "RFC 1445 <https://github.com/rust-lang/rfcs/pull/1445>",
|
|
|
|
},
|
|
|
|
FutureIncompatibleInfo {
|
|
|
|
id: LintId::of(ILLEGAL_STRUCT_OR_ENUM_CONSTANT_PATTERN),
|
|
|
|
reference: "RFC 1445 <https://github.com/rust-lang/rfcs/pull/1445>",
|
|
|
|
},
|
2016-05-01 01:59:28 -05:00
|
|
|
FutureIncompatibleInfo {
|
|
|
|
id: LintId::of(UNSIZED_IN_TUPLE),
|
|
|
|
reference: "RFC PR 1592 <https://github.com/rust-lang/rfcs/pull/1592>",
|
|
|
|
}
|
2016-01-08 16:53:44 -06:00
|
|
|
]);
|
2015-11-26 11:56:20 -06:00
|
|
|
|
2015-02-25 05:44:44 -06:00
|
|
|
// We have one lint pass defined specially
|
2015-09-14 18:35:25 -05:00
|
|
|
store.register_late_pass(sess, false, box lint::GatherNodeLevels);
|
2015-02-25 05:44:44 -06:00
|
|
|
|
2016-01-13 12:54:06 -06:00
|
|
|
// Register renamed and removed lints
|
2015-02-25 05:44:44 -06:00
|
|
|
store.register_renamed("unknown_features", "unused_features");
|
2015-07-15 12:12:30 -05:00
|
|
|
store.register_removed("unsigned_negation", "replaced by negate_unsigned feature gate");
|
2016-01-11 05:31:46 -06:00
|
|
|
store.register_removed("negate_unsigned", "cast a signed value instead");
|
2016-01-13 12:54:06 -06:00
|
|
|
store.register_removed("raw_pointer_derive", "using derive with raw pointers is ok");
|
|
|
|
// This was renamed to raw_pointer_derive, which was then removed,
|
|
|
|
// so it is also considered removed
|
|
|
|
store.register_removed("raw_pointer_deriving", "using derive with raw pointers is ok");
|
2015-02-25 05:44:44 -06:00
|
|
|
}
|