2019-11-12 08:51:57 -05:00
|
|
|
pub use self::Level::*;
|
2020-01-05 09:40:16 +01:00
|
|
|
use crate::node_id::{NodeId, NodeMap};
|
2019-12-22 17:42:04 -05:00
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::edition::Edition;
|
2020-01-05 09:40:16 +01:00
|
|
|
use rustc_span::{sym, symbol::Ident, MultiSpan, Span, Symbol};
|
2019-11-12 08:51:57 -05:00
|
|
|
|
|
|
|
/// Setting for how to handle a lint.
|
|
|
|
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
|
|
|
|
pub enum Level {
|
2019-12-22 17:42:04 -05:00
|
|
|
Allow,
|
|
|
|
Warn,
|
|
|
|
Deny,
|
|
|
|
Forbid,
|
2019-11-12 08:51:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
rustc_data_structures::impl_stable_hash_via_hash!(Level);
|
|
|
|
|
|
|
|
impl Level {
|
|
|
|
/// Converts a level to a lower-case string.
|
|
|
|
pub fn as_str(self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
Level::Allow => "allow",
|
|
|
|
Level::Warn => "warn",
|
|
|
|
Level::Deny => "deny",
|
|
|
|
Level::Forbid => "forbid",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts a lower-case string to a level.
|
|
|
|
pub fn from_str(x: &str) -> Option<Level> {
|
|
|
|
match x {
|
|
|
|
"allow" => Some(Level::Allow),
|
|
|
|
"warn" => Some(Level::Warn),
|
|
|
|
"deny" => Some(Level::Deny),
|
|
|
|
"forbid" => Some(Level::Forbid),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts a symbol to a level.
|
|
|
|
pub fn from_symbol(x: Symbol) -> Option<Level> {
|
|
|
|
match x {
|
|
|
|
sym::allow => Some(Level::Allow),
|
|
|
|
sym::warn => Some(Level::Warn),
|
|
|
|
sym::deny => Some(Level::Deny),
|
|
|
|
sym::forbid => Some(Level::Forbid),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-12 11:52:26 -05:00
|
|
|
|
|
|
|
/// Specification of a single lint.
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub struct Lint {
|
|
|
|
/// A string identifier for the lint.
|
|
|
|
///
|
|
|
|
/// 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.
|
|
|
|
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,
|
|
|
|
|
|
|
|
/// 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)>,
|
|
|
|
|
|
|
|
/// `true` if this lint is reported even inside expansions of external macros.
|
|
|
|
pub report_in_external_macro: bool,
|
|
|
|
|
|
|
|
pub future_incompatible: Option<FutureIncompatibleInfo>,
|
|
|
|
|
|
|
|
pub is_plugin: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extra information for a future incompatibility lint.
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub struct FutureIncompatibleInfo {
|
|
|
|
/// e.g., a URL for an issue/PR/RFC or error code
|
|
|
|
pub reference: &'static str,
|
|
|
|
/// If this is an edition fixing lint, the edition in which
|
|
|
|
/// this lint becomes obsolete
|
|
|
|
pub edition: Option<Edition>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Lint {
|
|
|
|
pub const fn default_fields_for_macro() -> Self {
|
|
|
|
Lint {
|
|
|
|
name: "",
|
|
|
|
default_level: Level::Forbid,
|
|
|
|
desc: "",
|
|
|
|
edition_lint_opts: None,
|
|
|
|
is_plugin: false,
|
|
|
|
report_in_external_macro: false,
|
|
|
|
future_incompatible: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the lint's name, with ASCII letters converted to lowercase.
|
|
|
|
pub fn name_lower(&self) -> String {
|
|
|
|
self.name.to_ascii_lowercase()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn default_level(&self, edition: Edition) -> Level {
|
|
|
|
self.edition_lint_opts
|
|
|
|
.filter(|(e, _)| *e <= edition)
|
|
|
|
.map(|(_, l)| l)
|
|
|
|
.unwrap_or(self.default_level)
|
|
|
|
}
|
|
|
|
}
|
2019-11-12 12:09:20 -05:00
|
|
|
|
|
|
|
/// Identifies a lint known to the compiler.
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub struct LintId {
|
|
|
|
// Identity is based on pointer equality of this field.
|
|
|
|
pub lint: &'static Lint,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq for LintId {
|
|
|
|
fn eq(&self, other: &LintId) -> bool {
|
|
|
|
std::ptr::eq(self.lint, other.lint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
impl Eq for LintId {}
|
2019-11-12 12:09:20 -05:00
|
|
|
|
|
|
|
impl std::hash::Hash for LintId {
|
|
|
|
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
|
|
|
let ptr = self.lint as *const Lint;
|
|
|
|
ptr.hash(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintId {
|
|
|
|
/// Gets the `LintId` for a `Lint`.
|
|
|
|
pub fn of(lint: &'static Lint) -> LintId {
|
2019-12-22 17:42:04 -05:00
|
|
|
LintId { lint }
|
2019-11-12 12:09:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn lint_name_raw(&self) -> &'static str {
|
|
|
|
self.lint.name
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the name of the lint.
|
|
|
|
pub fn to_string(&self) -> String {
|
|
|
|
self.lint.name_lower()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<HCX> HashStable<HCX> for LintId {
|
|
|
|
#[inline]
|
|
|
|
fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
|
|
|
|
self.lint_name_raw().hash_stable(hcx, hasher);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<HCX> ToStableHashKey<HCX> for LintId {
|
|
|
|
type KeyType = &'static str;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn to_stable_hash_key(&self, _: &HCX) -> &'static str {
|
|
|
|
self.lint_name_raw()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-05 09:40:16 +01:00
|
|
|
// This could be a closure, but then implementing derive trait
|
|
|
|
// becomes hacky (and it gets allocated).
|
|
|
|
#[derive(PartialEq)]
|
|
|
|
pub enum BuiltinLintDiagnostics {
|
|
|
|
Normal,
|
|
|
|
BareTraitObject(Span, /* is_global */ bool),
|
|
|
|
AbsPathWithModule(Span),
|
|
|
|
ProcMacroDeriveResolutionFallback(Span),
|
|
|
|
MacroExpandedMacroExportsAccessedByAbsolutePaths(Span),
|
|
|
|
ElidedLifetimesInPaths(usize, Span, bool, Span, String),
|
|
|
|
UnknownCrateTypes(Span, String, String),
|
|
|
|
UnusedImports(String, Vec<(Span, String)>),
|
|
|
|
RedundantImport(Vec<(Span, bool)>, Ident),
|
|
|
|
DeprecatedMacro(Option<Symbol>, Span),
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Lints that are buffered up early on in the `Session` before the
|
|
|
|
/// `LintLevels` is calculated. These are later passed to `librustc`.
|
|
|
|
#[derive(PartialEq)]
|
2019-11-29 15:41:22 -05:00
|
|
|
pub struct BufferedEarlyLint {
|
|
|
|
/// The span of code that we are linting on.
|
2019-12-22 17:42:04 -05:00
|
|
|
pub span: MultiSpan,
|
2019-11-29 15:41:22 -05:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
/// The lint message.
|
|
|
|
pub msg: String,
|
2019-11-29 15:41:22 -05:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
/// The `NodeId` of the AST node that generated the lint.
|
2020-01-05 09:40:16 +01:00
|
|
|
pub node_id: NodeId,
|
2019-11-29 15:41:22 -05:00
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
/// A lint Id that can be passed to `rustc::lint::Lint::from_parser_lint_id`.
|
2020-01-05 09:40:16 +01:00
|
|
|
pub lint_id: LintId,
|
|
|
|
|
|
|
|
/// Customization of the `DiagnosticBuilder<'_>` for the lint.
|
|
|
|
pub diagnostic: BuiltinLintDiagnostics,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct LintBuffer {
|
|
|
|
pub map: NodeMap<Vec<BufferedEarlyLint>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintBuffer {
|
|
|
|
pub fn add_early_lint(&mut self, early_lint: BufferedEarlyLint) {
|
|
|
|
let arr = self.map.entry(early_lint.node_id).or_default();
|
|
|
|
if !arr.contains(&early_lint) {
|
|
|
|
arr.push(early_lint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_lint(
|
|
|
|
&mut self,
|
|
|
|
lint: &'static Lint,
|
|
|
|
node_id: NodeId,
|
|
|
|
span: MultiSpan,
|
|
|
|
msg: &str,
|
|
|
|
diagnostic: BuiltinLintDiagnostics,
|
|
|
|
) {
|
|
|
|
let lint_id = LintId::of(lint);
|
|
|
|
let msg = msg.to_string();
|
|
|
|
self.add_early_lint(BufferedEarlyLint { lint_id, node_id, span, msg, diagnostic });
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn take(&mut self, id: NodeId) -> Vec<BufferedEarlyLint> {
|
|
|
|
self.map.remove(&id).unwrap_or_default()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn buffer_lint(
|
|
|
|
&mut self,
|
|
|
|
lint: &'static Lint,
|
|
|
|
id: NodeId,
|
|
|
|
sp: impl Into<MultiSpan>,
|
|
|
|
msg: &str,
|
|
|
|
) {
|
|
|
|
self.add_lint(lint, id, sp.into(), msg, BuiltinLintDiagnostics::Normal)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn buffer_lint_with_diagnostic(
|
|
|
|
&mut self,
|
|
|
|
lint: &'static Lint,
|
|
|
|
id: NodeId,
|
|
|
|
sp: impl Into<MultiSpan>,
|
|
|
|
msg: &str,
|
|
|
|
diagnostic: BuiltinLintDiagnostics,
|
|
|
|
) {
|
|
|
|
self.add_lint(lint, id, sp.into(), msg, diagnostic)
|
|
|
|
}
|
2019-11-29 15:41:22 -05:00
|
|
|
}
|
|
|
|
|
2019-11-12 12:09:20 -05:00
|
|
|
/// Declares a static item of type `&'static Lint`.
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! declare_lint {
|
|
|
|
($vis: vis $NAME: ident, $Level: ident, $desc: expr) => (
|
|
|
|
$crate::declare_lint!(
|
|
|
|
$vis $NAME, $Level, $desc,
|
|
|
|
);
|
|
|
|
);
|
|
|
|
($vis: vis $NAME: ident, $Level: ident, $desc: expr,
|
|
|
|
$(@future_incompatible = $fi:expr;)? $($v:ident),*) => (
|
|
|
|
$vis static $NAME: &$crate::lint::Lint = &$crate::lint::Lint {
|
|
|
|
name: stringify!($NAME),
|
|
|
|
default_level: $crate::lint::$Level,
|
|
|
|
desc: $desc,
|
|
|
|
edition_lint_opts: None,
|
|
|
|
is_plugin: false,
|
|
|
|
$($v: true,)*
|
|
|
|
$(future_incompatible: Some($fi),)*
|
|
|
|
..$crate::lint::Lint::default_fields_for_macro()
|
|
|
|
};
|
|
|
|
);
|
|
|
|
($vis: vis $NAME: ident, $Level: ident, $desc: expr,
|
|
|
|
$lint_edition: expr => $edition_level: ident
|
|
|
|
) => (
|
|
|
|
$vis static $NAME: &$crate::lint::Lint = &$crate::lint::Lint {
|
|
|
|
name: stringify!($NAME),
|
|
|
|
default_level: $crate::lint::$Level,
|
|
|
|
desc: $desc,
|
|
|
|
edition_lint_opts: Some(($lint_edition, $crate::lint::Level::$edition_level)),
|
|
|
|
report_in_external_macro: false,
|
|
|
|
is_plugin: false,
|
|
|
|
};
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! declare_tool_lint {
|
|
|
|
(
|
|
|
|
$(#[$attr:meta])* $vis:vis $tool:ident ::$NAME:ident, $Level: ident, $desc: expr
|
|
|
|
) => (
|
|
|
|
$crate::declare_tool_lint!{$(#[$attr])* $vis $tool::$NAME, $Level, $desc, false}
|
|
|
|
);
|
|
|
|
(
|
|
|
|
$(#[$attr:meta])* $vis:vis $tool:ident ::$NAME:ident, $Level:ident, $desc:expr,
|
|
|
|
report_in_external_macro: $rep:expr
|
|
|
|
) => (
|
|
|
|
$crate::declare_tool_lint!{$(#[$attr])* $vis $tool::$NAME, $Level, $desc, $rep}
|
|
|
|
);
|
|
|
|
(
|
|
|
|
$(#[$attr:meta])* $vis:vis $tool:ident ::$NAME:ident, $Level:ident, $desc:expr,
|
|
|
|
$external:expr
|
|
|
|
) => (
|
|
|
|
$(#[$attr])*
|
|
|
|
$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,
|
|
|
|
future_incompatible: None,
|
|
|
|
is_plugin: true,
|
|
|
|
};
|
|
|
|
);
|
|
|
|
}
|