2014-01-25 07:37:51 +00:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-04 00:48:01 +00:00
|
|
|
// 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.
|
|
|
|
|
2014-06-06 22:49:48 +00:00
|
|
|
//! Lints, aka compiler warnings.
|
|
|
|
//!
|
2013-10-02 18:29:29 +00: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 13:10:16 +00: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 22:49:48 +00:00
|
|
|
//! within `builtin.rs`, which has further comments on how to add such a lint.
|
2014-06-19 00:26:14 +00:00
|
|
|
//! rustc can also load user-defined lint plugins via the plugin mechanism.
|
2014-06-02 22:27:15 +00:00
|
|
|
//!
|
2014-06-06 22:49:48 +00: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 13:10:16 +00:00
|
|
|
//! `add_lint()`. To emit lints after the main lint pass (from codegen, for
|
2014-06-06 22:49:48 +00:00
|
|
|
//! example) requires more effort. See `emit_lint` and `GatherNodeLevels`
|
|
|
|
//! in `context.rs`.
|
2013-05-17 22:28:44 +00:00
|
|
|
|
2014-11-06 08:05:53 +00:00
|
|
|
pub use self::Level::*;
|
|
|
|
pub use self::LintSource::*;
|
|
|
|
|
2018-03-03 05:18:09 +00:00
|
|
|
use rustc_data_structures::sync::{self, Lrc};
|
2017-07-27 04:51:09 +00:00
|
|
|
|
2017-10-27 06:21:22 +00:00
|
|
|
use errors::{DiagnosticBuilder, DiagnosticId};
|
2017-07-27 04:51:09 +00:00
|
|
|
use hir::def_id::{CrateNum, LOCAL_CRATE};
|
2018-06-21 07:04:50 +00:00
|
|
|
use hir::intravisit;
|
2017-01-28 23:13:21 +00:00
|
|
|
use hir;
|
2018-02-23 06:34:06 +00:00
|
|
|
use lint::builtin::BuiltinLintDiagnostics;
|
2018-07-12 01:54:12 +00:00
|
|
|
use lint::builtin::parser::QUESTION_MARK_MACRO_SEP;
|
2018-03-06 22:05:03 +00:00
|
|
|
use session::{Session, DiagnosticMessageId};
|
2018-07-18 18:53:54 +00:00
|
|
|
use std::{hash, ptr};
|
2014-06-06 22:49:48 +00:00
|
|
|
use syntax::ast;
|
2018-08-18 10:14:03 +00:00
|
|
|
use syntax::source_map::{MultiSpan, ExpnFormat};
|
2018-07-12 01:54:12 +00:00
|
|
|
use syntax::early_buffered_lints::BufferedEarlyLintId;
|
2018-03-15 03:30:06 +00:00
|
|
|
use syntax::edition::Edition;
|
2017-01-06 02:55:43 +00:00
|
|
|
use syntax::symbol::Symbol;
|
2017-07-27 04:51:09 +00:00
|
|
|
use syntax::visit as ast_visit;
|
|
|
|
use syntax_pos::Span;
|
|
|
|
use ty::TyCtxt;
|
2018-06-13 13:44:43 +00:00
|
|
|
use ty::query::Providers;
|
2017-07-27 04:51:09 +00:00
|
|
|
use util::nodemap::NodeMap;
|
2014-06-06 22:49:48 +00:00
|
|
|
|
2015-09-14 23:35:25 +00:00
|
|
|
pub use lint::context::{LateContext, EarlyContext, LintContext, LintStore,
|
2017-07-27 04:51:09 +00:00
|
|
|
check_crate, check_ast_crate,
|
|
|
|
FutureIncompatibleInfo, BufferedEarlyLint};
|
2017-01-28 12:01:45 +00:00
|
|
|
|
2014-06-06 22:49:48 +00:00
|
|
|
/// Specification of a single lint.
|
2015-03-30 13:38:44 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2014-06-06 22:49:48 +00:00
|
|
|
pub struct Lint {
|
|
|
|
/// A string identifier for the lint.
|
|
|
|
///
|
2014-06-13 20:04:52 +00:00
|
|
|
/// This identifies the lint in attributes and in command-line arguments.
|
|
|
|
/// In those contexts it is always lowercase, but this field is compared
|
|
|
|
/// in a way which is case-insensitive for ASCII characters. This allows
|
|
|
|
/// `declare_lint!()` invocations to follow the convention of upper-case
|
|
|
|
/// statics without repeating the name.
|
|
|
|
///
|
|
|
|
/// The name is written with underscores, e.g. "unused_imports".
|
|
|
|
/// On the command line, underscores become dashes.
|
2014-06-06 22:49:48 +00:00
|
|
|
pub name: &'static str,
|
|
|
|
|
|
|
|
/// Default level for the lint.
|
|
|
|
pub default_level: Level,
|
|
|
|
|
|
|
|
/// Description of the lint or the issue it detects.
|
|
|
|
///
|
|
|
|
/// e.g. "imports that are never used"
|
|
|
|
pub desc: &'static str,
|
2018-02-23 00:51:42 +00:00
|
|
|
|
2018-02-17 23:33:27 +00:00
|
|
|
/// Starting at the given edition, default to the given lint level. If this is `None`, then use
|
|
|
|
/// `default_level`.
|
|
|
|
pub edition_lint_opts: Option<(Edition, Level)>,
|
2018-07-20 20:45:52 +00:00
|
|
|
|
|
|
|
/// Whether this lint is reported even inside expansions of external macros
|
|
|
|
pub report_in_external_macro: bool,
|
2014-06-06 22:49:48 +00:00
|
|
|
}
|
|
|
|
|
2014-06-13 20:04:52 +00:00
|
|
|
impl Lint {
|
2018-07-12 01:54:12 +00:00
|
|
|
/// Returns the `rust::lint::Lint` for a `syntax::early_buffered_lints::BufferedEarlyLintId`.
|
|
|
|
pub fn from_parser_lint_id(lint_id: BufferedEarlyLintId) -> &'static Self {
|
|
|
|
match lint_id {
|
|
|
|
BufferedEarlyLintId::QuestionMarkMacroSep => QUESTION_MARK_MACRO_SEP,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-13 20:04:52 +00:00
|
|
|
/// Get the lint's name, with ASCII letters converted to lowercase.
|
|
|
|
pub fn name_lower(&self) -> String {
|
2014-12-05 17:57:42 +00:00
|
|
|
self.name.to_ascii_lowercase()
|
2014-06-13 20:04:52 +00:00
|
|
|
}
|
2018-02-23 00:51:42 +00:00
|
|
|
|
|
|
|
pub fn default_level(&self, session: &Session) -> Level {
|
2018-02-17 23:33:27 +00:00
|
|
|
self.edition_lint_opts
|
|
|
|
.filter(|(e, _)| *e <= session.edition())
|
|
|
|
.map(|(_, l)| l)
|
|
|
|
.unwrap_or(self.default_level)
|
2018-02-23 00:51:42 +00:00
|
|
|
}
|
2014-06-13 20:04:52 +00:00
|
|
|
}
|
|
|
|
|
2014-06-06 22:49:48 +00:00
|
|
|
/// Declare a static item of type `&'static Lint`.
|
2014-06-04 21:35:58 +00:00
|
|
|
#[macro_export]
|
2014-11-14 17:18:10 +00:00
|
|
|
macro_rules! declare_lint {
|
2018-02-17 23:33:27 +00:00
|
|
|
($vis: vis $NAME: ident, $Level: ident, $desc: expr) => (
|
2018-07-20 20:45:52 +00:00
|
|
|
declare_lint!{$vis $NAME, $Level, $desc, false}
|
|
|
|
);
|
2018-07-21 10:36:18 +00:00
|
|
|
($vis: vis $NAME: ident, $Level: ident, $desc: expr, report_in_external_macro: $rep: expr) => (
|
|
|
|
declare_lint!{$vis $NAME, $Level, $desc, $rep}
|
2018-07-20 20:45:52 +00:00
|
|
|
);
|
|
|
|
($vis: vis $NAME: ident, $Level: ident, $desc: expr, $external: expr) => (
|
2018-02-23 00:51:42 +00:00
|
|
|
$vis static $NAME: &$crate::lint::Lint = &$crate::lint::Lint {
|
|
|
|
name: stringify!($NAME),
|
|
|
|
default_level: $crate::lint::$Level,
|
|
|
|
desc: $desc,
|
2018-02-17 23:33:27 +00:00
|
|
|
edition_lint_opts: None,
|
2018-07-20 20:45:52 +00:00
|
|
|
report_in_external_macro: $external,
|
2018-02-23 00:51:42 +00:00
|
|
|
};
|
|
|
|
);
|
2018-02-17 23:33:27 +00:00
|
|
|
($vis: vis $NAME: ident, $Level: ident, $desc: expr,
|
2018-06-16 02:49:00 +00:00
|
|
|
$lint_edition: expr => $edition_level: ident
|
2018-02-17 23:33:27 +00:00
|
|
|
) => (
|
2017-10-20 21:00:57 +00:00
|
|
|
$vis static $NAME: &$crate::lint::Lint = &$crate::lint::Lint {
|
|
|
|
name: stringify!($NAME),
|
|
|
|
default_level: $crate::lint::$Level,
|
2018-02-23 00:51:42 +00:00
|
|
|
desc: $desc,
|
2018-02-17 23:33:27 +00:00
|
|
|
edition_lint_opts: Some(($lint_edition, $crate::lint::Level::$edition_level)),
|
2018-07-20 20:45:52 +00:00
|
|
|
report_in_external_macro: false,
|
2017-10-20 21:00:57 +00:00
|
|
|
};
|
2018-02-23 00:51:42 +00:00
|
|
|
);
|
2014-11-14 17:18:10 +00:00
|
|
|
}
|
2014-06-04 21:35:58 +00:00
|
|
|
|
2018-07-30 09:20:11 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! declare_tool_lint {
|
|
|
|
($vis: vis $tool: ident ::$NAME: ident, $Level: ident, $desc: expr) => (
|
|
|
|
declare_tool_lint!{$vis $tool::$NAME, $Level, $desc, false}
|
|
|
|
);
|
|
|
|
($vis: vis $tool: ident ::$NAME: ident, $Level: ident, $desc: expr,
|
|
|
|
report_in_external_macro: $rep: expr) => (
|
|
|
|
declare_tool_lint!{$vis $tool::$NAME, $Level, $desc, $rep}
|
|
|
|
);
|
|
|
|
($vis: vis $tool: ident ::$NAME: ident, $Level: ident, $desc: expr, $external: expr) => (
|
|
|
|
$vis static $NAME: &$crate::lint::Lint = &$crate::lint::Lint {
|
|
|
|
name: &concat!(stringify!($tool), "::", stringify!($NAME)),
|
|
|
|
default_level: $crate::lint::$Level,
|
|
|
|
desc: $desc,
|
|
|
|
edition_lint_opts: None,
|
|
|
|
report_in_external_macro: $external,
|
|
|
|
};
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-06-06 22:49:48 +00:00
|
|
|
/// Declare a static `LintArray` and return it as an expression.
|
2014-06-04 21:35:58 +00:00
|
|
|
#[macro_export]
|
2018-01-15 16:29:30 +00:00
|
|
|
macro_rules! lint_array {
|
2018-06-16 02:49:00 +00:00
|
|
|
($( $lint:expr ),* ,) => { lint_array!( $($lint),* ) };
|
|
|
|
($( $lint:expr ),*) => {{
|
2018-06-21 07:04:50 +00:00
|
|
|
vec![$($lint),*]
|
2018-01-15 16:29:30 +00:00
|
|
|
}}
|
|
|
|
}
|
2014-06-01 23:16:00 +00:00
|
|
|
|
2018-06-21 07:04:50 +00:00
|
|
|
pub type LintArray = Vec<&'static Lint>;
|
2014-06-04 21:35:58 +00:00
|
|
|
|
2014-06-10 21:03:19 +00:00
|
|
|
pub trait LintPass {
|
2014-06-04 21:35:58 +00:00
|
|
|
/// Get descriptions of the lints this `LintPass` object can emit.
|
|
|
|
///
|
|
|
|
/// NB: there is no enforcement that the object only emits lints it registered.
|
|
|
|
/// And some `rustc` internal `LintPass`es register lints to be emitted by other
|
|
|
|
/// parts of the compiler. If you want enforced access restrictions for your
|
|
|
|
/// `Lint`, make it a private `static` item in its own module.
|
|
|
|
fn get_lints(&self) -> LintArray;
|
2015-09-14 23:35:25 +00:00
|
|
|
}
|
2014-06-04 21:35:58 +00:00
|
|
|
|
2018-06-21 07:04:50 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! late_lint_methods {
|
|
|
|
($macro:path, $args:tt, [$hir:tt]) => (
|
|
|
|
$macro!($args, [$hir], [
|
|
|
|
fn check_body(a: &$hir hir::Body);
|
|
|
|
fn check_body_post(a: &$hir hir::Body);
|
|
|
|
fn check_name(a: Span, b: ast::Name);
|
|
|
|
fn check_crate(a: &$hir hir::Crate);
|
|
|
|
fn check_crate_post(a: &$hir hir::Crate);
|
|
|
|
fn check_mod(a: &$hir hir::Mod, b: Span, c: ast::NodeId);
|
|
|
|
fn check_mod_post(a: &$hir hir::Mod, b: Span, c: ast::NodeId);
|
|
|
|
fn check_foreign_item(a: &$hir hir::ForeignItem);
|
|
|
|
fn check_foreign_item_post(a: &$hir hir::ForeignItem);
|
|
|
|
fn check_item(a: &$hir hir::Item);
|
|
|
|
fn check_item_post(a: &$hir hir::Item);
|
|
|
|
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_decl(a: &$hir hir::Decl);
|
|
|
|
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,
|
|
|
|
e: ast::NodeId);
|
|
|
|
fn check_fn_post(
|
|
|
|
a: hir::intravisit::FnKind<$hir>,
|
|
|
|
b: &$hir hir::FnDecl,
|
|
|
|
c: &$hir hir::Body,
|
|
|
|
d: Span,
|
|
|
|
e: ast::NodeId
|
|
|
|
);
|
|
|
|
fn check_trait_item(a: &$hir hir::TraitItem);
|
|
|
|
fn check_trait_item_post(a: &$hir hir::TraitItem);
|
|
|
|
fn check_impl_item(a: &$hir hir::ImplItem);
|
|
|
|
fn check_impl_item_post(a: &$hir hir::ImplItem);
|
|
|
|
fn check_struct_def(
|
|
|
|
a: &$hir hir::VariantData,
|
|
|
|
b: ast::Name,
|
|
|
|
c: &$hir hir::Generics,
|
|
|
|
d: ast::NodeId
|
|
|
|
);
|
|
|
|
fn check_struct_def_post(
|
|
|
|
a: &$hir hir::VariantData,
|
|
|
|
b: ast::Name,
|
|
|
|
c: &$hir hir::Generics,
|
|
|
|
d: ast::NodeId
|
|
|
|
);
|
|
|
|
fn check_struct_field(a: &$hir hir::StructField);
|
|
|
|
fn check_variant(a: &$hir hir::Variant, b: &$hir hir::Generics);
|
|
|
|
fn check_variant_post(a: &$hir hir::Variant, b: &$hir hir::Generics);
|
|
|
|
fn check_lifetime(a: &$hir hir::Lifetime);
|
2018-07-31 16:43:51 +00:00
|
|
|
fn check_path(a: &$hir hir::Path, b: hir::HirId);
|
2018-06-21 07:04:50 +00: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-14 23:35:25 +00: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 07:04:50 +00:00
|
|
|
|
|
|
|
macro_rules! expand_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_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 {
|
|
|
|
([$name:ident, [$($passes:ident: $constructor:expr,)*]], [$hir:tt], $methods:tt) => (
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
struct $name {
|
|
|
|
$($passes: $passes,)*
|
|
|
|
}
|
|
|
|
|
|
|
|
impl $name {
|
|
|
|
fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
$($passes: $constructor,)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for $name {
|
|
|
|
expand_combined_late_lint_pass_methods!([$($passes),*], $methods);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for $name {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
let mut lints = Vec::new();
|
|
|
|
$(lints.extend_from_slice(&self.$passes.get_lints());)*
|
|
|
|
lints
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2015-09-14 23:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait EarlyLintPass: LintPass {
|
2018-03-19 00:54:56 +00:00
|
|
|
fn check_ident(&mut self, _: &EarlyContext, _: ast::Ident) { }
|
2015-09-14 23:35:25 +00:00
|
|
|
fn check_crate(&mut self, _: &EarlyContext, _: &ast::Crate) { }
|
2016-02-11 04:58:09 +00:00
|
|
|
fn check_crate_post(&mut self, _: &EarlyContext, _: &ast::Crate) { }
|
2015-09-14 23:35:25 +00:00
|
|
|
fn check_mod(&mut self, _: &EarlyContext, _: &ast::Mod, _: Span, _: ast::NodeId) { }
|
2016-02-26 23:21:20 +00:00
|
|
|
fn check_mod_post(&mut self, _: &EarlyContext, _: &ast::Mod, _: Span, _: ast::NodeId) { }
|
2015-09-14 23:35:25 +00:00
|
|
|
fn check_foreign_item(&mut self, _: &EarlyContext, _: &ast::ForeignItem) { }
|
2016-02-26 23:21:20 +00:00
|
|
|
fn check_foreign_item_post(&mut self, _: &EarlyContext, _: &ast::ForeignItem) { }
|
2015-09-14 23:35:25 +00:00
|
|
|
fn check_item(&mut self, _: &EarlyContext, _: &ast::Item) { }
|
2016-02-11 17:19:18 +00:00
|
|
|
fn check_item_post(&mut self, _: &EarlyContext, _: &ast::Item) { }
|
2015-09-14 23:35:25 +00:00
|
|
|
fn check_local(&mut self, _: &EarlyContext, _: &ast::Local) { }
|
|
|
|
fn check_block(&mut self, _: &EarlyContext, _: &ast::Block) { }
|
2016-02-11 04:58:09 +00:00
|
|
|
fn check_block_post(&mut self, _: &EarlyContext, _: &ast::Block) { }
|
2015-09-14 23:35:25 +00:00
|
|
|
fn check_stmt(&mut self, _: &EarlyContext, _: &ast::Stmt) { }
|
|
|
|
fn check_arm(&mut self, _: &EarlyContext, _: &ast::Arm) { }
|
|
|
|
fn check_pat(&mut self, _: &EarlyContext, _: &ast::Pat) { }
|
|
|
|
fn check_expr(&mut self, _: &EarlyContext, _: &ast::Expr) { }
|
|
|
|
fn check_expr_post(&mut self, _: &EarlyContext, _: &ast::Expr) { }
|
|
|
|
fn check_ty(&mut self, _: &EarlyContext, _: &ast::Ty) { }
|
2017-10-16 19:07:26 +00:00
|
|
|
fn check_generic_param(&mut self, _: &EarlyContext, _: &ast::GenericParam) { }
|
2015-09-14 23:35:25 +00:00
|
|
|
fn check_generics(&mut self, _: &EarlyContext, _: &ast::Generics) { }
|
2018-02-18 18:37:07 +00:00
|
|
|
fn check_where_predicate(&mut self, _: &EarlyContext, _: &ast::WherePredicate) { }
|
|
|
|
fn check_poly_trait_ref(&mut self, _: &EarlyContext, _: &ast::PolyTraitRef,
|
|
|
|
_: &ast::TraitBoundModifier) { }
|
2015-09-14 23:35:25 +00:00
|
|
|
fn check_fn(&mut self, _: &EarlyContext,
|
2016-10-25 23:17:29 +00:00
|
|
|
_: ast_visit::FnKind, _: &ast::FnDecl, _: Span, _: ast::NodeId) { }
|
2016-02-26 23:21:20 +00:00
|
|
|
fn check_fn_post(&mut self, _: &EarlyContext,
|
2016-10-25 23:17:29 +00:00
|
|
|
_: ast_visit::FnKind, _: &ast::FnDecl, _: Span, _: ast::NodeId) { }
|
2015-09-14 23:35:25 +00:00
|
|
|
fn check_trait_item(&mut self, _: &EarlyContext, _: &ast::TraitItem) { }
|
2016-02-26 23:21:20 +00:00
|
|
|
fn check_trait_item_post(&mut self, _: &EarlyContext, _: &ast::TraitItem) { }
|
2015-09-14 23:35:25 +00:00
|
|
|
fn check_impl_item(&mut self, _: &EarlyContext, _: &ast::ImplItem) { }
|
2016-02-26 23:21:20 +00:00
|
|
|
fn check_impl_item_post(&mut self, _: &EarlyContext, _: &ast::ImplItem) { }
|
2015-09-14 23:35:25 +00:00
|
|
|
fn check_struct_def(&mut self, _: &EarlyContext,
|
2015-10-08 00:20:57 +00:00
|
|
|
_: &ast::VariantData, _: ast::Ident, _: &ast::Generics, _: ast::NodeId) { }
|
2015-09-14 23:35:25 +00:00
|
|
|
fn check_struct_def_post(&mut self, _: &EarlyContext,
|
2015-10-08 00:20:57 +00:00
|
|
|
_: &ast::VariantData, _: ast::Ident, _: &ast::Generics, _: ast::NodeId) { }
|
2015-09-14 23:35:25 +00:00
|
|
|
fn check_struct_field(&mut self, _: &EarlyContext, _: &ast::StructField) { }
|
|
|
|
fn check_variant(&mut self, _: &EarlyContext, _: &ast::Variant, _: &ast::Generics) { }
|
|
|
|
fn check_variant_post(&mut self, _: &EarlyContext, _: &ast::Variant, _: &ast::Generics) { }
|
2015-09-28 11:26:26 +00:00
|
|
|
fn check_lifetime(&mut self, _: &EarlyContext, _: &ast::Lifetime) { }
|
2015-09-14 23:35:25 +00:00
|
|
|
fn check_path(&mut self, _: &EarlyContext, _: &ast::Path, _: ast::NodeId) { }
|
|
|
|
fn check_attribute(&mut self, _: &EarlyContext, _: &ast::Attribute) { }
|
2018-07-14 14:40:17 +00:00
|
|
|
fn check_mac_def(&mut self, _: &EarlyContext, _: &ast::MacroDef, _id: ast::NodeId) { }
|
|
|
|
fn check_mac(&mut self, _: &EarlyContext, _: &ast::Mac) { }
|
2015-09-10 04:40:59 +00:00
|
|
|
|
|
|
|
/// Called when entering a syntax node that can have lint attributes such
|
|
|
|
/// as `#[allow(...)]`. Called with *all* the attributes of that node.
|
2015-09-14 23:35:25 +00:00
|
|
|
fn enter_lint_attrs(&mut self, _: &EarlyContext, _: &[ast::Attribute]) { }
|
2015-09-10 04:40:59 +00:00
|
|
|
|
2015-09-14 23:35:25 +00:00
|
|
|
/// Counterpart to `enter_lint_attrs`.
|
|
|
|
fn exit_lint_attrs(&mut self, _: &EarlyContext, _: &[ast::Attribute]) { }
|
2014-06-02 22:27:15 +00:00
|
|
|
}
|
|
|
|
|
2014-06-06 22:49:48 +00:00
|
|
|
/// A lint pass boxed up as a trait object.
|
2018-03-03 05:18:09 +00: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 22:27:15 +00:00
|
|
|
|
2018-07-14 14:40:17 +00:00
|
|
|
|
|
|
|
|
2014-06-04 21:35:58 +00:00
|
|
|
/// Identifies a lint known to the compiler.
|
2015-11-17 23:56:13 +00:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
2014-06-04 21:35:58 +00:00
|
|
|
pub struct LintId {
|
|
|
|
// Identity is based on pointer equality of this field.
|
|
|
|
lint: &'static Lint,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq for LintId {
|
|
|
|
fn eq(&self, other: &LintId) -> bool {
|
2018-07-18 18:53:54 +00:00
|
|
|
ptr::eq(self.lint, other.lint)
|
2014-06-04 21:35:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for LintId { }
|
|
|
|
|
2015-02-18 04:48:07 +00:00
|
|
|
impl hash::Hash for LintId {
|
|
|
|
fn hash<H: hash::Hasher>(&self, state: &mut H) {
|
|
|
|
let ptr = self.lint as *const Lint;
|
|
|
|
ptr.hash(state);
|
|
|
|
}
|
|
|
|
}
|
2012-01-19 08:50:51 +00:00
|
|
|
|
2014-06-04 21:35:58 +00:00
|
|
|
impl LintId {
|
2014-06-06 22:49:48 +00:00
|
|
|
/// Get the `LintId` for a `Lint`.
|
2014-06-04 21:35:58 +00:00
|
|
|
pub fn of(lint: &'static Lint) -> LintId {
|
|
|
|
LintId {
|
2017-07-03 18:19:51 +00:00
|
|
|
lint,
|
2014-06-04 21:35:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-14 16:19:42 +00:00
|
|
|
pub fn lint_name_raw(&self) -> &'static str {
|
|
|
|
self.lint.name
|
|
|
|
}
|
|
|
|
|
2014-06-06 22:49:48 +00:00
|
|
|
/// Get the name of the lint.
|
2016-08-24 01:27:20 +00:00
|
|
|
pub fn to_string(&self) -> String {
|
2014-06-13 20:04:52 +00:00
|
|
|
self.lint.name_lower()
|
Nomenclature fixes in the lint checker. Fewer double-negatives.
New style is allow(foo), warn(foo), deny(foo) and forbid(foo),
mirrored by -A foo, -W foo, -D foo and -F foo on command line.
These replace -W no-foo, -W foo, -W err-foo, respectively.
Forbid is new, and means "deny, and you can't override it".
2012-07-27 00:08:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-06 22:49:48 +00:00
|
|
|
/// Setting for how to handle a lint.
|
2016-08-02 20:53:58 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
|
2014-05-21 11:50:37 +00:00
|
|
|
pub enum Level {
|
2018-02-23 00:51:42 +00:00
|
|
|
Allow, Warn, Deny, Forbid,
|
2012-01-21 17:20:22 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 16:19:42 +00:00
|
|
|
impl_stable_hash_for!(enum self::Level {
|
|
|
|
Allow,
|
|
|
|
Warn,
|
|
|
|
Deny,
|
|
|
|
Forbid
|
|
|
|
});
|
|
|
|
|
2014-06-04 21:35:58 +00:00
|
|
|
impl Level {
|
2014-06-06 22:49:48 +00:00
|
|
|
/// Convert a level to a lower-case string.
|
2014-06-04 21:35:58 +00:00
|
|
|
pub fn as_str(self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
Allow => "allow",
|
|
|
|
Warn => "warn",
|
|
|
|
Deny => "deny",
|
|
|
|
Forbid => "forbid",
|
|
|
|
}
|
|
|
|
}
|
2013-07-17 04:12:16 +00:00
|
|
|
|
2014-06-06 22:49:48 +00:00
|
|
|
/// Convert a lower-case string to a level.
|
2014-06-04 21:35:58 +00:00
|
|
|
pub fn from_str(x: &str) -> Option<Level> {
|
|
|
|
match x {
|
|
|
|
"allow" => Some(Allow),
|
|
|
|
"warn" => Some(Warn),
|
|
|
|
"deny" => Some(Deny),
|
|
|
|
"forbid" => Some(Forbid),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-30 05:15:17 +00:00
|
|
|
|
2014-06-06 22:49:48 +00:00
|
|
|
/// How a lint level was set.
|
2015-01-04 03:54:18 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
2014-05-19 21:57:24 +00:00
|
|
|
pub enum LintSource {
|
2014-06-06 22:49:48 +00:00
|
|
|
/// Lint is at the default level as declared
|
|
|
|
/// in rustc or a plugin.
|
2013-05-09 17:22:26 +00:00
|
|
|
Default,
|
2012-06-04 23:07:54 +00:00
|
|
|
|
2014-06-06 22:49:48 +00:00
|
|
|
/// Lint level was set by an attribute.
|
2017-01-06 02:55:36 +00:00
|
|
|
Node(ast::Name, Span),
|
2013-05-28 20:44:53 +00:00
|
|
|
|
2014-06-06 22:49:48 +00:00
|
|
|
/// Lint level was set by a command-line flag.
|
2017-01-06 02:55:43 +00:00
|
|
|
CommandLine(Symbol),
|
2013-05-24 08:27:31 +00:00
|
|
|
}
|
|
|
|
|
2017-08-14 16:19:42 +00:00
|
|
|
impl_stable_hash_for!(enum self::LintSource {
|
|
|
|
Default,
|
|
|
|
Node(name, span),
|
|
|
|
CommandLine(text)
|
|
|
|
});
|
|
|
|
|
2014-06-06 22:49:48 +00:00
|
|
|
pub type LevelSource = (Level, LintSource);
|
2013-04-30 05:15:17 +00:00
|
|
|
|
2014-06-06 22:49:48 +00:00
|
|
|
pub mod builtin;
|
|
|
|
mod context;
|
2017-07-27 04:51:09 +00:00
|
|
|
mod levels;
|
|
|
|
|
|
|
|
pub use self::levels::{LintLevelSets, LintLevelMap};
|
|
|
|
|
|
|
|
pub struct LintBuffer {
|
|
|
|
map: NodeMap<Vec<BufferedEarlyLint>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintBuffer {
|
|
|
|
pub fn new() -> LintBuffer {
|
|
|
|
LintBuffer { map: NodeMap() }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_lint(&mut self,
|
|
|
|
lint: &'static Lint,
|
|
|
|
id: ast::NodeId,
|
|
|
|
sp: MultiSpan,
|
2018-02-23 06:34:06 +00:00
|
|
|
msg: &str,
|
|
|
|
diagnostic: BuiltinLintDiagnostics) {
|
2017-07-27 04:51:09 +00:00
|
|
|
let early_lint = BufferedEarlyLint {
|
|
|
|
lint_id: LintId::of(lint),
|
|
|
|
ast_id: id,
|
|
|
|
span: sp,
|
|
|
|
msg: msg.to_string(),
|
2018-02-23 06:34:06 +00:00
|
|
|
diagnostic
|
2017-07-27 04:51:09 +00:00
|
|
|
};
|
2018-07-21 19:43:31 +00:00
|
|
|
let arr = self.map.entry(id).or_default();
|
2017-07-27 04:51:09 +00:00
|
|
|
if !arr.contains(&early_lint) {
|
|
|
|
arr.push(early_lint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn take(&mut self, id: ast::NodeId) -> Vec<BufferedEarlyLint> {
|
|
|
|
self.map.remove(&id).unwrap_or(Vec::new())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_any(&self) -> Option<&[BufferedEarlyLint]> {
|
|
|
|
let key = self.map.keys().next().map(|k| *k);
|
|
|
|
key.map(|k| &self.map[&k][..])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
};
|
|
|
|
|
|
|
|
let name = lint.name_lower();
|
|
|
|
match src {
|
|
|
|
LintSource::Default => {
|
|
|
|
sess.diag_note_once(
|
|
|
|
&mut err,
|
2017-11-24 18:26:42 +00:00
|
|
|
DiagnosticMessageId::from(lint),
|
2017-07-27 04:51:09 +00:00
|
|
|
&format!("#[{}({})] on by default", level.as_str(), name));
|
|
|
|
}
|
|
|
|
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 18:26:42 +00:00
|
|
|
DiagnosticMessageId::from(lint),
|
2017-07-27 04:51:09 +00: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 18:26:42 +00:00
|
|
|
DiagnosticMessageId::from(lint),
|
2017-07-27 04:51:09 +00:00
|
|
|
&format!("`{} {}` implied by `{} {}`",
|
|
|
|
flag, hyphen_case_lint_name, flag,
|
|
|
|
hyphen_case_flag_val));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LintSource::Node(lint_attr_name, src) => {
|
2017-11-24 18:26:42 +00:00
|
|
|
sess.diag_span_note_once(&mut err, DiagnosticMessageId::from(lint),
|
|
|
|
src, "lint level defined here");
|
2017-07-27 04:51:09 +00:00
|
|
|
if lint_attr_name.as_str() != name {
|
|
|
|
let level_str = level.as_str();
|
2017-11-24 18:26:42 +00:00
|
|
|
sess.diag_note_once(&mut err, DiagnosticMessageId::from(lint),
|
2017-07-27 04:51:09 +00:00
|
|
|
&format!("#[{}({})] implied by #[{}({})]",
|
|
|
|
level_str, name, level_str, lint_attr_name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-27 06:21:22 +00:00
|
|
|
err.code(DiagnosticId::Lint(name));
|
2017-10-24 06:37:41 +00:00
|
|
|
|
2017-07-27 04:51:09 +00:00
|
|
|
// Check for future incompatibility lints and issue a stronger warning.
|
|
|
|
let lints = sess.lint_store.borrow();
|
2018-03-03 11:27:49 +00:00
|
|
|
let lint_id = LintId::of(lint);
|
2018-07-26 21:53:15 +00:00
|
|
|
let future_incompatible = lints.future_incompatible(lint_id);
|
|
|
|
if let Some(future_incompatible) = future_incompatible {
|
2018-03-03 11:27:49 +00:00
|
|
|
const STANDARD_MESSAGE: &str =
|
|
|
|
"this was previously accepted by the compiler but is being phased out; \
|
|
|
|
it will become a hard error";
|
|
|
|
|
2018-05-18 22:13:53 +00:00
|
|
|
let explanation = if lint_id == LintId::of(::lint::builtin::UNSTABLE_NAME_COLLISIONS) {
|
2018-03-03 11:27:49 +00:00
|
|
|
"once this method is added to the standard library, \
|
2018-05-01 08:52:03 +00:00
|
|
|
the ambiguity may cause an error or change in behavior!"
|
2018-03-03 11:27:49 +00:00
|
|
|
.to_owned()
|
|
|
|
} else if let Some(edition) = future_incompatible.edition {
|
|
|
|
format!("{} in the {} edition!", STANDARD_MESSAGE, edition)
|
2018-03-08 21:16:36 +00:00
|
|
|
} else {
|
2018-03-03 11:27:49 +00:00
|
|
|
format!("{} in a future release!", STANDARD_MESSAGE)
|
2018-03-08 21:16:36 +00:00
|
|
|
};
|
2017-07-27 04:51:09 +00:00
|
|
|
let citation = format!("for more information, see {}",
|
|
|
|
future_incompatible.reference);
|
|
|
|
err.warn(&explanation);
|
|
|
|
err.note(&citation);
|
2018-07-26 21:53:15 +00:00
|
|
|
}
|
2018-07-17 17:23:03 +00:00
|
|
|
|
2018-07-26 21:53:15 +00: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()
|
2018-07-17 17:23:03 +00:00
|
|
|
}
|
2017-07-27 04:51:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lint_levels<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, cnum: CrateNum)
|
2018-02-27 16:11:14 +00:00
|
|
|
-> Lrc<LintLevelMap>
|
2017-07-27 04:51:09 +00:00
|
|
|
{
|
|
|
|
assert_eq!(cnum, LOCAL_CRATE);
|
|
|
|
let mut builder = LintLevelMapBuilder {
|
|
|
|
levels: LintLevelSets::builder(tcx.sess),
|
|
|
|
tcx: tcx,
|
|
|
|
};
|
|
|
|
let krate = tcx.hir.krate();
|
|
|
|
|
|
|
|
builder.with_lint_attrs(ast::CRATE_NODE_ID, &krate.attrs, |builder| {
|
|
|
|
intravisit::walk_crate(builder, krate);
|
|
|
|
});
|
|
|
|
|
2018-02-27 16:11:14 +00:00
|
|
|
Lrc::new(builder.levels.build_map())
|
2017-07-27 04:51:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct LintLevelMapBuilder<'a, 'tcx: 'a> {
|
|
|
|
levels: levels::LintLevelsBuilder<'tcx>,
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> LintLevelMapBuilder<'a, 'tcx> {
|
|
|
|
fn with_lint_attrs<F>(&mut self,
|
|
|
|
id: ast::NodeId,
|
|
|
|
attrs: &[ast::Attribute],
|
|
|
|
f: F)
|
|
|
|
where F: FnOnce(&mut Self)
|
|
|
|
{
|
|
|
|
let push = self.levels.push(attrs);
|
|
|
|
self.levels.register_id(self.tcx.hir.definitions().node_to_hir_id(id));
|
|
|
|
f(self);
|
|
|
|
self.levels.pop(push);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> intravisit::Visitor<'tcx> for LintLevelMapBuilder<'a, 'tcx> {
|
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> intravisit::NestedVisitorMap<'this, 'tcx> {
|
|
|
|
intravisit::NestedVisitorMap::All(&self.tcx.hir)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_item(&mut self, it: &'tcx hir::Item) {
|
|
|
|
self.with_lint_attrs(it.id, &it.attrs, |builder| {
|
|
|
|
intravisit::walk_item(builder, it);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_foreign_item(&mut self, it: &'tcx hir::ForeignItem) {
|
|
|
|
self.with_lint_attrs(it.id, &it.attrs, |builder| {
|
|
|
|
intravisit::walk_foreign_item(builder, it);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_expr(&mut self, e: &'tcx hir::Expr) {
|
|
|
|
self.with_lint_attrs(e.id, &e.attrs, |builder| {
|
|
|
|
intravisit::walk_expr(builder, e);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_struct_field(&mut self, s: &'tcx hir::StructField) {
|
|
|
|
self.with_lint_attrs(s.id, &s.attrs, |builder| {
|
|
|
|
intravisit::walk_struct_field(builder, s);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_variant(&mut self,
|
|
|
|
v: &'tcx hir::Variant,
|
|
|
|
g: &'tcx hir::Generics,
|
|
|
|
item_id: ast::NodeId) {
|
|
|
|
self.with_lint_attrs(v.node.data.id(), &v.node.attrs, |builder| {
|
|
|
|
intravisit::walk_variant(builder, v, g, item_id);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_local(&mut self, l: &'tcx hir::Local) {
|
|
|
|
self.with_lint_attrs(l.id, &l.attrs, |builder| {
|
|
|
|
intravisit::walk_local(builder, l);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem) {
|
|
|
|
self.with_lint_attrs(trait_item.id, &trait_item.attrs, |builder| {
|
|
|
|
intravisit::walk_trait_item(builder, trait_item);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem) {
|
|
|
|
self.with_lint_attrs(impl_item.id, &impl_item.attrs, |builder| {
|
|
|
|
intravisit::walk_impl_item(builder, impl_item);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn provide(providers: &mut Providers) {
|
|
|
|
providers.lint_levels = lint_levels;
|
|
|
|
}
|
2018-04-07 10:20:36 +00:00
|
|
|
|
2018-07-17 17:23:03 +00:00
|
|
|
/// Returns whether `span` originates in a foreign crate's external macro.
|
|
|
|
///
|
|
|
|
/// This is used to test whether a lint should be entirely aborted above.
|
|
|
|
pub fn in_external_macro(sess: &Session, span: Span) -> bool {
|
|
|
|
let info = match span.ctxt().outer().expn_info() {
|
|
|
|
Some(info) => info,
|
|
|
|
// no ExpnInfo means this span doesn't come from a macro
|
|
|
|
None => return false,
|
|
|
|
};
|
|
|
|
|
|
|
|
match info.format {
|
|
|
|
ExpnFormat::MacroAttribute(..) => return true, // definitely a plugin
|
|
|
|
ExpnFormat::CompilerDesugaring(_) => return true, // well, it's "external"
|
|
|
|
ExpnFormat::MacroBang(..) => {} // check below
|
2018-04-07 10:20:36 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 17:23:03 +00:00
|
|
|
let def_site = match info.def_site {
|
|
|
|
Some(span) => span,
|
|
|
|
// no span for the def_site means it's an external macro
|
|
|
|
None => return true,
|
|
|
|
};
|
|
|
|
|
2018-08-18 10:14:09 +00:00
|
|
|
match sess.source_map().span_to_snippet(def_site) {
|
2018-07-17 17:23:03 +00:00
|
|
|
Ok(code) => !code.starts_with("macro_rules"),
|
|
|
|
// no snippet = external macro or compiler-builtin expansion
|
|
|
|
Err(_) => true,
|
|
|
|
}
|
2018-04-07 10:20:36 +00:00
|
|
|
}
|