2021-08-06 23:18:16 +02:00
|
|
|
#![feature(min_specialization)]
|
2022-08-18 19:27:29 +01:00
|
|
|
#![deny(rustc::untranslatable_diagnostic)]
|
|
|
|
#![deny(rustc::diagnostic_outside_of_impl)]
|
2021-08-06 23:18:16 +02:00
|
|
|
|
2020-08-13 15:41:52 -04:00
|
|
|
#[macro_use]
|
|
|
|
extern crate rustc_macros;
|
|
|
|
|
2019-11-12 08:51:57 -05:00
|
|
|
pub use self::Level::*;
|
2023-01-17 12:05:01 +01:00
|
|
|
use rustc_ast::node_id::NodeId;
|
2021-11-24 21:57:38 +01:00
|
|
|
use rustc_ast::{AttrId, Attribute};
|
2023-03-06 10:56:23 +00:00
|
|
|
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
|
2019-11-12 12:09:20 -05:00
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
|
2022-08-18 15:23:00 +09:00
|
|
|
use rustc_error_messages::{DiagnosticMessage, MultiSpan};
|
2022-04-19 10:43:09 +02:00
|
|
|
use rustc_hir::HashStableContext;
|
2021-11-20 20:45:27 +01:00
|
|
|
use rustc_hir::HirId;
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::edition::Edition;
|
2022-03-24 02:03:04 +00:00
|
|
|
use rustc_span::{sym, symbol::Ident, Span, Symbol};
|
2020-09-01 17:12:38 -04:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2019-11-12 08:51:57 -05:00
|
|
|
|
2021-06-03 21:14:15 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2020-01-05 10:35:40 +01:00
|
|
|
pub mod builtin;
|
|
|
|
|
2020-08-13 15:41:52 -04:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! pluralize {
|
|
|
|
($x:expr) => {
|
|
|
|
if $x != 1 { "s" } else { "" }
|
|
|
|
};
|
2022-08-15 14:11:11 -05:00
|
|
|
("has", $x:expr) => {
|
|
|
|
if $x == 1 { "has" } else { "have" }
|
|
|
|
};
|
2022-03-14 17:55:14 +01:00
|
|
|
("is", $x:expr) => {
|
|
|
|
if $x == 1 { "is" } else { "are" }
|
|
|
|
};
|
2022-06-22 14:56:40 +09:00
|
|
|
("was", $x:expr) => {
|
|
|
|
if $x == 1 { "was" } else { "were" }
|
|
|
|
};
|
2022-03-14 17:55:14 +01:00
|
|
|
("this", $x:expr) => {
|
|
|
|
if $x == 1 { "this" } else { "these" }
|
|
|
|
};
|
2020-08-13 15:41:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Indicates the confidence in the correctness of a suggestion.
|
|
|
|
///
|
|
|
|
/// All suggestions are marked with an `Applicability`. Tools use the applicability of a suggestion
|
|
|
|
/// to determine whether it should be automatically applied or if the user should be consulted
|
|
|
|
/// before applying the suggestion.
|
2022-08-28 01:08:24 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Hash, Encodable, Decodable, Serialize, Deserialize)]
|
|
|
|
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
2020-08-13 15:41:52 -04:00
|
|
|
pub enum Applicability {
|
2021-05-31 13:24:16 +02:00
|
|
|
/// The suggestion is definitely what the user intended, or maintains the exact meaning of the code.
|
|
|
|
/// This suggestion should be automatically applied.
|
|
|
|
///
|
|
|
|
/// In case of multiple `MachineApplicable` suggestions (whether as part of
|
|
|
|
/// the same `multipart_suggestion` or not), all of them should be
|
2020-08-13 15:41:52 -04:00
|
|
|
/// automatically applied.
|
|
|
|
MachineApplicable,
|
|
|
|
|
|
|
|
/// The suggestion may be what the user intended, but it is uncertain. The suggestion should
|
|
|
|
/// result in valid Rust code if it is applied.
|
|
|
|
MaybeIncorrect,
|
|
|
|
|
|
|
|
/// The suggestion contains placeholders like `(...)` or `{ /* fields */ }`. The suggestion
|
|
|
|
/// cannot be applied automatically because it will not result in valid Rust code. The user
|
|
|
|
/// will need to fill in the placeholders.
|
|
|
|
HasPlaceholders,
|
|
|
|
|
|
|
|
/// The applicability of the suggestion is unknown.
|
|
|
|
Unspecified,
|
|
|
|
}
|
|
|
|
|
2021-11-25 10:45:25 +01:00
|
|
|
/// Each lint expectation has a `LintExpectationId` assigned by the `LintLevelsBuilder`.
|
|
|
|
/// Expected `Diagnostic`s get the lint level `Expect` which stores the `LintExpectationId`
|
|
|
|
/// to match it with the actual expectation later on.
|
2021-11-20 20:45:27 +01:00
|
|
|
///
|
2022-03-06 11:47:08 +01:00
|
|
|
/// The `LintExpectationId` has to be stable between compilations, as diagnostic
|
2021-11-20 20:45:27 +01:00
|
|
|
/// instances might be loaded from cache. Lint messages can be emitted during an
|
|
|
|
/// `EarlyLintPass` operating on the AST and during a `LateLintPass` traversing the
|
|
|
|
/// HIR tree. The AST doesn't have enough information to create a stable id. The
|
|
|
|
/// `LintExpectationId` will instead store the [`AttrId`] defining the expectation.
|
|
|
|
/// These `LintExpectationId` will be updated to use the stable [`HirId`] once the
|
2021-11-25 10:45:25 +01:00
|
|
|
/// AST has been lowered. The transformation is done by the `LintLevelsBuilder`
|
2021-11-24 21:57:38 +01:00
|
|
|
///
|
|
|
|
/// Each lint inside the `expect` attribute is tracked individually, the `lint_index`
|
|
|
|
/// identifies the lint inside the attribute and ensures that the IDs are unique.
|
2021-11-24 23:21:10 +01:00
|
|
|
///
|
|
|
|
/// The index values have a type of `u16` to reduce the size of the `LintExpectationId`.
|
|
|
|
/// It's reasonable to assume that no user will define 2^16 attributes on one node or
|
|
|
|
/// have that amount of lints listed. `u16` values should therefore suffice.
|
2021-11-20 20:45:27 +01:00
|
|
|
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash, Encodable, Decodable)]
|
|
|
|
pub enum LintExpectationId {
|
|
|
|
/// Used for lints emitted during the `EarlyLintPass`. This id is not
|
2022-03-06 11:47:08 +01:00
|
|
|
/// hash stable and should not be cached.
|
2021-11-24 23:21:10 +01:00
|
|
|
Unstable { attr_id: AttrId, lint_index: Option<u16> },
|
2021-11-20 20:45:27 +01:00
|
|
|
/// The [`HirId`] that the lint expectation is attached to. This id is
|
|
|
|
/// stable and can be cached. The additional index ensures that nodes with
|
|
|
|
/// several expectations can correctly match diagnostics to the individual
|
|
|
|
/// expectation.
|
2022-07-22 16:48:36 +00:00
|
|
|
Stable { hir_id: HirId, attr_index: u16, lint_index: Option<u16>, attr_id: Option<AttrId> },
|
2021-11-20 20:45:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LintExpectationId {
|
|
|
|
pub fn is_stable(&self) -> bool {
|
|
|
|
match self {
|
2021-11-24 21:57:38 +01:00
|
|
|
LintExpectationId::Unstable { .. } => false,
|
2021-11-20 20:45:27 +01:00
|
|
|
LintExpectationId::Stable { .. } => true,
|
|
|
|
}
|
2021-08-06 23:18:16 +02:00
|
|
|
}
|
2021-11-24 21:57:38 +01:00
|
|
|
|
2021-11-24 23:21:10 +01:00
|
|
|
pub fn get_lint_index(&self) -> Option<u16> {
|
2021-11-24 21:57:38 +01:00
|
|
|
let (LintExpectationId::Unstable { lint_index, .. }
|
|
|
|
| LintExpectationId::Stable { lint_index, .. }) = self;
|
|
|
|
|
|
|
|
*lint_index
|
|
|
|
}
|
|
|
|
|
2021-11-24 23:21:10 +01:00
|
|
|
pub fn set_lint_index(&mut self, new_lint_index: Option<u16>) {
|
2021-11-24 21:57:38 +01:00
|
|
|
let (LintExpectationId::Unstable { ref mut lint_index, .. }
|
|
|
|
| LintExpectationId::Stable { ref mut lint_index, .. }) = self;
|
|
|
|
|
|
|
|
*lint_index = new_lint_index
|
|
|
|
}
|
2022-07-22 16:48:36 +00:00
|
|
|
|
|
|
|
/// Prepares the id for hashing. Removes references to the ast.
|
|
|
|
/// Should only be called when the id is stable.
|
|
|
|
pub fn normalize(self) -> Self {
|
|
|
|
match self {
|
|
|
|
Self::Stable { hir_id, attr_index, lint_index, .. } => {
|
|
|
|
Self::Stable { hir_id, attr_index, lint_index, attr_id: None }
|
|
|
|
}
|
|
|
|
Self::Unstable { .. } => {
|
|
|
|
unreachable!("`normalize` called when `ExpectationId` is unstable")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-06 23:18:16 +02:00
|
|
|
}
|
|
|
|
|
2021-11-20 20:45:27 +01:00
|
|
|
impl<HCX: rustc_hir::HashStableContext> HashStable<HCX> for LintExpectationId {
|
|
|
|
#[inline]
|
|
|
|
fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
|
|
|
|
match self {
|
2022-07-22 16:48:36 +00:00
|
|
|
LintExpectationId::Stable {
|
|
|
|
hir_id,
|
|
|
|
attr_index,
|
|
|
|
lint_index: Some(lint_index),
|
|
|
|
attr_id: _,
|
|
|
|
} => {
|
2021-11-20 20:45:27 +01:00
|
|
|
hir_id.hash_stable(hcx, hasher);
|
|
|
|
attr_index.hash_stable(hcx, hasher);
|
2021-11-24 21:57:38 +01:00
|
|
|
lint_index.hash_stable(hcx, hasher);
|
|
|
|
}
|
|
|
|
_ => {
|
2022-03-06 11:47:08 +01:00
|
|
|
unreachable!(
|
|
|
|
"HashStable should only be called for filled and stable `LintExpectationId`"
|
|
|
|
)
|
2021-11-20 20:45:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-06 23:18:16 +02:00
|
|
|
|
2021-11-20 20:45:27 +01:00
|
|
|
impl<HCX: rustc_hir::HashStableContext> ToStableHashKey<HCX> for LintExpectationId {
|
2021-11-24 23:21:10 +01:00
|
|
|
type KeyType = (HirId, u16, u16);
|
2021-08-06 23:18:16 +02:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn to_stable_hash_key(&self, _: &HCX) -> Self::KeyType {
|
2021-11-20 20:45:27 +01:00
|
|
|
match self {
|
2022-07-22 16:48:36 +00:00
|
|
|
LintExpectationId::Stable {
|
|
|
|
hir_id,
|
|
|
|
attr_index,
|
|
|
|
lint_index: Some(lint_index),
|
|
|
|
attr_id: _,
|
|
|
|
} => (*hir_id, *attr_index, *lint_index),
|
2021-11-24 21:57:38 +01:00
|
|
|
_ => {
|
|
|
|
unreachable!("HashStable should only be called for a filled `LintExpectationId`")
|
2021-11-20 20:45:27 +01:00
|
|
|
}
|
|
|
|
}
|
2021-08-06 23:18:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 08:51:57 -05:00
|
|
|
/// Setting for how to handle a lint.
|
2021-08-06 23:18:16 +02:00
|
|
|
///
|
2021-11-25 10:45:25 +01:00
|
|
|
/// See: <https://doc.rust-lang.org/rustc/lints/levels.html>
|
2022-04-19 10:43:09 +02:00
|
|
|
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash, HashStable_Generic)]
|
2019-11-12 08:51:57 -05:00
|
|
|
pub enum Level {
|
2021-08-06 23:18:16 +02:00
|
|
|
/// The `allow` level will not issue any message.
|
2019-11-12 08:51:57 -05:00
|
|
|
Allow,
|
2022-02-18 12:00:16 +01:00
|
|
|
/// The `expect` level will suppress the lint message but in turn produce a message
|
2021-08-06 23:18:16 +02:00
|
|
|
/// if the lint wasn't issued in the expected scope. `Expect` should not be used as
|
|
|
|
/// an initial level for a lint.
|
|
|
|
///
|
|
|
|
/// Note that this still means that the lint is enabled in this position and should
|
2022-02-18 12:00:16 +01:00
|
|
|
/// be emitted, this will in turn fulfill the expectation and suppress the lint.
|
2021-08-06 23:18:16 +02:00
|
|
|
///
|
|
|
|
/// See RFC 2383.
|
|
|
|
///
|
2022-06-05 12:33:45 +02:00
|
|
|
/// The [`LintExpectationId`] is used to later link a lint emission to the actual
|
2021-08-06 23:18:16 +02:00
|
|
|
/// expectation. It can be ignored in most cases.
|
|
|
|
Expect(LintExpectationId),
|
|
|
|
/// The `warn` level will produce a warning if the lint was violated, however the
|
|
|
|
/// compiler will continue with its execution.
|
2019-11-12 08:51:57 -05:00
|
|
|
Warn,
|
2022-06-05 12:33:45 +02:00
|
|
|
/// This lint level is a special case of [`Warn`], that can't be overridden. This is used
|
|
|
|
/// to ensure that a lint can't be suppressed. This lint level can currently only be set
|
|
|
|
/// via the console and is therefore session specific.
|
|
|
|
///
|
|
|
|
/// The [`LintExpectationId`] is intended to fulfill expectations marked via the
|
|
|
|
/// `#[expect]` attribute, that will still be suppressed due to the level.
|
|
|
|
ForceWarn(Option<LintExpectationId>),
|
2021-08-06 23:18:16 +02:00
|
|
|
/// The `deny` level will produce an error and stop further execution after the lint
|
|
|
|
/// pass is complete.
|
2019-11-12 08:51:57 -05:00
|
|
|
Deny,
|
2021-08-06 23:18:16 +02:00
|
|
|
/// `Forbid` is equivalent to the `deny` level but can't be overwritten like the previous
|
|
|
|
/// levels.
|
2019-11-12 08:51:57 -05:00
|
|
|
Forbid,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Level {
|
|
|
|
/// Converts a level to a lower-case string.
|
|
|
|
pub fn as_str(self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
Level::Allow => "allow",
|
2021-08-06 23:18:16 +02:00
|
|
|
Level::Expect(_) => "expect",
|
2019-11-12 08:51:57 -05:00
|
|
|
Level::Warn => "warn",
|
2022-06-05 12:33:45 +02:00
|
|
|
Level::ForceWarn(_) => "force-warn",
|
2019-11-12 08:51:57 -05:00
|
|
|
Level::Deny => "deny",
|
|
|
|
Level::Forbid => "forbid",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-06 23:18:16 +02:00
|
|
|
/// Converts a lower-case string to a level. This will never construct the expect
|
|
|
|
/// level as that would require a [`LintExpectationId`]
|
2019-11-12 08:51:57 -05:00
|
|
|
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),
|
2021-08-06 23:18:16 +02:00
|
|
|
"expect" | _ => None,
|
2019-11-12 08:51:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Converts a symbol to a level.
|
2021-11-24 21:57:38 +01:00
|
|
|
pub fn from_attr(attr: &Attribute) -> Option<Level> {
|
|
|
|
match attr.name_or_empty() {
|
2019-11-12 08:51:57 -05:00
|
|
|
sym::allow => Some(Level::Allow),
|
2021-11-24 21:57:38 +01:00
|
|
|
sym::expect => Some(Level::Expect(LintExpectationId::Unstable {
|
|
|
|
attr_id: attr.id,
|
|
|
|
lint_index: None,
|
|
|
|
})),
|
2019-11-12 08:51:57 -05:00
|
|
|
sym::warn => Some(Level::Warn),
|
|
|
|
sym::deny => Some(Level::Deny),
|
|
|
|
sym::forbid => Some(Level::Forbid),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2022-04-15 01:24:49 -07:00
|
|
|
|
2023-01-10 10:56:17 +03:00
|
|
|
pub fn to_cmd_flag(self) -> &'static str {
|
|
|
|
match self {
|
|
|
|
Level::Warn => "-W",
|
|
|
|
Level::Deny => "-D",
|
|
|
|
Level::Forbid => "-F",
|
|
|
|
Level::Allow => "-A",
|
|
|
|
Level::ForceWarn(_) => "--force-warn",
|
|
|
|
Level::Expect(_) => {
|
|
|
|
unreachable!("the expect level does not have a commandline flag")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 01:24:49 -07:00
|
|
|
pub fn is_error(self) -> bool {
|
|
|
|
match self {
|
2022-06-05 12:33:45 +02:00
|
|
|
Level::Allow | Level::Expect(_) | Level::Warn | Level::ForceWarn(_) => false,
|
2022-04-15 01:24:49 -07:00
|
|
|
Level::Deny | Level::Forbid => true,
|
|
|
|
}
|
|
|
|
}
|
2022-06-16 13:20:57 +02:00
|
|
|
|
|
|
|
pub fn get_expectation_id(&self) -> Option<LintExpectationId> {
|
|
|
|
match self {
|
|
|
|
Level::Expect(id) | Level::ForceWarn(Some(id)) => Some(*id),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2019-11-12 08:51:57 -05:00
|
|
|
}
|
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.
|
2020-09-08 15:09:57 -07:00
|
|
|
///
|
2020-11-05 10:23:39 +01:00
|
|
|
/// See <https://rustc-dev-guide.rust-lang.org/diagnostics.html#lint-naming>
|
2020-09-08 15:09:57 -07:00
|
|
|
/// for naming guidelines.
|
2019-11-12 11:52:26 -05:00
|
|
|
pub name: &'static str,
|
|
|
|
|
|
|
|
/// Default level for the lint.
|
2020-09-08 15:09:57 -07:00
|
|
|
///
|
2020-11-05 10:23:39 +01:00
|
|
|
/// See <https://rustc-dev-guide.rust-lang.org/diagnostics.html#diagnostic-levels>
|
2020-09-08 15:09:57 -07:00
|
|
|
/// for guidelines on choosing a default level.
|
2019-11-12 11:52:26 -05:00
|
|
|
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,
|
2020-06-03 19:16:29 -07:00
|
|
|
|
|
|
|
/// `Some` if this lint is feature gated, otherwise `None`.
|
|
|
|
pub feature_gate: Option<Symbol>,
|
2020-06-13 09:58:24 +08:00
|
|
|
|
|
|
|
pub crate_level_only: bool,
|
2019-11-12 11:52:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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,
|
2021-06-15 17:16:21 +02:00
|
|
|
/// The reason for the lint used by diagnostics to provide
|
|
|
|
/// the right help message
|
|
|
|
pub reason: FutureIncompatibilityReason,
|
2021-06-27 14:45:54 +00:00
|
|
|
/// Whether to explain the reason to the user.
|
|
|
|
///
|
|
|
|
/// Set to false for lints that already include a more detailed
|
|
|
|
/// explanation.
|
|
|
|
pub explain_reason: bool,
|
2021-06-15 17:16:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The reason for future incompatibility
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub enum FutureIncompatibilityReason {
|
2021-06-16 14:27:44 +02:00
|
|
|
/// This will be an error in a future release
|
|
|
|
/// for all editions
|
|
|
|
FutureReleaseError,
|
2021-07-11 13:08:58 -07:00
|
|
|
/// This will be an error in a future release, and
|
|
|
|
/// Cargo should create a report even for dependencies
|
|
|
|
FutureReleaseErrorReportNow,
|
2022-01-27 10:49:32 +01:00
|
|
|
/// Code that changes meaning in some way in a
|
|
|
|
/// future release.
|
|
|
|
FutureReleaseSemanticsChange,
|
2021-06-15 17:16:21 +02:00
|
|
|
/// Previously accepted code that will become an
|
|
|
|
/// error in the provided edition
|
|
|
|
EditionError(Edition),
|
|
|
|
/// Code that changes meaning in some way in
|
|
|
|
/// the provided edition
|
|
|
|
EditionSemanticsChange(Edition),
|
2022-01-27 10:49:32 +01:00
|
|
|
/// A custom reason.
|
|
|
|
Custom(&'static str),
|
2021-06-15 17:16:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FutureIncompatibilityReason {
|
|
|
|
pub fn edition(self) -> Option<Edition> {
|
|
|
|
match self {
|
|
|
|
Self::EditionError(e) => Some(e),
|
|
|
|
Self::EditionSemanticsChange(e) => Some(e),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2020-08-13 15:41:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl FutureIncompatibleInfo {
|
|
|
|
pub const fn default_fields_for_macro() -> Self {
|
2021-06-15 17:16:21 +02:00
|
|
|
FutureIncompatibleInfo {
|
|
|
|
reference: "",
|
2021-06-16 14:27:44 +02:00
|
|
|
reason: FutureIncompatibilityReason::FutureReleaseError,
|
2021-06-27 14:45:54 +00:00
|
|
|
explain_reason: true,
|
2021-06-15 17:16:21 +02:00
|
|
|
}
|
2020-08-13 15:41:52 -04:00
|
|
|
}
|
2019-11-12 11:52:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2020-06-03 19:16:29 -07:00
|
|
|
feature_gate: None,
|
2020-06-13 09:58:24 +08:00
|
|
|
crate_level_only: false,
|
2019-11-12 11:52:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Eq for LintId {}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
LintId { lint }
|
|
|
|
}
|
|
|
|
|
|
|
|
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).
|
2022-01-23 17:05:48 -05:00
|
|
|
#[derive(Debug)]
|
2020-01-05 09:40:16 +01:00
|
|
|
pub enum BuiltinLintDiagnostics {
|
|
|
|
Normal,
|
|
|
|
AbsPathWithModule(Span),
|
|
|
|
ProcMacroDeriveResolutionFallback(Span),
|
|
|
|
MacroExpandedMacroExportsAccessedByAbsolutePaths(Span),
|
2022-03-10 23:12:35 +01:00
|
|
|
ElidedLifetimesInPaths(usize, Span, bool, Span),
|
2020-01-05 09:40:16 +01:00
|
|
|
UnknownCrateTypes(Span, String, String),
|
2021-10-23 18:55:02 +09:00
|
|
|
UnusedImports(String, Vec<(Span, String)>, Option<Span>),
|
2020-01-05 09:40:16 +01:00
|
|
|
RedundantImport(Vec<(Span, bool)>, Ident),
|
|
|
|
DeprecatedMacro(Option<Symbol>, Span),
|
2020-09-01 17:12:38 -04:00
|
|
|
MissingAbi(Span, Abi),
|
2020-02-21 16:01:48 -08:00
|
|
|
UnusedDocComment(Span),
|
2022-05-10 21:15:30 +02:00
|
|
|
UnusedBuiltinAttribute {
|
|
|
|
attr_name: Symbol,
|
|
|
|
macro_name: String,
|
|
|
|
invoc_span: Span,
|
|
|
|
},
|
2020-11-30 23:24:08 +09:00
|
|
|
PatternsInFnsWithoutBody(Span, Ident),
|
2020-11-14 14:47:14 +03:00
|
|
|
LegacyDeriveHelpers(Span),
|
2021-03-14 16:55:59 -04:00
|
|
|
ProcMacroBackCompat(String),
|
2021-03-25 21:42:21 +08:00
|
|
|
OrPatternsBackCompat(Span, String),
|
2021-06-14 12:56:49 +00:00
|
|
|
ReservedPrefix(Span),
|
Display an extra note for trailing semicolon lint with trailing macro
Currently, we parse macros at the end of a block
(e.g. `fn foo() { my_macro!() }`) as expressions, rather than
statements. This means that a macro invoked in this position
cannot expand to items or semicolon-terminated expressions.
In the future, we might want to start parsing these kinds of macros
as statements. This would make expansion more 'token-based'
(i.e. macro expansion behaves (almost) as if you just textually
replaced the macro invocation with its output). However,
this is a breaking change (see PR #78991), so it will require
further discussion.
Since the current behavior will not be changing any time soon,
we need to address the interaction with the
`SEMICOLON_IN_EXPRESSIONS_FROM_MACROS` lint. Since we are parsing
the result of macro expansion as an expression, we will emit a lint
if there's a trailing semicolon in the macro output. However, this
results in a somewhat confusing message for users, since it visually
looks like there should be no problem with having a semicolon
at the end of a block
(e.g. `fn foo() { my_macro!() }` => `fn foo() { produced_expr; }`)
To help reduce confusion, this commit adds a note explaining
that the macro is being interpreted as an expression. Additionally,
we suggest adding a semicolon after the macro *invocation* - this
will cause us to parse the macro call as a statement. We do *not*
use a structured suggestion for this, since the user may actually
want to remove the semicolon from the macro definition (allowing
the block to evaluate to the expression produced by the macro).
2021-07-22 11:24:42 -05:00
|
|
|
TrailingMacro(bool, Ident),
|
2021-07-10 16:38:55 +02:00
|
|
|
BreakWithLabelAndLoop(Span),
|
2021-07-20 16:35:26 -04:00
|
|
|
NamedAsmLabel(String),
|
2021-08-19 11:40:00 -07:00
|
|
|
UnicodeTextFlow(Span, String),
|
2023-04-30 19:09:15 +02:00
|
|
|
UnexpectedCfgName((Symbol, Span), Option<(Symbol, Span)>),
|
|
|
|
UnexpectedCfgValue((Symbol, Span), Option<(Symbol, Span)>),
|
2022-02-07 01:23:37 -05:00
|
|
|
DeprecatedWhereclauseLocation(Span, String),
|
2022-05-10 21:15:30 +02:00
|
|
|
SingleUseLifetime {
|
|
|
|
/// Span of the parameter which declares this lifetime.
|
|
|
|
param_span: Span,
|
|
|
|
/// Span of the code that should be removed when eliding this lifetime.
|
|
|
|
/// This span should include leading or trailing comma.
|
2023-01-16 21:06:34 +09:00
|
|
|
deletion_span: Option<Span>,
|
2022-05-10 21:15:30 +02:00
|
|
|
/// Span of the single use, or None if the lifetime is never used.
|
|
|
|
/// If true, the lifetime will be fully elided.
|
|
|
|
use_span: Option<(Span, bool)>,
|
|
|
|
},
|
2022-07-29 22:52:46 -06:00
|
|
|
NamedArgumentUsedPositionally {
|
|
|
|
/// Span where the named argument is used by position and will be replaced with the named
|
|
|
|
/// argument name
|
|
|
|
position_sp_to_replace: Option<Span>,
|
|
|
|
/// Span where the named argument is used by position and is used for lint messages
|
|
|
|
position_sp_for_msg: Option<Span>,
|
|
|
|
/// Span where the named argument's name is (so we know where to put the warning message)
|
|
|
|
named_arg_sp: Span,
|
|
|
|
/// String containing the named arguments name
|
|
|
|
named_arg_name: String,
|
|
|
|
/// Indicates if the named argument is used as a width/precision for formatting
|
|
|
|
is_formatting_arg: bool,
|
|
|
|
},
|
Allow more deriving on packed structs.
Currently, deriving on packed structs has some non-trivial limitations,
related to the fact that taking references on unaligned fields is UB.
The current approach to field accesses in derived code:
- Normal case: `&self.0`
- In a packed struct that derives `Copy`: `&{self.0}`
- In a packed struct that doesn't derive `Copy`: `&self.0`
Plus, we disallow deriving any builtin traits other than `Default` for any
packed generic type, because it's possible that there might be
misaligned fields. This is a fairly broad restriction.
Plus, we disallow deriving any builtin traits other than `Default` for most
packed types that don't derive `Copy`. (The exceptions are those where the
alignments inherently satisfy the packing, e.g. in a type with
`repr(packed(N))` where all the fields have alignments of `N` or less
anyway. Such types are pretty strange, because the `packed` attribute is
not having any effect.)
This commit introduces a new, simpler approach to field accesses:
- Normal case: `&self.0`
- In a packed struct: `&{self.0}`
In the latter case, this requires that all fields impl `Copy`, which is
a new restriction. This means that the following example compiles under
the old approach and doesn't compile under the new approach.
```
#[derive(Debug)]
struct NonCopy(u8);
#[derive(Debug)
#[repr(packed)]
struct MyType(NonCopy);
```
(Note that the old approach's support for cases like this was brittle.
Changing the `u8` to a `u16` would be enough to stop it working. So not
much capability is lost here.)
However, the other constraints from the old rules are removed. We can now
derive builtin traits for packed generic structs like this:
```
trait Trait { type A; }
#[derive(Hash)]
#[repr(packed)]
pub struct Foo<T: Trait>(T, T::A);
```
To allow this, we add a `T: Copy` bound in the derived impl and a `T::A:
Copy` bound in where clauses. So `T` and `T::A` must impl `Copy`.
We can now also derive builtin traits for packed structs that don't derive
`Copy`, so long as the fields impl `Copy`:
```
#[derive(Hash)]
#[repr(packed)]
pub struct Foo(u32);
```
This includes types that hand-impl `Copy` rather than deriving it, such as the
following, that show up in winapi-0.2:
```
#[derive(Clone)]
#[repr(packed)]
struct MyType(i32);
impl Copy for MyType {}
```
The new approach is simpler to understand and implement, and it avoids
the need for the `unsafe_derive_on_repr_packed` check.
One exception is required for backwards-compatibility: we allow `[u8]`
fields for now. There is a new lint for this,
`byte_slice_in_packed_struct_with_derive`.
2022-11-21 14:40:32 +11:00
|
|
|
ByteSliceInPackedStructWithDerive,
|
2023-02-22 20:25:10 +00:00
|
|
|
UnusedExternCrate {
|
|
|
|
removal_span: Span,
|
|
|
|
},
|
|
|
|
ExternCrateNotIdiomatic {
|
|
|
|
vis_span: Span,
|
|
|
|
ident_span: Span,
|
|
|
|
},
|
2023-03-20 03:11:28 +08:00
|
|
|
AmbiguousGlobReexports {
|
|
|
|
/// The name for which collision(s) have occurred.
|
|
|
|
name: String,
|
2023-04-10 22:02:52 +02:00
|
|
|
/// The name space for which the collision(s) occurred in.
|
2023-03-20 03:11:28 +08:00
|
|
|
namespace: String,
|
|
|
|
/// Span where the name is first re-exported.
|
|
|
|
first_reexport_span: Span,
|
|
|
|
/// Span where the same name is also re-exported.
|
|
|
|
duplicate_reexport_span: Span,
|
|
|
|
},
|
2020-01-05 09:40:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Lints that are buffered up early on in the `Session` before the
|
2020-04-03 19:03:13 +09:00
|
|
|
/// `LintLevels` is calculated.
|
2023-03-06 10:56:53 +00:00
|
|
|
#[derive(Debug)]
|
2019-11-29 15:41:22 -05:00
|
|
|
pub struct BufferedEarlyLint {
|
|
|
|
/// The span of code that we are linting on.
|
|
|
|
pub span: MultiSpan,
|
|
|
|
|
|
|
|
/// The lint message.
|
2022-08-17 23:52:16 +09:00
|
|
|
pub msg: DiagnosticMessage,
|
2019-11-29 15:41:22 -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
|
|
|
|
2020-04-03 19:03:13 +09:00
|
|
|
/// A lint Id that can be passed to
|
|
|
|
/// `rustc_lint::early::EarlyContextAndPass::check_id`.
|
2020-01-05 09:40:16 +01:00
|
|
|
pub lint_id: LintId,
|
|
|
|
|
|
|
|
/// Customization of the `DiagnosticBuilder<'_>` for the lint.
|
|
|
|
pub diagnostic: BuiltinLintDiagnostics,
|
|
|
|
}
|
|
|
|
|
2023-03-06 10:56:53 +00:00
|
|
|
#[derive(Default, Debug)]
|
2020-01-05 09:40:16 +01:00
|
|
|
pub struct LintBuffer {
|
2023-01-17 12:05:01 +01:00
|
|
|
pub map: FxIndexMap<NodeId, Vec<BufferedEarlyLint>>,
|
2020-01-05 09:40:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl LintBuffer {
|
|
|
|
pub fn add_early_lint(&mut self, early_lint: BufferedEarlyLint) {
|
|
|
|
let arr = self.map.entry(early_lint.node_id).or_default();
|
2022-01-23 17:05:48 -05:00
|
|
|
arr.push(early_lint);
|
2020-01-05 09:40:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_lint(
|
|
|
|
&mut self,
|
|
|
|
lint: &'static Lint,
|
|
|
|
node_id: NodeId,
|
|
|
|
span: MultiSpan,
|
2022-08-17 23:52:16 +09:00
|
|
|
msg: impl Into<DiagnosticMessage>,
|
2020-01-05 09:40:16 +01:00
|
|
|
diagnostic: BuiltinLintDiagnostics,
|
|
|
|
) {
|
|
|
|
let lint_id = LintId::of(lint);
|
2022-08-17 23:52:16 +09:00
|
|
|
let msg = msg.into();
|
2020-01-05 09:40:16 +01:00
|
|
|
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>,
|
2022-08-18 04:36:57 +09:00
|
|
|
msg: impl Into<DiagnosticMessage>,
|
2020-01-05 09:40:16 +01:00
|
|
|
) {
|
|
|
|
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>,
|
2022-08-18 04:36:57 +09:00
|
|
|
msg: impl Into<DiagnosticMessage>,
|
2020-01-05 09:40:16 +01:00
|
|
|
diagnostic: BuiltinLintDiagnostics,
|
|
|
|
) {
|
|
|
|
self.add_lint(lint, id, sp.into(), msg, diagnostic)
|
|
|
|
}
|
2019-11-29 15:41:22 -05:00
|
|
|
}
|
|
|
|
|
2023-03-06 10:56:23 +00:00
|
|
|
pub type RegisteredTools = FxIndexSet<Ident>;
|
|
|
|
|
2019-11-12 12:09:20 -05:00
|
|
|
/// Declares a static item of type `&'static Lint`.
|
2020-09-08 15:09:57 -07:00
|
|
|
///
|
2020-11-05 10:23:39 +01:00
|
|
|
/// See <https://rustc-dev-guide.rust-lang.org/diagnostics.html> for
|
|
|
|
/// documentation and guidelines on writing lints.
|
2020-09-08 15:09:57 -07:00
|
|
|
///
|
|
|
|
/// The macro call should start with a doc comment explaining the lint
|
|
|
|
/// which will be embedded in the rustc user documentation book. It should
|
|
|
|
/// be written in markdown and have a format that looks like this:
|
|
|
|
///
|
|
|
|
/// ```rust,ignore (doc-example)
|
|
|
|
/// /// The `my_lint_name` lint detects [short explanation here].
|
|
|
|
/// ///
|
|
|
|
/// /// ### Example
|
|
|
|
/// ///
|
|
|
|
/// /// ```rust
|
|
|
|
/// /// [insert a concise example that triggers the lint]
|
|
|
|
/// /// ```
|
|
|
|
/// ///
|
|
|
|
/// /// {{produces}}
|
|
|
|
/// ///
|
|
|
|
/// /// ### Explanation
|
|
|
|
/// ///
|
|
|
|
/// /// This should be a detailed explanation of *why* the lint exists,
|
|
|
|
/// /// and also include suggestions on how the user should fix the problem.
|
|
|
|
/// /// Try to keep the text simple enough that a beginner can understand,
|
|
|
|
/// /// and include links to other documentation for terminology that a
|
|
|
|
/// /// beginner may not be familiar with. If this is "allow" by default,
|
|
|
|
/// /// it should explain why (are there false positives or other issues?). If
|
|
|
|
/// /// this is a future-incompatible lint, it should say so, with text that
|
|
|
|
/// /// looks roughly like this:
|
|
|
|
/// ///
|
|
|
|
/// /// This is a [future-incompatible] lint to transition this to a hard
|
|
|
|
/// /// error in the future. See [issue #xxxxx] for more details.
|
|
|
|
/// ///
|
|
|
|
/// /// [issue #xxxxx]: https://github.com/rust-lang/rust/issues/xxxxx
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// The `{{produces}}` tag will be automatically replaced with the output from
|
2020-11-28 13:29:51 -08:00
|
|
|
/// the example by the build system. If the lint example is too complex to run
|
|
|
|
/// as a simple example (for example, it needs an extern crate), mark the code
|
|
|
|
/// block with `ignore` and manually replace the `{{produces}}` line with the
|
|
|
|
/// expected output in a `text` code block.
|
|
|
|
///
|
|
|
|
/// If this is a rustdoc-only lint, then only include a brief introduction
|
|
|
|
/// with a link with the text `[rustdoc book]` so that the validator knows
|
|
|
|
/// that this is for rustdoc only (see BROKEN_INTRA_DOC_LINKS as an example).
|
|
|
|
///
|
|
|
|
/// Commands to view and test the documentation:
|
|
|
|
///
|
|
|
|
/// * `./x.py doc --stage=1 src/doc/rustc --open`: Builds the rustc book and opens it.
|
|
|
|
/// * `./x.py test src/tools/lint-docs`: Validates that the lint docs have the
|
|
|
|
/// correct style, and that the code example actually emits the expected
|
|
|
|
/// lint.
|
|
|
|
///
|
|
|
|
/// If you have already built the compiler, and you want to make changes to
|
|
|
|
/// just the doc comments, then use the `--keep-stage=0` flag with the above
|
|
|
|
/// commands to avoid rebuilding the compiler.
|
2019-11-12 12:09:20 -05:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! declare_lint {
|
2020-09-08 15:09:57 -07:00
|
|
|
($(#[$attr:meta])* $vis: vis $NAME: ident, $Level: ident, $desc: expr) => (
|
2019-11-12 12:09:20 -05:00
|
|
|
$crate::declare_lint!(
|
2020-09-08 15:09:57 -07:00
|
|
|
$(#[$attr])* $vis $NAME, $Level, $desc,
|
2019-11-12 12:09:20 -05:00
|
|
|
);
|
|
|
|
);
|
2020-09-08 15:09:57 -07:00
|
|
|
($(#[$attr:meta])* $vis: vis $NAME: ident, $Level: ident, $desc: expr,
|
2020-06-03 19:16:29 -07:00
|
|
|
$(@feature_gate = $gate:expr;)?
|
2020-08-13 15:41:52 -04:00
|
|
|
$(@future_incompatible = FutureIncompatibleInfo { $($field:ident : $val:expr),* $(,)* }; )?
|
2020-06-03 19:16:29 -07:00
|
|
|
$($v:ident),*) => (
|
2020-09-08 15:09:57 -07:00
|
|
|
$(#[$attr])*
|
2020-08-13 15:41:52 -04:00
|
|
|
$vis static $NAME: &$crate::Lint = &$crate::Lint {
|
2019-11-12 12:09:20 -05:00
|
|
|
name: stringify!($NAME),
|
2020-08-13 15:41:52 -04:00
|
|
|
default_level: $crate::$Level,
|
2019-11-12 12:09:20 -05:00
|
|
|
desc: $desc,
|
|
|
|
edition_lint_opts: None,
|
|
|
|
is_plugin: false,
|
|
|
|
$($v: true,)*
|
2020-06-03 19:16:29 -07:00
|
|
|
$(feature_gate: Some($gate),)*
|
2020-08-13 15:41:52 -04:00
|
|
|
$(future_incompatible: Some($crate::FutureIncompatibleInfo {
|
|
|
|
$($field: $val,)*
|
|
|
|
..$crate::FutureIncompatibleInfo::default_fields_for_macro()
|
|
|
|
}),)*
|
|
|
|
..$crate::Lint::default_fields_for_macro()
|
2019-11-12 12:09:20 -05:00
|
|
|
};
|
|
|
|
);
|
2020-09-08 15:09:57 -07:00
|
|
|
($(#[$attr:meta])* $vis: vis $NAME: ident, $Level: ident, $desc: expr,
|
2019-11-12 12:09:20 -05:00
|
|
|
$lint_edition: expr => $edition_level: ident
|
|
|
|
) => (
|
2020-09-08 15:09:57 -07:00
|
|
|
$(#[$attr])*
|
2020-08-13 15:41:52 -04:00
|
|
|
$vis static $NAME: &$crate::Lint = &$crate::Lint {
|
2019-11-12 12:09:20 -05:00
|
|
|
name: stringify!($NAME),
|
2020-08-13 15:41:52 -04:00
|
|
|
default_level: $crate::$Level,
|
2019-11-12 12:09:20 -05:00
|
|
|
desc: $desc,
|
2020-08-13 15:41:52 -04:00
|
|
|
edition_lint_opts: Some(($lint_edition, $crate::Level::$edition_level)),
|
2019-11-12 12:09:20 -05:00
|
|
|
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
|
2022-09-12 20:08:58 +02:00
|
|
|
$(, @feature_gate = $gate:expr;)?
|
2019-11-12 12:09:20 -05:00
|
|
|
) => (
|
2022-09-12 20:08:58 +02:00
|
|
|
$crate::declare_tool_lint!{$(#[$attr])* $vis $tool::$NAME, $Level, $desc, false $(, @feature_gate = $gate;)?}
|
2019-11-12 12:09:20 -05:00
|
|
|
);
|
|
|
|
(
|
|
|
|
$(#[$attr:meta])* $vis:vis $tool:ident ::$NAME:ident, $Level:ident, $desc:expr,
|
|
|
|
report_in_external_macro: $rep:expr
|
2022-09-12 20:08:58 +02:00
|
|
|
$(, @feature_gate = $gate:expr;)?
|
2019-11-12 12:09:20 -05:00
|
|
|
) => (
|
2022-09-12 20:08:58 +02:00
|
|
|
$crate::declare_tool_lint!{$(#[$attr])* $vis $tool::$NAME, $Level, $desc, $rep $(, @feature_gate = $gate;)?}
|
2019-11-12 12:09:20 -05:00
|
|
|
);
|
|
|
|
(
|
|
|
|
$(#[$attr:meta])* $vis:vis $tool:ident ::$NAME:ident, $Level:ident, $desc:expr,
|
|
|
|
$external:expr
|
2022-09-12 20:08:58 +02:00
|
|
|
$(, @feature_gate = $gate:expr;)?
|
2019-11-12 12:09:20 -05:00
|
|
|
) => (
|
|
|
|
$(#[$attr])*
|
2020-08-13 15:41:52 -04:00
|
|
|
$vis static $NAME: &$crate::Lint = &$crate::Lint {
|
2019-11-12 12:09:20 -05:00
|
|
|
name: &concat!(stringify!($tool), "::", stringify!($NAME)),
|
2020-08-13 15:41:52 -04:00
|
|
|
default_level: $crate::$Level,
|
2019-11-12 12:09:20 -05:00
|
|
|
desc: $desc,
|
|
|
|
edition_lint_opts: None,
|
|
|
|
report_in_external_macro: $external,
|
|
|
|
future_incompatible: None,
|
|
|
|
is_plugin: true,
|
2022-09-12 20:08:58 +02:00
|
|
|
$(feature_gate: Some($gate),)?
|
2020-06-13 09:58:24 +08:00
|
|
|
crate_level_only: false,
|
2022-09-12 20:08:58 +02:00
|
|
|
..$crate::Lint::default_fields_for_macro()
|
2019-11-12 12:09:20 -05:00
|
|
|
};
|
|
|
|
);
|
|
|
|
}
|
2020-01-05 10:07:26 +01:00
|
|
|
|
|
|
|
/// Declares a static `LintArray` and return it as an expression.
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! lint_array {
|
|
|
|
($( $lint:expr ),* ,) => { lint_array!( $($lint),* ) };
|
|
|
|
($( $lint:expr ),*) => {{
|
|
|
|
vec![$($lint),*]
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type LintArray = Vec<&'static Lint>;
|
|
|
|
|
|
|
|
pub trait LintPass {
|
|
|
|
fn name(&self) -> &'static str;
|
|
|
|
}
|
|
|
|
|
2020-05-27 16:56:57 +02:00
|
|
|
/// Implements `LintPass for $ty` with the given list of `Lint` statics.
|
2020-01-05 10:07:26 +01:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! impl_lint_pass {
|
2020-05-27 16:56:57 +02:00
|
|
|
($ty:ty => [$($lint:expr),* $(,)?]) => {
|
2020-08-13 15:41:52 -04:00
|
|
|
impl $crate::LintPass for $ty {
|
2020-05-27 16:56:57 +02:00
|
|
|
fn name(&self) -> &'static str { stringify!($ty) }
|
2020-01-05 10:07:26 +01:00
|
|
|
}
|
2020-05-27 16:56:57 +02:00
|
|
|
impl $ty {
|
2020-08-13 15:41:52 -04:00
|
|
|
pub fn get_lints() -> $crate::LintArray { $crate::lint_array!($($lint),*) }
|
2020-01-05 10:07:26 +01: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),*]);
|
|
|
|
};
|
|
|
|
}
|