2014-06-06 15:49:48 -07:00
|
|
|
//! Lints, aka compiler warnings.
|
|
|
|
//!
|
2013-10-02 11:29:29 -07:00
|
|
|
//! A 'lint' check is a kind of miscellaneous constraint that a user _might_
|
|
|
|
//! want to enforce, but might reasonably want to permit as well, on a
|
|
|
|
//! module-by-module basis. They contrast with static constraints enforced by
|
|
|
|
//! other phases of the compiler, which are generally required to hold in order
|
|
|
|
//! to compile the program at all.
|
|
|
|
//!
|
2018-05-08 16:10:16 +03:00
|
|
|
//! Most lints can be written as `LintPass` instances. These run after
|
|
|
|
//! all other analyses. The `LintPass`es built into rustc are defined
|
2014-06-06 15:49:48 -07:00
|
|
|
//! within `builtin.rs`, which has further comments on how to add such a lint.
|
2014-06-18 17:26:14 -07:00
|
|
|
//! rustc can also load user-defined lint plugins via the plugin mechanism.
|
2014-06-02 15:27:15 -07:00
|
|
|
//!
|
2014-06-06 15:49:48 -07:00
|
|
|
//! Some of rustc's lints are defined elsewhere in the compiler and work by
|
|
|
|
//! calling `add_lint()` on the overall `Session` object. This works when
|
|
|
|
//! it happens before the main lint pass, which emits the lints stored by
|
2018-05-08 16:10:16 +03:00
|
|
|
//! `add_lint()`. To emit lints after the main lint pass (from codegen, for
|
2014-06-06 15:49:48 -07:00
|
|
|
//! example) requires more effort. See `emit_lint` and `GatherNodeLevels`
|
|
|
|
//! in `context.rs`.
|
2013-05-17 15:28:44 -07:00
|
|
|
|
2014-11-06 00:05:53 -08:00
|
|
|
pub use self::Level::*;
|
|
|
|
pub use self::LintSource::*;
|
|
|
|
|
2018-11-30 21:01:50 +01:00
|
|
|
use rustc_data_structures::sync;
|
2017-07-26 21:51:09 -07:00
|
|
|
|
2019-02-05 11:20:45 -06:00
|
|
|
use crate::hir::def_id::{CrateNum, LOCAL_CRATE};
|
|
|
|
use crate::hir::intravisit;
|
|
|
|
use crate::hir;
|
2019-03-28 12:36:13 -05:00
|
|
|
use crate::lint::builtin::BuiltinLintDiagnostics;
|
2019-02-05 11:20:45 -06:00
|
|
|
use crate::session::{Session, DiagnosticMessageId};
|
2019-02-09 11:24:02 +09:00
|
|
|
use crate::ty::TyCtxt;
|
|
|
|
use crate::ty::query::Providers;
|
|
|
|
use crate::util::nodemap::NodeMap;
|
|
|
|
use errors::{DiagnosticBuilder, DiagnosticId};
|
2014-06-06 15:49:48 -07:00
|
|
|
use syntax::ast;
|
2019-06-19 01:08:45 +03:00
|
|
|
use syntax::source_map::{MultiSpan, ExpnKind, DesugaringKind};
|
2019-11-12 08:51:57 -05:00
|
|
|
use syntax::symbol::Symbol;
|
2019-10-09 16:47:38 +02:00
|
|
|
use syntax_pos::hygiene::MacroKind;
|
2017-07-26 21:51:09 -07:00
|
|
|
use syntax_pos::Span;
|
2014-06-06 15:49:48 -07:00
|
|
|
|
2019-02-05 11:20:45 -06:00
|
|
|
pub use crate::lint::context::{LateContext, EarlyContext, LintContext, LintStore,
|
2019-01-31 01:36:11 +01:00
|
|
|
check_crate, check_ast_crate, late_lint_mod, CheckLintNameResult,
|
2019-10-08 21:49:21 -04:00
|
|
|
BufferedEarlyLint,};
|
2017-01-28 07:01:45 -05:00
|
|
|
|
2019-11-12 12:09:20 -05:00
|
|
|
pub use rustc_session::lint::{Lint, LintId, Level, FutureIncompatibleInfo};
|
2018-07-30 11:20:11 +02:00
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Declares a static `LintArray` and return it as an expression.
|
2014-06-04 14:35:58 -07:00
|
|
|
#[macro_export]
|
2018-01-15 10:29:30 -06:00
|
|
|
macro_rules! lint_array {
|
2018-06-15 21:49:00 -05:00
|
|
|
($( $lint:expr ),* ,) => { lint_array!( $($lint),* ) };
|
|
|
|
($( $lint:expr ),*) => {{
|
2018-06-21 09:04:50 +02:00
|
|
|
vec![$($lint),*]
|
2018-01-15 10:29:30 -06:00
|
|
|
}}
|
|
|
|
}
|
2014-06-01 16:16:00 -07:00
|
|
|
|
2018-06-21 09:04:50 +02:00
|
|
|
pub type LintArray = Vec<&'static Lint>;
|
2014-06-04 14:35:58 -07:00
|
|
|
|
2014-06-10 14:03:19 -07:00
|
|
|
pub trait LintPass {
|
2019-01-18 07:40:55 +01:00
|
|
|
fn name(&self) -> &'static str;
|
2015-09-15 11:35:25 +12:00
|
|
|
}
|
2014-06-04 14:35:58 -07:00
|
|
|
|
2019-04-03 16:05:40 +02:00
|
|
|
/// Implements `LintPass for $name` with the given list of `Lint` statics.
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! impl_lint_pass {
|
|
|
|
($name:ident => [$($lint:expr),* $(,)?]) => {
|
|
|
|
impl LintPass for $name {
|
|
|
|
fn name(&self) -> &'static str { stringify!($name) }
|
2019-10-07 18:04:05 -04:00
|
|
|
}
|
|
|
|
impl $name {
|
|
|
|
pub fn get_lints() -> LintArray { $crate::lint_array!($($lint),*) }
|
2019-04-03 16:05:40 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Declares a type named `$name` which implements `LintPass`.
|
|
|
|
/// To the right of `=>` a comma separated list of `Lint` statics is given.
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! declare_lint_pass {
|
|
|
|
($(#[$m:meta])* $name:ident => [$($lint:expr),* $(,)?]) => {
|
|
|
|
$(#[$m])* #[derive(Copy, Clone)] pub struct $name;
|
|
|
|
$crate::impl_lint_pass!($name => [$($lint),*]);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-06-21 09:04:50 +02:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! late_lint_methods {
|
|
|
|
($macro:path, $args:tt, [$hir:tt]) => (
|
|
|
|
$macro!($args, [$hir], [
|
2019-08-27 13:24:32 +02:00
|
|
|
fn check_param(a: &$hir hir::Param);
|
2018-06-21 09:04:50 +02:00
|
|
|
fn check_body(a: &$hir hir::Body);
|
|
|
|
fn check_body_post(a: &$hir hir::Body);
|
|
|
|
fn check_name(a: Span, b: ast::Name);
|
2019-11-28 11:49:29 +01:00
|
|
|
fn check_crate(a: &$hir hir::Crate<$hir>);
|
|
|
|
fn check_crate_post(a: &$hir hir::Crate<$hir>);
|
2019-02-06 14:16:11 +01:00
|
|
|
fn check_mod(a: &$hir hir::Mod, b: Span, c: hir::HirId);
|
|
|
|
fn check_mod_post(a: &$hir hir::Mod, b: Span, c: hir::HirId);
|
2019-11-28 20:18:29 +01:00
|
|
|
fn check_foreign_item(a: &$hir hir::ForeignItem<$hir>);
|
|
|
|
fn check_foreign_item_post(a: &$hir hir::ForeignItem<$hir>);
|
2019-11-28 19:28:50 +01:00
|
|
|
fn check_item(a: &$hir hir::Item<$hir>);
|
|
|
|
fn check_item_post(a: &$hir hir::Item<$hir>);
|
2018-06-21 09:04:50 +02:00
|
|
|
fn check_local(a: &$hir hir::Local);
|
|
|
|
fn check_block(a: &$hir hir::Block);
|
|
|
|
fn check_block_post(a: &$hir hir::Block);
|
|
|
|
fn check_stmt(a: &$hir hir::Stmt);
|
|
|
|
fn check_arm(a: &$hir hir::Arm);
|
|
|
|
fn check_pat(a: &$hir hir::Pat);
|
|
|
|
fn check_expr(a: &$hir hir::Expr);
|
|
|
|
fn check_expr_post(a: &$hir hir::Expr);
|
|
|
|
fn check_ty(a: &$hir hir::Ty);
|
|
|
|
fn check_generic_param(a: &$hir hir::GenericParam);
|
|
|
|
fn check_generics(a: &$hir hir::Generics);
|
|
|
|
fn check_where_predicate(a: &$hir hir::WherePredicate);
|
|
|
|
fn check_poly_trait_ref(a: &$hir hir::PolyTraitRef, b: hir::TraitBoundModifier);
|
|
|
|
fn check_fn(
|
|
|
|
a: hir::intravisit::FnKind<$hir>,
|
|
|
|
b: &$hir hir::FnDecl,
|
|
|
|
c: &$hir hir::Body,
|
|
|
|
d: Span,
|
2019-02-06 14:16:11 +01:00
|
|
|
e: hir::HirId);
|
2018-06-21 09:04:50 +02:00
|
|
|
fn check_fn_post(
|
|
|
|
a: hir::intravisit::FnKind<$hir>,
|
|
|
|
b: &$hir hir::FnDecl,
|
|
|
|
c: &$hir hir::Body,
|
|
|
|
d: Span,
|
2019-02-06 14:16:11 +01:00
|
|
|
e: hir::HirId
|
2018-06-21 09:04:50 +02:00
|
|
|
);
|
2019-11-28 21:47:10 +01:00
|
|
|
fn check_trait_item(a: &$hir hir::TraitItem<$hir>);
|
|
|
|
fn check_trait_item_post(a: &$hir hir::TraitItem<$hir>);
|
2018-06-21 09:04:50 +02:00
|
|
|
fn check_impl_item(a: &$hir hir::ImplItem);
|
|
|
|
fn check_impl_item_post(a: &$hir hir::ImplItem);
|
2019-08-24 13:54:40 -03:00
|
|
|
fn check_struct_def(a: &$hir hir::VariantData);
|
|
|
|
fn check_struct_def_post(a: &$hir hir::VariantData);
|
2018-06-21 09:04:50 +02:00
|
|
|
fn check_struct_field(a: &$hir hir::StructField);
|
2019-08-24 13:54:40 -03:00
|
|
|
fn check_variant(a: &$hir hir::Variant);
|
|
|
|
fn check_variant_post(a: &$hir hir::Variant);
|
2018-06-21 09:04:50 +02:00
|
|
|
fn check_lifetime(a: &$hir hir::Lifetime);
|
2018-07-31 10:43:51 -06:00
|
|
|
fn check_path(a: &$hir hir::Path, b: hir::HirId);
|
2018-06-21 09:04:50 +02:00
|
|
|
fn check_attribute(a: &$hir ast::Attribute);
|
|
|
|
|
|
|
|
/// Called when entering a syntax node that can have lint attributes such
|
|
|
|
/// as `#[allow(...)]`. Called with *all* the attributes of that node.
|
|
|
|
fn enter_lint_attrs(a: &$hir [ast::Attribute]);
|
|
|
|
|
|
|
|
/// Counterpart to `enter_lint_attrs`.
|
|
|
|
fn exit_lint_attrs(a: &$hir [ast::Attribute]);
|
|
|
|
]);
|
|
|
|
)
|
|
|
|
}
|
2015-09-15 11:35:25 +12:00
|
|
|
|
|
|
|
/// Trait for types providing lint checks.
|
|
|
|
///
|
|
|
|
/// Each `check` method checks a single syntax node, and should not
|
|
|
|
/// invoke methods recursively (unlike `Visitor`). By default they
|
|
|
|
/// do nothing.
|
|
|
|
//
|
|
|
|
// FIXME: eliminate the duplication with `Visitor`. But this also
|
|
|
|
// contains a few lint-specific methods with no equivalent in `Visitor`.
|
2018-06-21 09:04:50 +02:00
|
|
|
|
|
|
|
macro_rules! expand_lint_pass_methods {
|
|
|
|
($context:ty, [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
|
2018-08-11 12:28:35 -05:00
|
|
|
$(#[inline(always)] fn $name(&mut self, _: $context, $(_: $arg),*) {})*
|
2018-06-21 09:04:50 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! declare_late_lint_pass {
|
|
|
|
([], [$hir:tt], [$($methods:tt)*]) => (
|
|
|
|
pub trait LateLintPass<'a, $hir>: LintPass {
|
|
|
|
expand_lint_pass_methods!(&LateContext<'a, $hir>, [$($methods)*]);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
late_lint_methods!(declare_late_lint_pass, [], ['tcx]);
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! expand_combined_late_lint_pass_method {
|
|
|
|
([$($passes:ident),*], $self: ident, $name: ident, $params:tt) => ({
|
|
|
|
$($self.$passes.$name $params;)*
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! expand_combined_late_lint_pass_methods {
|
|
|
|
($passes:tt, [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
|
|
|
|
$(fn $name(&mut self, context: &LateContext<'a, 'tcx>, $($param: $arg),*) {
|
|
|
|
expand_combined_late_lint_pass_method!($passes, self, $name, (context, $($param),*));
|
|
|
|
})*
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! declare_combined_late_lint_pass {
|
2019-01-31 01:36:11 +01:00
|
|
|
([$v:vis $name:ident, [$($passes:ident: $constructor:expr,)*]], [$hir:tt], $methods:tt) => (
|
2018-06-21 09:04:50 +02:00
|
|
|
#[allow(non_snake_case)]
|
2019-01-31 01:36:11 +01:00
|
|
|
$v struct $name {
|
2018-06-21 09:04:50 +02:00
|
|
|
$($passes: $passes,)*
|
|
|
|
}
|
|
|
|
|
|
|
|
impl $name {
|
2019-01-31 01:36:11 +01:00
|
|
|
$v fn new() -> Self {
|
2018-06-21 09:04:50 +02:00
|
|
|
Self {
|
|
|
|
$($passes: $constructor,)*
|
|
|
|
}
|
|
|
|
}
|
2019-10-07 18:04:05 -04:00
|
|
|
|
|
|
|
$v fn get_lints() -> LintArray {
|
|
|
|
let mut lints = Vec::new();
|
|
|
|
$(lints.extend_from_slice(&$passes::get_lints());)*
|
|
|
|
lints
|
|
|
|
}
|
2018-06-21 09:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for $name {
|
|
|
|
expand_combined_late_lint_pass_methods!([$($passes),*], $methods);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for $name {
|
2019-01-18 07:40:55 +01:00
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
panic!()
|
|
|
|
}
|
2018-06-21 09:04:50 +02:00
|
|
|
}
|
|
|
|
)
|
2015-09-15 11:35:25 +12:00
|
|
|
}
|
|
|
|
|
2019-01-18 07:40:55 +01:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! early_lint_methods {
|
|
|
|
($macro:path, $args:tt) => (
|
|
|
|
$macro!($args, [
|
2019-08-27 13:24:32 +02:00
|
|
|
fn check_param(a: &ast::Param);
|
2019-01-18 07:40:55 +01:00
|
|
|
fn check_ident(a: ast::Ident);
|
|
|
|
fn check_crate(a: &ast::Crate);
|
|
|
|
fn check_crate_post(a: &ast::Crate);
|
|
|
|
fn check_mod(a: &ast::Mod, b: Span, c: ast::NodeId);
|
|
|
|
fn check_mod_post(a: &ast::Mod, b: Span, c: ast::NodeId);
|
|
|
|
fn check_foreign_item(a: &ast::ForeignItem);
|
|
|
|
fn check_foreign_item_post(a: &ast::ForeignItem);
|
|
|
|
fn check_item(a: &ast::Item);
|
|
|
|
fn check_item_post(a: &ast::Item);
|
|
|
|
fn check_local(a: &ast::Local);
|
|
|
|
fn check_block(a: &ast::Block);
|
|
|
|
fn check_block_post(a: &ast::Block);
|
|
|
|
fn check_stmt(a: &ast::Stmt);
|
|
|
|
fn check_arm(a: &ast::Arm);
|
2019-04-21 17:09:30 +03:00
|
|
|
fn check_pat(a: &ast::Pat);
|
|
|
|
fn check_pat_post(a: &ast::Pat);
|
2019-01-18 07:40:55 +01:00
|
|
|
fn check_expr(a: &ast::Expr);
|
|
|
|
fn check_expr_post(a: &ast::Expr);
|
|
|
|
fn check_ty(a: &ast::Ty);
|
|
|
|
fn check_generic_param(a: &ast::GenericParam);
|
|
|
|
fn check_generics(a: &ast::Generics);
|
|
|
|
fn check_where_predicate(a: &ast::WherePredicate);
|
|
|
|
fn check_poly_trait_ref(a: &ast::PolyTraitRef,
|
|
|
|
b: &ast::TraitBoundModifier);
|
|
|
|
fn check_fn(a: syntax::visit::FnKind<'_>, b: &ast::FnDecl, c: Span, d_: ast::NodeId);
|
|
|
|
fn check_fn_post(
|
|
|
|
a: syntax::visit::FnKind<'_>,
|
|
|
|
b: &ast::FnDecl,
|
|
|
|
c: Span,
|
|
|
|
d: ast::NodeId
|
|
|
|
);
|
2019-12-08 00:08:09 +01:00
|
|
|
fn check_trait_item(a: &ast::AssocItem);
|
|
|
|
fn check_trait_item_post(a: &ast::AssocItem);
|
|
|
|
fn check_impl_item(a: &ast::AssocItem);
|
|
|
|
fn check_impl_item_post(a: &ast::AssocItem);
|
2019-08-24 13:54:40 -03:00
|
|
|
fn check_struct_def(a: &ast::VariantData);
|
|
|
|
fn check_struct_def_post(a: &ast::VariantData);
|
2019-01-18 07:40:55 +01:00
|
|
|
fn check_struct_field(a: &ast::StructField);
|
2019-08-24 13:54:40 -03:00
|
|
|
fn check_variant(a: &ast::Variant);
|
|
|
|
fn check_variant_post(a: &ast::Variant);
|
2019-01-18 07:40:55 +01:00
|
|
|
fn check_lifetime(a: &ast::Lifetime);
|
|
|
|
fn check_path(a: &ast::Path, b: ast::NodeId);
|
|
|
|
fn check_attribute(a: &ast::Attribute);
|
|
|
|
fn check_mac_def(a: &ast::MacroDef, b: ast::NodeId);
|
|
|
|
fn check_mac(a: &ast::Mac);
|
|
|
|
|
|
|
|
/// Called when entering a syntax node that can have lint attributes such
|
|
|
|
/// as `#[allow(...)]`. Called with *all* the attributes of that node.
|
|
|
|
fn enter_lint_attrs(a: &[ast::Attribute]);
|
|
|
|
|
|
|
|
/// Counterpart to `enter_lint_attrs`.
|
|
|
|
fn exit_lint_attrs(a: &[ast::Attribute]);
|
|
|
|
]);
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! expand_early_lint_pass_methods {
|
|
|
|
($context:ty, [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
|
|
|
|
$(#[inline(always)] fn $name(&mut self, _: $context, $(_: $arg),*) {})*
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! declare_early_lint_pass {
|
|
|
|
([], [$($methods:tt)*]) => (
|
|
|
|
pub trait EarlyLintPass: LintPass {
|
|
|
|
expand_early_lint_pass_methods!(&EarlyContext<'_>, [$($methods)*]);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
early_lint_methods!(declare_early_lint_pass, []);
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! expand_combined_early_lint_pass_method {
|
|
|
|
([$($passes:ident),*], $self: ident, $name: ident, $params:tt) => ({
|
|
|
|
$($self.$passes.$name $params;)*
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! expand_combined_early_lint_pass_methods {
|
|
|
|
($passes:tt, [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
|
|
|
|
$(fn $name(&mut self, context: &EarlyContext<'_>, $($param: $arg),*) {
|
|
|
|
expand_combined_early_lint_pass_method!($passes, self, $name, (context, $($param),*));
|
|
|
|
})*
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! declare_combined_early_lint_pass {
|
|
|
|
([$v:vis $name:ident, [$($passes:ident: $constructor:expr,)*]], $methods:tt) => (
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
$v struct $name {
|
|
|
|
$($passes: $passes,)*
|
|
|
|
}
|
|
|
|
|
|
|
|
impl $name {
|
|
|
|
$v fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
$($passes: $constructor,)*
|
|
|
|
}
|
|
|
|
}
|
2019-10-07 18:04:05 -04:00
|
|
|
|
|
|
|
$v fn get_lints() -> LintArray {
|
|
|
|
let mut lints = Vec::new();
|
|
|
|
$(lints.extend_from_slice(&$passes::get_lints());)*
|
|
|
|
lints
|
|
|
|
}
|
2019-01-18 07:40:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EarlyLintPass for $name {
|
|
|
|
expand_combined_early_lint_pass_methods!([$($passes),*], $methods);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for $name {
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
panic!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-06-02 15:27:15 -07:00
|
|
|
}
|
|
|
|
|
2014-06-06 15:49:48 -07:00
|
|
|
/// A lint pass boxed up as a trait object.
|
2018-03-03 06:18:09 +01:00
|
|
|
pub type EarlyLintPassObject = Box<dyn EarlyLintPass + sync::Send + sync::Sync + 'static>;
|
|
|
|
pub type LateLintPassObject = Box<dyn for<'a, 'tcx> LateLintPass<'a, 'tcx> + sync::Send
|
|
|
|
+ sync::Sync + 'static>;
|
2014-06-02 15:27:15 -07:00
|
|
|
|
2014-06-06 15:49:48 -07:00
|
|
|
/// How a lint level was set.
|
2019-11-09 23:17:42 +01:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, HashStable)]
|
2014-05-19 14:57:24 -07:00
|
|
|
pub enum LintSource {
|
2014-06-06 15:49:48 -07:00
|
|
|
/// Lint is at the default level as declared
|
|
|
|
/// in rustc or a plugin.
|
2013-05-09 13:22:26 -04:00
|
|
|
Default,
|
2012-06-04 16:07:54 -07:00
|
|
|
|
2014-06-06 15:49:48 -07:00
|
|
|
/// Lint level was set by an attribute.
|
2018-09-29 17:25:26 -07:00
|
|
|
Node(ast::Name, Span, Option<Symbol> /* RFC 2383 reason */),
|
2013-05-28 15:44:53 -05:00
|
|
|
|
2014-06-06 15:49:48 -07:00
|
|
|
/// Lint level was set by a command-line flag.
|
2017-01-05 18:55:43 -08:00
|
|
|
CommandLine(Symbol),
|
2013-05-24 10:27:31 +02:00
|
|
|
}
|
|
|
|
|
2014-06-06 15:49:48 -07:00
|
|
|
pub type LevelSource = (Level, LintSource);
|
2013-04-30 01:15:17 -04:00
|
|
|
|
2014-06-06 15:49:48 -07:00
|
|
|
pub mod builtin;
|
2018-12-06 13:50:04 +01:00
|
|
|
pub mod internal;
|
2014-06-06 15:49:48 -07:00
|
|
|
mod context;
|
2017-07-26 21:51:09 -07:00
|
|
|
mod levels;
|
|
|
|
|
|
|
|
pub use self::levels::{LintLevelSets, LintLevelMap};
|
|
|
|
|
2018-07-25 15:44:06 +03:00
|
|
|
#[derive(Default)]
|
2017-07-26 21:51:09 -07:00
|
|
|
pub struct LintBuffer {
|
|
|
|
map: NodeMap<Vec<BufferedEarlyLint>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintBuffer {
|
|
|
|
pub fn add_lint(&mut self,
|
|
|
|
lint: &'static Lint,
|
|
|
|
id: ast::NodeId,
|
|
|
|
sp: MultiSpan,
|
2018-02-22 22:34:06 -08:00
|
|
|
msg: &str,
|
|
|
|
diagnostic: BuiltinLintDiagnostics) {
|
2017-07-26 21:51:09 -07:00
|
|
|
let early_lint = BufferedEarlyLint {
|
|
|
|
lint_id: LintId::of(lint),
|
|
|
|
ast_id: id,
|
|
|
|
span: sp,
|
|
|
|
msg: msg.to_string(),
|
2018-02-22 22:34:06 -08:00
|
|
|
diagnostic
|
2017-07-26 21:51:09 -07:00
|
|
|
};
|
2018-07-21 22:43:31 +03:00
|
|
|
let arr = self.map.entry(id).or_default();
|
2017-07-26 21:51:09 -07:00
|
|
|
if !arr.contains(&early_lint) {
|
|
|
|
arr.push(early_lint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-25 08:08:53 -04:00
|
|
|
fn take(&mut self, id: ast::NodeId) -> Vec<BufferedEarlyLint> {
|
2018-10-12 16:16:00 +02:00
|
|
|
self.map.remove(&id).unwrap_or_default()
|
2017-07-26 21:51:09 -07:00
|
|
|
}
|
2019-10-25 09:15:33 -04:00
|
|
|
|
|
|
|
pub fn buffer_lint<S: Into<MultiSpan>>(
|
|
|
|
&mut self,
|
|
|
|
lint: &'static Lint,
|
|
|
|
id: ast::NodeId,
|
|
|
|
sp: S,
|
|
|
|
msg: &str,
|
|
|
|
) {
|
|
|
|
self.add_lint(lint, id, sp.into(), msg, BuiltinLintDiagnostics::Normal)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn buffer_lint_with_diagnostic<S: Into<MultiSpan>>(
|
|
|
|
&mut self,
|
|
|
|
lint: &'static Lint,
|
|
|
|
id: ast::NodeId,
|
|
|
|
sp: S,
|
|
|
|
msg: &str,
|
|
|
|
diagnostic: BuiltinLintDiagnostics,
|
|
|
|
) {
|
|
|
|
self.add_lint(lint, id, sp.into(), msg, diagnostic)
|
|
|
|
}
|
2017-07-26 21:51:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn struct_lint_level<'a>(sess: &'a Session,
|
|
|
|
lint: &'static Lint,
|
|
|
|
level: Level,
|
|
|
|
src: LintSource,
|
|
|
|
span: Option<MultiSpan>,
|
|
|
|
msg: &str)
|
|
|
|
-> DiagnosticBuilder<'a>
|
|
|
|
{
|
|
|
|
let mut err = match (level, span) {
|
|
|
|
(Level::Allow, _) => return sess.diagnostic().struct_dummy(),
|
|
|
|
(Level::Warn, Some(span)) => sess.struct_span_warn(span, msg),
|
|
|
|
(Level::Warn, None) => sess.struct_warn(msg),
|
|
|
|
(Level::Deny, Some(span)) |
|
|
|
|
(Level::Forbid, Some(span)) => sess.struct_span_err(span, msg),
|
|
|
|
(Level::Deny, None) |
|
|
|
|
(Level::Forbid, None) => sess.struct_err(msg),
|
|
|
|
};
|
|
|
|
|
2019-08-27 21:13:20 +01:00
|
|
|
// Check for future incompatibility lints and issue a stronger warning.
|
|
|
|
let lint_id = LintId::of(lint);
|
2019-10-08 21:51:18 -04:00
|
|
|
let future_incompatible = lint.future_incompatible;
|
2019-08-27 21:13:20 +01:00
|
|
|
|
|
|
|
// If this code originates in a foreign macro, aka something that this crate
|
|
|
|
// did not itself author, then it's likely that there's nothing this crate
|
|
|
|
// can do about it. We probably want to skip the lint entirely.
|
|
|
|
if err.span.primary_spans().iter().any(|s| in_external_macro(sess, *s)) {
|
|
|
|
// Any suggestions made here are likely to be incorrect, so anything we
|
|
|
|
// emit shouldn't be automatically fixed by rustfix.
|
|
|
|
err.allow_suggestions(false);
|
|
|
|
|
|
|
|
// If this is a future incompatible lint it'll become a hard error, so
|
|
|
|
// we have to emit *something*. Also allow lints to whitelist themselves
|
|
|
|
// on a case-by-case basis for emission in a foreign macro.
|
|
|
|
if future_incompatible.is_none() && !lint.report_in_external_macro {
|
|
|
|
err.cancel();
|
|
|
|
// Don't continue further, since we don't want to have
|
|
|
|
// `diag_span_note_once` called for a diagnostic that isn't emitted.
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-26 21:51:09 -07:00
|
|
|
let name = lint.name_lower();
|
|
|
|
match src {
|
|
|
|
LintSource::Default => {
|
|
|
|
sess.diag_note_once(
|
|
|
|
&mut err,
|
2017-11-24 10:26:42 -08:00
|
|
|
DiagnosticMessageId::from(lint),
|
2019-07-16 22:17:38 +02:00
|
|
|
&format!("`#[{}({})]` on by default", level.as_str(), name));
|
2017-07-26 21:51:09 -07:00
|
|
|
}
|
|
|
|
LintSource::CommandLine(lint_flag_val) => {
|
|
|
|
let flag = match level {
|
|
|
|
Level::Warn => "-W",
|
|
|
|
Level::Deny => "-D",
|
|
|
|
Level::Forbid => "-F",
|
|
|
|
Level::Allow => panic!(),
|
|
|
|
};
|
|
|
|
let hyphen_case_lint_name = name.replace("_", "-");
|
|
|
|
if lint_flag_val.as_str() == name {
|
|
|
|
sess.diag_note_once(
|
|
|
|
&mut err,
|
2017-11-24 10:26:42 -08:00
|
|
|
DiagnosticMessageId::from(lint),
|
2017-07-26 21:51:09 -07:00
|
|
|
&format!("requested on the command line with `{} {}`",
|
|
|
|
flag, hyphen_case_lint_name));
|
|
|
|
} else {
|
|
|
|
let hyphen_case_flag_val = lint_flag_val.as_str().replace("_", "-");
|
|
|
|
sess.diag_note_once(
|
|
|
|
&mut err,
|
2017-11-24 10:26:42 -08:00
|
|
|
DiagnosticMessageId::from(lint),
|
2017-07-26 21:51:09 -07:00
|
|
|
&format!("`{} {}` implied by `{} {}`",
|
|
|
|
flag, hyphen_case_lint_name, flag,
|
|
|
|
hyphen_case_flag_val));
|
|
|
|
}
|
|
|
|
}
|
2018-09-29 17:25:26 -07:00
|
|
|
LintSource::Node(lint_attr_name, src, reason) => {
|
|
|
|
if let Some(rationale) = reason {
|
|
|
|
err.note(&rationale.as_str());
|
|
|
|
}
|
2017-11-24 10:26:42 -08:00
|
|
|
sess.diag_span_note_once(&mut err, DiagnosticMessageId::from(lint),
|
|
|
|
src, "lint level defined here");
|
2017-07-26 21:51:09 -07:00
|
|
|
if lint_attr_name.as_str() != name {
|
|
|
|
let level_str = level.as_str();
|
2017-11-24 10:26:42 -08:00
|
|
|
sess.diag_note_once(&mut err, DiagnosticMessageId::from(lint),
|
2019-07-16 22:17:38 +02:00
|
|
|
&format!("`#[{}({})]` implied by `#[{}({})]`",
|
2017-07-26 21:51:09 -07:00
|
|
|
level_str, name, level_str, lint_attr_name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-27 08:21:22 +02:00
|
|
|
err.code(DiagnosticId::Lint(name));
|
2017-10-24 08:37:41 +02:00
|
|
|
|
2018-07-26 14:53:15 -07:00
|
|
|
if let Some(future_incompatible) = future_incompatible {
|
2018-03-03 19:27:49 +08:00
|
|
|
const STANDARD_MESSAGE: &str =
|
|
|
|
"this was previously accepted by the compiler but is being phased out; \
|
|
|
|
it will become a hard error";
|
|
|
|
|
2019-03-26 13:31:36 +01:00
|
|
|
let explanation = if lint_id == LintId::of(builtin::UNSTABLE_NAME_COLLISIONS) {
|
2018-03-03 19:27:49 +08:00
|
|
|
"once this method is added to the standard library, \
|
2018-05-01 16:52:03 +08:00
|
|
|
the ambiguity may cause an error or change in behavior!"
|
2018-03-03 19:27:49 +08:00
|
|
|
.to_owned()
|
2019-03-26 13:31:36 +01:00
|
|
|
} else if lint_id == LintId::of(builtin::MUTABLE_BORROW_RESERVATION_CONFLICT) {
|
2019-03-25 23:16:39 +00:00
|
|
|
"this borrowing pattern was not meant to be accepted, \
|
|
|
|
and may become a hard error in the future"
|
|
|
|
.to_owned()
|
2018-03-03 19:27:49 +08:00
|
|
|
} else if let Some(edition) = future_incompatible.edition {
|
|
|
|
format!("{} in the {} edition!", STANDARD_MESSAGE, edition)
|
2018-03-08 13:16:36 -08:00
|
|
|
} else {
|
2018-03-03 19:27:49 +08:00
|
|
|
format!("{} in a future release!", STANDARD_MESSAGE)
|
2018-03-08 13:16:36 -08:00
|
|
|
};
|
2017-07-26 21:51:09 -07:00
|
|
|
let citation = format!("for more information, see {}",
|
|
|
|
future_incompatible.reference);
|
|
|
|
err.warn(&explanation);
|
|
|
|
err.note(&citation);
|
2018-07-26 14:53:15 -07:00
|
|
|
}
|
2018-07-17 10:23:03 -07:00
|
|
|
|
2017-07-26 21:51:09 -07:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
pub fn maybe_lint_level_root(tcx: TyCtxt<'_>, id: hir::HirId) -> bool {
|
2019-06-14 18:58:55 +02:00
|
|
|
let attrs = tcx.hir().attrs(id);
|
2019-05-08 14:33:06 +10:00
|
|
|
attrs.iter().any(|attr| Level::from_symbol(attr.name_or_empty()).is_some())
|
2019-01-31 23:11:29 +01:00
|
|
|
}
|
|
|
|
|
2019-06-21 23:49:03 +02:00
|
|
|
fn lint_levels(tcx: TyCtxt<'_>, cnum: CrateNum) -> &LintLevelMap {
|
2017-07-26 21:51:09 -07:00
|
|
|
assert_eq!(cnum, LOCAL_CRATE);
|
2019-10-09 09:53:13 -04:00
|
|
|
let store = &tcx.lint_store;
|
2017-07-26 21:51:09 -07:00
|
|
|
let mut builder = LintLevelMapBuilder {
|
2019-10-25 13:41:51 -04:00
|
|
|
levels: LintLevelSets::builder(tcx.sess, false, &store),
|
2017-07-26 21:51:09 -07:00
|
|
|
tcx: tcx,
|
2019-10-09 09:53:13 -04:00
|
|
|
store: store,
|
2017-07-26 21:51:09 -07:00
|
|
|
};
|
2018-12-04 13:45:36 +01:00
|
|
|
let krate = tcx.hir().krate();
|
2017-07-26 21:51:09 -07:00
|
|
|
|
2019-10-09 08:46:11 -04:00
|
|
|
let push = builder.levels.push(&krate.attrs, &store);
|
2019-01-31 23:11:29 +01:00
|
|
|
builder.levels.register_id(hir::CRATE_HIR_ID);
|
2019-11-28 11:49:29 +01:00
|
|
|
for macro_def in krate.exported_macros {
|
2019-05-16 18:31:53 +02:00
|
|
|
builder.levels.register_id(macro_def.hir_id);
|
|
|
|
}
|
2019-01-31 23:11:29 +01:00
|
|
|
intravisit::walk_crate(&mut builder, krate);
|
|
|
|
builder.levels.pop(push);
|
2017-07-26 21:51:09 -07:00
|
|
|
|
2018-11-30 21:01:50 +01:00
|
|
|
tcx.arena.alloc(builder.levels.build_map())
|
2017-07-26 21:51:09 -07:00
|
|
|
}
|
|
|
|
|
2019-10-09 08:46:11 -04:00
|
|
|
struct LintLevelMapBuilder<'a, 'tcx> {
|
2017-07-26 21:51:09 -07:00
|
|
|
levels: levels::LintLevelsBuilder<'tcx>,
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2019-10-09 08:46:11 -04:00
|
|
|
store: &'a LintStore,
|
2017-07-26 21:51:09 -07:00
|
|
|
}
|
|
|
|
|
2019-10-09 08:46:11 -04:00
|
|
|
impl LintLevelMapBuilder<'_, '_> {
|
2017-07-26 21:51:09 -07:00
|
|
|
fn with_lint_attrs<F>(&mut self,
|
2019-02-24 09:33:17 +01:00
|
|
|
id: hir::HirId,
|
2017-07-26 21:51:09 -07:00
|
|
|
attrs: &[ast::Attribute],
|
|
|
|
f: F)
|
|
|
|
where F: FnOnce(&mut Self)
|
|
|
|
{
|
2019-10-09 08:46:11 -04:00
|
|
|
let push = self.levels.push(attrs, self.store);
|
2019-01-31 23:11:29 +01:00
|
|
|
if push.changed {
|
|
|
|
self.levels.register_id(id);
|
|
|
|
}
|
2017-07-26 21:51:09 -07:00
|
|
|
f(self);
|
|
|
|
self.levels.pop(push);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-09 08:46:11 -04:00
|
|
|
impl intravisit::Visitor<'tcx> for LintLevelMapBuilder<'_, 'tcx> {
|
2017-07-26 21:51:09 -07:00
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> intravisit::NestedVisitorMap<'this, 'tcx> {
|
2018-12-04 13:45:36 +01:00
|
|
|
intravisit::NestedVisitorMap::All(&self.tcx.hir())
|
2017-07-26 21:51:09 -07:00
|
|
|
}
|
|
|
|
|
2019-08-27 13:24:32 +02:00
|
|
|
fn visit_param(&mut self, param: &'tcx hir::Param) {
|
|
|
|
self.with_lint_attrs(param.hir_id, ¶m.attrs, |builder| {
|
|
|
|
intravisit::walk_param(builder, param);
|
2019-07-26 19:52:37 -03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-28 19:28:50 +01:00
|
|
|
fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) {
|
2019-02-24 09:33:17 +01:00
|
|
|
self.with_lint_attrs(it.hir_id, &it.attrs, |builder| {
|
2017-07-26 21:51:09 -07:00
|
|
|
intravisit::walk_item(builder, it);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-28 20:18:29 +01:00
|
|
|
fn visit_foreign_item(&mut self, it: &'tcx hir::ForeignItem<'tcx>) {
|
2019-02-24 09:33:17 +01:00
|
|
|
self.with_lint_attrs(it.hir_id, &it.attrs, |builder| {
|
2017-07-26 21:51:09 -07:00
|
|
|
intravisit::walk_foreign_item(builder, it);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_expr(&mut self, e: &'tcx hir::Expr) {
|
2019-02-24 09:33:17 +01:00
|
|
|
self.with_lint_attrs(e.hir_id, &e.attrs, |builder| {
|
2017-07-26 21:51:09 -07:00
|
|
|
intravisit::walk_expr(builder, e);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_struct_field(&mut self, s: &'tcx hir::StructField) {
|
2019-02-24 09:33:17 +01:00
|
|
|
self.with_lint_attrs(s.hir_id, &s.attrs, |builder| {
|
2017-07-26 21:51:09 -07:00
|
|
|
intravisit::walk_struct_field(builder, s);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_variant(&mut self,
|
|
|
|
v: &'tcx hir::Variant,
|
|
|
|
g: &'tcx hir::Generics,
|
2019-02-06 14:16:11 +01:00
|
|
|
item_id: hir::HirId) {
|
2019-08-13 21:40:21 -03:00
|
|
|
self.with_lint_attrs(v.id, &v.attrs, |builder| {
|
2017-07-26 21:51:09 -07:00
|
|
|
intravisit::walk_variant(builder, v, g, item_id);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_local(&mut self, l: &'tcx hir::Local) {
|
2019-02-24 09:33:17 +01:00
|
|
|
self.with_lint_attrs(l.hir_id, &l.attrs, |builder| {
|
2017-07-26 21:51:09 -07:00
|
|
|
intravisit::walk_local(builder, l);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-03-30 23:00:07 +00:00
|
|
|
fn visit_arm(&mut self, a: &'tcx hir::Arm) {
|
|
|
|
self.with_lint_attrs(a.hir_id, &a.attrs, |builder| {
|
|
|
|
intravisit::walk_arm(builder, a);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-28 21:47:10 +01:00
|
|
|
fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
|
2019-02-24 09:33:17 +01:00
|
|
|
self.with_lint_attrs(trait_item.hir_id, &trait_item.attrs, |builder| {
|
2017-07-26 21:51:09 -07:00
|
|
|
intravisit::walk_trait_item(builder, trait_item);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) {
|
2019-02-24 09:33:17 +01:00
|
|
|
self.with_lint_attrs(impl_item.hir_id, &impl_item.attrs, |builder| {
|
2017-07-26 21:51:09 -07:00
|
|
|
intravisit::walk_impl_item(builder, impl_item);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 22:02:42 -07:00
|
|
|
pub fn provide(providers: &mut Providers<'_>) {
|
2017-07-26 21:51:09 -07:00
|
|
|
providers.lint_levels = lint_levels;
|
|
|
|
}
|
2018-04-07 15:50:36 +05:30
|
|
|
|
2018-07-17 10:23:03 -07:00
|
|
|
/// Returns whether `span` originates in a foreign crate's external macro.
|
|
|
|
///
|
2019-03-16 22:33:15 +01:00
|
|
|
/// This is used to test whether a lint should not even begin to figure out whether it should
|
|
|
|
/// be reported on the current node.
|
2018-07-17 10:23:03 -07:00
|
|
|
pub fn in_external_macro(sess: &Session, span: Span) -> bool {
|
2019-08-13 23:56:42 +03:00
|
|
|
let expn_data = span.ctxt().outer_expn_data();
|
|
|
|
match expn_data.kind {
|
2019-07-07 16:45:41 +03:00
|
|
|
ExpnKind::Root | ExpnKind::Desugaring(DesugaringKind::ForLoop) => false,
|
2019-08-25 19:59:51 +01:00
|
|
|
ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => true, // well, it's "external"
|
2019-06-30 15:58:56 +03:00
|
|
|
ExpnKind::Macro(MacroKind::Bang, _) => {
|
2019-08-13 23:56:42 +03:00
|
|
|
if expn_data.def_site.is_dummy() {
|
2019-09-06 03:57:44 +01:00
|
|
|
// Dummy span for the `def_site` means it's an external macro.
|
2019-06-30 03:05:52 +03:00
|
|
|
return true;
|
|
|
|
}
|
2019-08-13 23:56:42 +03:00
|
|
|
match sess.source_map().span_to_snippet(expn_data.def_site) {
|
2019-05-23 21:39:27 +01:00
|
|
|
Ok(code) => !code.starts_with("macro_rules"),
|
2019-09-06 03:57:44 +01:00
|
|
|
// No snippet means external macro or compiler-builtin expansion.
|
2019-05-23 21:39:27 +01:00
|
|
|
Err(_) => true,
|
|
|
|
}
|
|
|
|
}
|
2019-06-30 15:58:56 +03:00
|
|
|
ExpnKind::Macro(..) => true, // definitely a plugin
|
2018-07-17 10:23:03 -07:00
|
|
|
}
|
2018-04-07 15:50:36 +05:30
|
|
|
}
|
2019-03-16 22:33:15 +01:00
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Returns `true` if `span` originates in a derive-macro's expansion.
|
2019-03-16 22:33:15 +01:00
|
|
|
pub fn in_derive_expansion(span: Span) -> bool {
|
2019-08-13 23:56:42 +03:00
|
|
|
if let ExpnKind::Macro(MacroKind::Derive, _) = span.ctxt().outer_expn_data().kind {
|
2019-08-11 03:00:05 +03:00
|
|
|
return true;
|
2019-03-16 22:33:15 +01:00
|
|
|
}
|
2019-06-30 15:58:56 +03:00
|
|
|
false
|
2019-03-16 22:33:15 +01:00
|
|
|
}
|