1use std::borrow::Cow;
2use std::fmt::Display;
34use rustc_ast::AttrId;
5use rustc_ast::attr::AttributeExt;
6use rustc_data_structures::fx::FxIndexSet;
7use rustc_data_structures::stable_hasher::{
8HashStable, StableCompare, StableHasher, ToStableHashKey,
9};
10use rustc_error_messages::{DiagArgValue, IntoDiagArg};
11use rustc_hir_id::{HirId, ItemLocalId};
12use rustc_macros::{Decodable, Encodable, HashStable_Generic};
13use rustc_span::def_id::DefPathHash;
14pub use rustc_span::edition::Edition;
15use rustc_span::{HashStableContext, Ident, Span, Symbol, sym};
16use serde::{Deserialize, Serialize};
1718pub use self::Level::*;
1920pub mod builtin;
2122#[macro_export]
23macro_rules!pluralize {
24// Pluralize based on count (e.g., apples)
25($x:expr) => {
26if $x == 1 { "" } else { "s" }
27 };
28 ("has", $x:expr) => {
29if $x == 1 { "has" } else { "have" }
30 };
31 ("is", $x:expr) => {
32if $x == 1 { "is" } else { "are" }
33 };
34 ("was", $x:expr) => {
35if $x == 1 { "was" } else { "were" }
36 };
37 ("this", $x:expr) => {
38if $x == 1 { "this" } else { "these" }
39 };
40}
4142/// Grammatical tool for displaying messages to end users in a nice form.
43///
44/// Take a list of items and a function to turn those items into a `String`, and output a display
45/// friendly comma separated list of those items.
46// FIXME(estebank): this needs to be changed to go through the translation machinery.
47pub fn listify<T>(list: &[T], fmt: impl Fn(&T) -> String) -> Option<String> {
48Some(match list {
49 [only] => fmt(&only),
50 [others @ .., last] => ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} and {1}",
others.iter().map(|i| fmt(i)).collect::<Vec<_>>().join(", "),
fmt(&last)))
})format!(
51"{} and {}",
52 others.iter().map(|i| fmt(i)).collect::<Vec<_>>().join(", "),
53 fmt(&last),
54 ),
55 [] => return None,
56 })
57}
5859/// Indicates the confidence in the correctness of a suggestion.
60///
61/// All suggestions are marked with an `Applicability`. Tools use the applicability of a suggestion
62/// to determine whether it should be automatically applied or if the user should be consulted
63/// before applying the suggestion.
64#[derive(#[automatically_derived]
impl ::core::marker::Copy for Applicability { }Copy, #[automatically_derived]
impl ::core::clone::Clone for Applicability {
#[inline]
fn clone(&self) -> Applicability { *self }
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for Applicability {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
Applicability::MachineApplicable => "MachineApplicable",
Applicability::MaybeIncorrect => "MaybeIncorrect",
Applicability::HasPlaceholders => "HasPlaceholders",
Applicability::Unspecified => "Unspecified",
})
}
}Debug, #[automatically_derived]
impl ::core::hash::Hash for Applicability {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
let __self_discr = ::core::intrinsics::discriminant_value(self);
::core::hash::Hash::hash(&__self_discr, state)
}
}Hash, const _: () =
{
impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
for Applicability {
fn encode(&self, __encoder: &mut __E) {
let disc =
match *self {
Applicability::MachineApplicable => { 0usize }
Applicability::MaybeIncorrect => { 1usize }
Applicability::HasPlaceholders => { 2usize }
Applicability::Unspecified => { 3usize }
};
::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
match *self {
Applicability::MachineApplicable => {}
Applicability::MaybeIncorrect => {}
Applicability::HasPlaceholders => {}
Applicability::Unspecified => {}
}
}
}
};Encodable, const _: () =
{
impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
for Applicability {
fn decode(__decoder: &mut __D) -> Self {
match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
{
0usize => { Applicability::MachineApplicable }
1usize => { Applicability::MaybeIncorrect }
2usize => { Applicability::HasPlaceholders }
3usize => { Applicability::Unspecified }
n => {
::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `Applicability`, expected 0..4, actual {0}",
n));
}
}
}
}
};Decodable, #[doc(hidden)]
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications,
clippy :: absolute_paths,)]
const _: () =
{
#[allow(unused_extern_crates, clippy :: useless_attribute)]
extern crate serde as _serde;
;
#[automatically_derived]
impl _serde::Serialize for Applicability {
fn serialize<__S>(&self, __serializer: __S)
-> _serde::__private228::Result<__S::Ok, __S::Error> where
__S: _serde::Serializer {
match *self {
Applicability::MachineApplicable =>
_serde::Serializer::serialize_unit_variant(__serializer,
"Applicability", 0u32, "MachineApplicable"),
Applicability::MaybeIncorrect =>
_serde::Serializer::serialize_unit_variant(__serializer,
"Applicability", 1u32, "MaybeIncorrect"),
Applicability::HasPlaceholders =>
_serde::Serializer::serialize_unit_variant(__serializer,
"Applicability", 2u32, "HasPlaceholders"),
Applicability::Unspecified =>
_serde::Serializer::serialize_unit_variant(__serializer,
"Applicability", 3u32, "Unspecified"),
}
}
}
};Serialize, #[doc(hidden)]
#[allow(non_upper_case_globals, unused_attributes, unused_qualifications,
clippy :: absolute_paths,)]
const _: () =
{
#[allow(unused_extern_crates, clippy :: useless_attribute)]
extern crate serde as _serde;
;
#[automatically_derived]
impl<'de> _serde::Deserialize<'de> for Applicability {
fn deserialize<__D>(__deserializer: __D)
-> _serde::__private228::Result<Self, __D::Error> where
__D: _serde::Deserializer<'de> {
#[allow(non_camel_case_types)]
#[doc(hidden)]
enum __Field { __field0, __field1, __field2, __field3, }
#[doc(hidden)]
struct __FieldVisitor;
#[automatically_derived]
impl<'de> _serde::de::Visitor<'de> for __FieldVisitor {
type Value = __Field;
fn expecting(&self,
__formatter: &mut _serde::__private228::Formatter)
-> _serde::__private228::fmt::Result {
_serde::__private228::Formatter::write_str(__formatter,
"variant identifier")
}
fn visit_u64<__E>(self, __value: u64)
-> _serde::__private228::Result<Self::Value, __E> where
__E: _serde::de::Error {
match __value {
0u64 => _serde::__private228::Ok(__Field::__field0),
1u64 => _serde::__private228::Ok(__Field::__field1),
2u64 => _serde::__private228::Ok(__Field::__field2),
3u64 => _serde::__private228::Ok(__Field::__field3),
_ =>
_serde::__private228::Err(_serde::de::Error::invalid_value(_serde::de::Unexpected::Unsigned(__value),
&"variant index 0 <= i < 4")),
}
}
fn visit_str<__E>(self, __value: &str)
-> _serde::__private228::Result<Self::Value, __E> where
__E: _serde::de::Error {
match __value {
"MachineApplicable" =>
_serde::__private228::Ok(__Field::__field0),
"MaybeIncorrect" =>
_serde::__private228::Ok(__Field::__field1),
"HasPlaceholders" =>
_serde::__private228::Ok(__Field::__field2),
"Unspecified" =>
_serde::__private228::Ok(__Field::__field3),
_ => {
_serde::__private228::Err(_serde::de::Error::unknown_variant(__value,
VARIANTS))
}
}
}
fn visit_bytes<__E>(self, __value: &[u8])
-> _serde::__private228::Result<Self::Value, __E> where
__E: _serde::de::Error {
match __value {
b"MachineApplicable" =>
_serde::__private228::Ok(__Field::__field0),
b"MaybeIncorrect" =>
_serde::__private228::Ok(__Field::__field1),
b"HasPlaceholders" =>
_serde::__private228::Ok(__Field::__field2),
b"Unspecified" =>
_serde::__private228::Ok(__Field::__field3),
_ => {
let __value =
&_serde::__private228::from_utf8_lossy(__value);
_serde::__private228::Err(_serde::de::Error::unknown_variant(__value,
VARIANTS))
}
}
}
}
#[automatically_derived]
impl<'de> _serde::Deserialize<'de> for __Field {
#[inline]
fn deserialize<__D>(__deserializer: __D)
-> _serde::__private228::Result<Self, __D::Error> where
__D: _serde::Deserializer<'de> {
_serde::Deserializer::deserialize_identifier(__deserializer,
__FieldVisitor)
}
}
#[doc(hidden)]
struct __Visitor<'de> {
marker: _serde::__private228::PhantomData<Applicability>,
lifetime: _serde::__private228::PhantomData<&'de ()>,
}
#[automatically_derived]
impl<'de> _serde::de::Visitor<'de> for __Visitor<'de> {
type Value = Applicability;
fn expecting(&self,
__formatter: &mut _serde::__private228::Formatter)
-> _serde::__private228::fmt::Result {
_serde::__private228::Formatter::write_str(__formatter,
"enum Applicability")
}
fn visit_enum<__A>(self, __data: __A)
-> _serde::__private228::Result<Self::Value, __A::Error>
where __A: _serde::de::EnumAccess<'de> {
match _serde::de::EnumAccess::variant(__data)? {
(__Field::__field0, __variant) => {
_serde::de::VariantAccess::unit_variant(__variant)?;
_serde::__private228::Ok(Applicability::MachineApplicable)
}
(__Field::__field1, __variant) => {
_serde::de::VariantAccess::unit_variant(__variant)?;
_serde::__private228::Ok(Applicability::MaybeIncorrect)
}
(__Field::__field2, __variant) => {
_serde::de::VariantAccess::unit_variant(__variant)?;
_serde::__private228::Ok(Applicability::HasPlaceholders)
}
(__Field::__field3, __variant) => {
_serde::de::VariantAccess::unit_variant(__variant)?;
_serde::__private228::Ok(Applicability::Unspecified)
}
}
}
}
#[doc(hidden)]
const VARIANTS: &'static [&'static str] =
&["MachineApplicable", "MaybeIncorrect", "HasPlaceholders",
"Unspecified"];
_serde::Deserializer::deserialize_enum(__deserializer,
"Applicability", VARIANTS,
__Visitor {
marker: _serde::__private228::PhantomData::<Applicability>,
lifetime: _serde::__private228::PhantomData,
})
}
}
};Deserialize)]
65#[derive(#[automatically_derived]
impl ::core::cmp::PartialEq for Applicability {
#[inline]
fn eq(&self, other: &Applicability) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for Applicability {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::cmp::PartialOrd for Applicability {
#[inline]
fn partial_cmp(&self, other: &Applicability)
-> ::core::option::Option<::core::cmp::Ordering> {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
::core::cmp::PartialOrd::partial_cmp(&__self_discr, &__arg1_discr)
}
}PartialOrd, #[automatically_derived]
impl ::core::cmp::Ord for Applicability {
#[inline]
fn cmp(&self, other: &Applicability) -> ::core::cmp::Ordering {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
::core::cmp::Ord::cmp(&__self_discr, &__arg1_discr)
}
}Ord)]
66pub enum Applicability {
67/// The suggestion is definitely what the user intended, or maintains the exact meaning of the code.
68 /// This suggestion should be automatically applied.
69 ///
70 /// In case of multiple `MachineApplicable` suggestions (whether as part of
71 /// the same `multipart_suggestion` or not), all of them should be
72 /// automatically applied.
73MachineApplicable,
7475/// The suggestion may be what the user intended, but it is uncertain. The suggestion should
76 /// result in valid Rust code if it is applied.
77MaybeIncorrect,
7879/// The suggestion contains placeholders like `(...)` or `{ /* fields */ }`. The suggestion
80 /// cannot be applied automatically because it will not result in valid Rust code. The user
81 /// will need to fill in the placeholders.
82HasPlaceholders,
8384/// The applicability of the suggestion is unknown.
85Unspecified,
86}
8788/// Each lint expectation has a `LintExpectationId` assigned by the `LintLevelsBuilder`.
89/// Expected diagnostics get the lint level `Expect` which stores the `LintExpectationId`
90/// to match it with the actual expectation later on.
91///
92/// The `LintExpectationId` has to be stable between compilations, as diagnostic
93/// instances might be loaded from cache. Lint messages can be emitted during an
94/// `EarlyLintPass` operating on the AST and during a `LateLintPass` traversing the
95/// HIR tree. The AST doesn't have enough information to create a stable id. The
96/// `LintExpectationId` will instead store the [`AttrId`] defining the expectation.
97/// These `LintExpectationId` will be updated to use the stable [`HirId`] once the
98/// AST has been lowered. The transformation is done by the `LintLevelsBuilder`
99///
100/// Each lint inside the `expect` attribute is tracked individually, the `lint_index`
101/// identifies the lint inside the attribute and ensures that the IDs are unique.
102///
103/// The index values have a type of `u16` to reduce the size of the `LintExpectationId`.
104/// It's reasonable to assume that no user will define 2^16 attributes on one node or
105/// have that amount of lints listed. `u16` values should therefore suffice.
106#[derive(#[automatically_derived]
impl ::core::clone::Clone for LintExpectationId {
#[inline]
fn clone(&self) -> LintExpectationId {
let _: ::core::clone::AssertParamIsClone<AttrId>;
let _: ::core::clone::AssertParamIsClone<Option<u16>>;
let _: ::core::clone::AssertParamIsClone<HirId>;
let _: ::core::clone::AssertParamIsClone<u16>;
let _: ::core::clone::AssertParamIsClone<Option<u16>>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for LintExpectationId { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for LintExpectationId {
#[inline]
fn eq(&self, other: &LintExpectationId) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(LintExpectationId::Unstable {
attr_id: __self_0, lint_index: __self_1 },
LintExpectationId::Unstable {
attr_id: __arg1_0, lint_index: __arg1_1 }) =>
__self_0 == __arg1_0 && __self_1 == __arg1_1,
(LintExpectationId::Stable {
hir_id: __self_0, attr_index: __self_1, lint_index: __self_2
}, LintExpectationId::Stable {
hir_id: __arg1_0, attr_index: __arg1_1, lint_index: __arg1_2
}) =>
__self_1 == __arg1_1 && __self_0 == __arg1_0 &&
__self_2 == __arg1_2,
_ => unsafe { ::core::intrinsics::unreachable() }
}
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for LintExpectationId {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<AttrId>;
let _: ::core::cmp::AssertParamIsEq<Option<u16>>;
let _: ::core::cmp::AssertParamIsEq<HirId>;
let _: ::core::cmp::AssertParamIsEq<u16>;
let _: ::core::cmp::AssertParamIsEq<Option<u16>>;
}
}Eq, #[automatically_derived]
impl ::core::fmt::Debug for LintExpectationId {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
LintExpectationId::Unstable {
attr_id: __self_0, lint_index: __self_1 } =>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"Unstable", "attr_id", __self_0, "lint_index", &__self_1),
LintExpectationId::Stable {
hir_id: __self_0, attr_index: __self_1, lint_index: __self_2 }
=>
::core::fmt::Formatter::debug_struct_field3_finish(f,
"Stable", "hir_id", __self_0, "attr_index", __self_1,
"lint_index", &__self_2),
}
}
}Debug, #[automatically_derived]
impl ::core::hash::Hash for LintExpectationId {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
let __self_discr = ::core::intrinsics::discriminant_value(self);
::core::hash::Hash::hash(&__self_discr, state);
match self {
LintExpectationId::Unstable {
attr_id: __self_0, lint_index: __self_1 } => {
::core::hash::Hash::hash(__self_0, state);
::core::hash::Hash::hash(__self_1, state)
}
LintExpectationId::Stable {
hir_id: __self_0, attr_index: __self_1, lint_index: __self_2 }
=> {
::core::hash::Hash::hash(__self_0, state);
::core::hash::Hash::hash(__self_1, state);
::core::hash::Hash::hash(__self_2, state)
}
}
}
}Hash, const _: () =
{
impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
for LintExpectationId {
fn encode(&self, __encoder: &mut __E) {
let disc =
match *self {
LintExpectationId::Unstable {
attr_id: ref __binding_0, lint_index: ref __binding_1 } => {
0usize
}
LintExpectationId::Stable {
hir_id: ref __binding_0,
attr_index: ref __binding_1,
lint_index: ref __binding_2 } => {
1usize
}
};
::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
match *self {
LintExpectationId::Unstable {
attr_id: ref __binding_0, lint_index: ref __binding_1 } => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
}
LintExpectationId::Stable {
hir_id: ref __binding_0,
attr_index: ref __binding_1,
lint_index: ref __binding_2 } => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_2,
__encoder);
}
}
}
}
};Encodable, const _: () =
{
impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
for LintExpectationId {
fn decode(__decoder: &mut __D) -> Self {
match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
{
0usize => {
LintExpectationId::Unstable {
attr_id: ::rustc_serialize::Decodable::decode(__decoder),
lint_index: ::rustc_serialize::Decodable::decode(__decoder),
}
}
1usize => {
LintExpectationId::Stable {
hir_id: ::rustc_serialize::Decodable::decode(__decoder),
attr_index: ::rustc_serialize::Decodable::decode(__decoder),
lint_index: ::rustc_serialize::Decodable::decode(__decoder),
}
}
n => {
::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `LintExpectationId`, expected 0..2, actual {0}",
n));
}
}
}
}
};Decodable)]
107pub enum LintExpectationId {
108/// Used for lints emitted during the `EarlyLintPass`. This id is not
109 /// hash stable and should not be cached.
110Unstable { attr_id: AttrId, lint_index: Option<u16> },
111/// The [`HirId`] that the lint expectation is attached to. This id is
112 /// stable and can be cached. The additional index ensures that nodes with
113 /// several expectations can correctly match diagnostics to the individual
114 /// expectation.
115Stable { hir_id: HirId, attr_index: u16, lint_index: Option<u16> },
116}
117118impl LintExpectationId {
119pub fn is_stable(&self) -> bool {
120match self {
121 LintExpectationId::Unstable { .. } => false,
122 LintExpectationId::Stable { .. } => true,
123 }
124 }
125126pub fn get_lint_index(&self) -> Option<u16> {
127let (LintExpectationId::Unstable { lint_index, .. }
128 | LintExpectationId::Stable { lint_index, .. }) = self;
129130*lint_index131 }
132133pub fn set_lint_index(&mut self, new_lint_index: Option<u16>) {
134let (LintExpectationId::Unstable { lint_index, .. }
135 | LintExpectationId::Stable { lint_index, .. }) = self;
136137*lint_index = new_lint_index138 }
139}
140141impl<Hcx: HashStableContext> HashStable<Hcx> for LintExpectationId {
142#[inline]
143fn hash_stable(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
144match self {
145 LintExpectationId::Stable { hir_id, attr_index, lint_index: Some(lint_index) } => {
146hir_id.hash_stable(hcx, hasher);
147attr_index.hash_stable(hcx, hasher);
148lint_index.hash_stable(hcx, hasher);
149 }
150_ => {
151{
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("HashStable should only be called for filled and stable `LintExpectationId`")));
}unreachable!(
152"HashStable should only be called for filled and stable `LintExpectationId`"
153)154 }
155 }
156 }
157}
158159impl<Hcx: HashStableContext> ToStableHashKey<Hcx> for LintExpectationId {
160type KeyType = (DefPathHash, ItemLocalId, u16, u16);
161162#[inline]
163fn to_stable_hash_key(&self, hcx: &mut Hcx) -> Self::KeyType {
164match self {
165 LintExpectationId::Stable { hir_id, attr_index, lint_index: Some(lint_index) } => {
166let (def_path_hash, lint_idx) = hir_id.to_stable_hash_key(hcx);
167 (def_path_hash, lint_idx, *attr_index, *lint_index)
168 }
169_ => {
170{
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("HashStable should only be called for a filled `LintExpectationId`")));
}unreachable!("HashStable should only be called for a filled `LintExpectationId`")171 }
172 }
173 }
174}
175176/// Setting for how to handle a lint.
177///
178/// See: <https://doc.rust-lang.org/rustc/lints/levels.html>
179#[derive(
180#[automatically_derived]
impl ::core::clone::Clone for Level {
#[inline]
fn clone(&self) -> Level { *self }
}Clone,
181#[automatically_derived]
impl ::core::marker::Copy for Level { }Copy,
182#[automatically_derived]
impl ::core::cmp::PartialEq for Level {
#[inline]
fn eq(&self, other: &Level) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr
}
}PartialEq,
183#[automatically_derived]
impl ::core::cmp::PartialOrd for Level {
#[inline]
fn partial_cmp(&self, other: &Level)
-> ::core::option::Option<::core::cmp::Ordering> {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
::core::cmp::PartialOrd::partial_cmp(&__self_discr, &__arg1_discr)
}
}PartialOrd,
184#[automatically_derived]
impl ::core::cmp::Eq for Level {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {}
}Eq,
185#[automatically_derived]
impl ::core::cmp::Ord for Level {
#[inline]
fn cmp(&self, other: &Level) -> ::core::cmp::Ordering {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
::core::cmp::Ord::cmp(&__self_discr, &__arg1_discr)
}
}Ord,
186#[automatically_derived]
impl ::core::fmt::Debug for Level {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::write_str(f,
match self {
Level::Allow => "Allow",
Level::Expect => "Expect",
Level::Warn => "Warn",
Level::ForceWarn => "ForceWarn",
Level::Deny => "Deny",
Level::Forbid => "Forbid",
})
}
}Debug,
187#[automatically_derived]
impl ::core::hash::Hash for Level {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
let __self_discr = ::core::intrinsics::discriminant_value(self);
::core::hash::Hash::hash(&__self_discr, state)
}
}Hash,
188const _: () =
{
impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
for Level {
fn encode(&self, __encoder: &mut __E) {
let disc =
match *self {
Level::Allow => { 0usize }
Level::Expect => { 1usize }
Level::Warn => { 2usize }
Level::ForceWarn => { 3usize }
Level::Deny => { 4usize }
Level::Forbid => { 5usize }
};
::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
match *self {
Level::Allow => {}
Level::Expect => {}
Level::Warn => {}
Level::ForceWarn => {}
Level::Deny => {}
Level::Forbid => {}
}
}
}
};Encodable,
189const _: () =
{
impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
for Level {
fn decode(__decoder: &mut __D) -> Self {
match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
{
0usize => { Level::Allow }
1usize => { Level::Expect }
2usize => { Level::Warn }
3usize => { Level::ForceWarn }
4usize => { Level::Deny }
5usize => { Level::Forbid }
n => {
::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `Level`, expected 0..6, actual {0}",
n));
}
}
}
}
};Decodable,
190const _: () =
{
impl<__CTX> ::rustc_data_structures::stable_hasher::HashStable<__CTX>
for Level where __CTX: ::rustc_span::HashStableContext {
#[inline]
fn hash_stable(&self, __hcx: &mut __CTX,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
match *self {
Level::Allow => {}
Level::Expect => {}
Level::Warn => {}
Level::ForceWarn => {}
Level::Deny => {}
Level::Forbid => {}
}
}
}
};HashStable_Generic191)]
192pub enum Level {
193/// The `allow` level will not issue any message.
194Allow,
195/// The `expect` level will suppress the lint message but in turn produce a message
196 /// if the lint wasn't issued in the expected scope. `Expect` should not be used as
197 /// an initial level for a lint.
198 ///
199 /// Note that this still means that the lint is enabled in this position and should
200 /// be emitted, this will in turn fulfill the expectation and suppress the lint.
201 ///
202 /// See RFC 2383.
203 ///
204 /// Requires a [`LintExpectationId`] to later link a lint emission to the actual
205 /// expectation. It can be ignored in most cases.
206Expect,
207/// The `warn` level will produce a warning if the lint was violated, however the
208 /// compiler will continue with its execution.
209Warn,
210/// This lint level is a special case of [`Warn`], that can't be overridden. This is used
211 /// to ensure that a lint can't be suppressed. This lint level can currently only be set
212 /// via the console and is therefore session specific.
213 ///
214 /// Requires a [`LintExpectationId`] to fulfill expectations marked via the
215 /// `#[expect]` attribute, that will still be suppressed due to the level.
216ForceWarn,
217/// The `deny` level will produce an error and stop further execution after the lint
218 /// pass is complete.
219Deny,
220/// `Forbid` is equivalent to the `deny` level but can't be overwritten like the previous
221 /// levels.
222Forbid,
223}
224225impl Level {
226/// Converts a level to a lower-case string.
227pub fn as_str(self) -> &'static str {
228match self {
229 Level::Allow => "allow",
230 Level::Expect => "expect",
231 Level::Warn => "warn",
232 Level::ForceWarn => "force-warn",
233 Level::Deny => "deny",
234 Level::Forbid => "forbid",
235 }
236 }
237238/// Converts a lower-case string to a level. This will never construct the expect
239 /// level as that would require a [`LintExpectationId`].
240pub fn from_str(x: &str) -> Option<Self> {
241match x {
242"allow" => Some(Level::Allow),
243"warn" => Some(Level::Warn),
244"deny" => Some(Level::Deny),
245"forbid" => Some(Level::Forbid),
246"expect" | _ => None,
247 }
248 }
249250/// Converts an `Attribute` to a level.
251pub fn from_attr(attr: &impl AttributeExt) -> Option<(Self, Option<LintExpectationId>)> {
252attr.name().and_then(|name| Self::from_symbol(name, || Some(attr.id())))
253 }
254255/// Converts a `Symbol` to a level.
256pub fn from_symbol(
257 s: Symbol,
258 id: impl FnOnce() -> Option<AttrId>,
259 ) -> Option<(Self, Option<LintExpectationId>)> {
260match s {
261 sym::allow => Some((Level::Allow, None)),
262 sym::expect => {
263if let Some(attr_id) = id() {
264Some((
265 Level::Expect,
266Some(LintExpectationId::Unstable { attr_id, lint_index: None }),
267 ))
268 } else {
269None270 }
271 }
272 sym::warn => Some((Level::Warn, None)),
273 sym::deny => Some((Level::Deny, None)),
274 sym::forbid => Some((Level::Forbid, None)),
275_ => None,
276 }
277 }
278279pub fn to_cmd_flag(self) -> &'static str {
280match self {
281 Level::Warn => "-W",
282 Level::Deny => "-D",
283 Level::Forbid => "-F",
284 Level::Allow => "-A",
285 Level::ForceWarn => "--force-warn",
286 Level::Expect => {
287{
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("the expect level does not have a commandline flag")));
}unreachable!("the expect level does not have a commandline flag")288 }
289 }
290 }
291292pub fn is_error(self) -> bool {
293match self {
294 Level::Allow | Level::Expect | Level::Warn | Level::ForceWarn => false,
295 Level::Deny | Level::Forbid => true,
296 }
297 }
298}
299300impl IntoDiagArg for Level {
301fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
302 DiagArgValue::Str(Cow::Borrowed(self.to_cmd_flag()))
303 }
304}
305306/// Specification of a single lint.
307#[derive(#[automatically_derived]
impl ::core::marker::Copy for Lint { }Copy, #[automatically_derived]
impl ::core::clone::Clone for Lint {
#[inline]
fn clone(&self) -> Lint {
let _: ::core::clone::AssertParamIsClone<&'static str>;
let _: ::core::clone::AssertParamIsClone<Level>;
let _: ::core::clone::AssertParamIsClone<&'static str>;
let _: ::core::clone::AssertParamIsClone<Option<(Edition, Level)>>;
let _: ::core::clone::AssertParamIsClone<bool>;
let _:
::core::clone::AssertParamIsClone<Option<FutureIncompatibleInfo>>;
let _: ::core::clone::AssertParamIsClone<Option<Symbol>>;
*self
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for Lint {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
let names: &'static _ =
&["name", "default_level", "desc", "edition_lint_opts",
"report_in_external_macro", "future_incompatible",
"is_externally_loaded", "feature_gate", "crate_level_only",
"eval_always"];
let values: &[&dyn ::core::fmt::Debug] =
&[&self.name, &self.default_level, &self.desc,
&self.edition_lint_opts, &self.report_in_external_macro,
&self.future_incompatible, &self.is_externally_loaded,
&self.feature_gate, &self.crate_level_only,
&&self.eval_always];
::core::fmt::Formatter::debug_struct_fields_finish(f, "Lint", names,
values)
}
}Debug)]
308pub struct Lint {
309/// A string identifier for the lint.
310 ///
311 /// This identifies the lint in attributes and in command-line arguments.
312 /// In those contexts it is always lowercase, but this field is compared
313 /// in a way which is case-insensitive for ASCII characters. This allows
314 /// `declare_lint!()` invocations to follow the convention of upper-case
315 /// statics without repeating the name.
316 ///
317 /// The name is written with underscores, e.g., "unused_imports".
318 /// On the command line, underscores become dashes.
319 ///
320 /// See <https://rustc-dev-guide.rust-lang.org/diagnostics.html#lint-naming>
321 /// for naming guidelines.
322pub name: &'static str,
323324/// Default level for the lint.
325 ///
326 /// See <https://rustc-dev-guide.rust-lang.org/diagnostics.html#diagnostic-levels>
327 /// for guidelines on choosing a default level.
328pub default_level: Level,
329330/// Description of the lint or the issue it detects.
331 ///
332 /// e.g., "imports that are never used"
333pub desc: &'static str,
334335/// Starting at the given edition, default to the given lint level. If this is `None`, then use
336 /// `default_level`.
337pub edition_lint_opts: Option<(Edition, Level)>,
338339/// `true` if this lint is reported even inside expansions of external macros.
340pub report_in_external_macro: bool,
341342pub future_incompatible: Option<FutureIncompatibleInfo>,
343344/// `true` if this lint is being loaded by another tool (e.g. Clippy).
345pub is_externally_loaded: bool,
346347/// `Some` if this lint is feature gated, otherwise `None`.
348pub feature_gate: Option<Symbol>,
349350pub crate_level_only: bool,
351352/// `true` if this lint should not be filtered out under any circustamces
353 /// (e.g. the unknown_attributes lint)
354pub eval_always: bool,
355}
356357/// Extra information for a future incompatibility lint.
358#[derive(#[automatically_derived]
impl ::core::marker::Copy for FutureIncompatibleInfo { }Copy, #[automatically_derived]
impl ::core::clone::Clone for FutureIncompatibleInfo {
#[inline]
fn clone(&self) -> FutureIncompatibleInfo {
let _: ::core::clone::AssertParamIsClone<FutureIncompatibilityReason>;
let _: ::core::clone::AssertParamIsClone<bool>;
*self
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for FutureIncompatibleInfo {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field3_finish(f,
"FutureIncompatibleInfo", "reason", &self.reason,
"explain_reason", &self.explain_reason, "report_in_deps",
&&self.report_in_deps)
}
}Debug)]
359pub struct FutureIncompatibleInfo {
360/// The reason for the lint used by diagnostics to provide
361 /// the right help message
362pub reason: FutureIncompatibilityReason,
363/// Whether to explain the reason to the user.
364 ///
365 /// Set to false for lints that already include a more detailed
366 /// explanation.
367pub explain_reason: bool,
368/// If set to `true`, this will make future incompatibility warnings show up in cargo's
369 /// reports.
370 ///
371 /// When a future incompatibility warning is first inroduced, set this to `false`
372 /// (or, rather, don't override the default). This allows crate developers an opportunity
373 /// to fix the warning before blasting all dependents with a warning they can't fix
374 /// (dependents have to wait for a new release of the affected crate to be published).
375 ///
376 /// After a lint has been in this state for a while, consider setting this to true, so it
377 /// warns for everyone. It is a good signal that it is ready if you can determine that all
378 /// or most affected crates on crates.io have been updated.
379pub report_in_deps: bool,
380}
381382#[derive(#[automatically_derived]
impl ::core::marker::Copy for EditionFcw { }Copy, #[automatically_derived]
impl ::core::clone::Clone for EditionFcw {
#[inline]
fn clone(&self) -> EditionFcw {
let _: ::core::clone::AssertParamIsClone<Edition>;
let _: ::core::clone::AssertParamIsClone<&'static str>;
*self
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for EditionFcw {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "EditionFcw",
"edition", &self.edition, "page_slug", &&self.page_slug)
}
}Debug)]
383pub struct EditionFcw {
384pub edition: Edition,
385pub page_slug: &'static str,
386}
387388#[derive(#[automatically_derived]
impl ::core::marker::Copy for ReleaseFcw { }Copy, #[automatically_derived]
impl ::core::clone::Clone for ReleaseFcw {
#[inline]
fn clone(&self) -> ReleaseFcw {
let _: ::core::clone::AssertParamIsClone<usize>;
*self
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for ReleaseFcw {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field1_finish(f, "ReleaseFcw",
"issue_number", &&self.issue_number)
}
}Debug)]
389pub struct ReleaseFcw {
390pub issue_number: usize,
391}
392393/// The reason for future incompatibility
394///
395/// Future-incompatible lints come in roughly two categories:
396///
397/// 1. There was a mistake in the compiler (such as a soundness issue), and
398/// we're trying to fix it, but it may be a breaking change.
399/// 2. A change across an Edition boundary, typically used for the
400/// introduction of new language features that can't otherwise be
401/// introduced in a backwards-compatible way.
402///
403/// See <https://rustc-dev-guide.rust-lang.org/bug-fix-procedure.html> and
404/// <https://rustc-dev-guide.rust-lang.org/diagnostics.html#future-incompatible-lints>
405/// for more information.
406#[derive(#[automatically_derived]
impl ::core::marker::Copy for FutureIncompatibilityReason { }Copy, #[automatically_derived]
impl ::core::clone::Clone for FutureIncompatibilityReason {
#[inline]
fn clone(&self) -> FutureIncompatibilityReason {
let _: ::core::clone::AssertParamIsClone<ReleaseFcw>;
let _: ::core::clone::AssertParamIsClone<EditionFcw>;
let _: ::core::clone::AssertParamIsClone<&'static str>;
*self
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for FutureIncompatibilityReason {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
FutureIncompatibilityReason::FutureReleaseError(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"FutureReleaseError", &__self_0),
FutureIncompatibilityReason::FutureReleaseSemanticsChange(__self_0)
=>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"FutureReleaseSemanticsChange", &__self_0),
FutureIncompatibilityReason::EditionError(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"EditionError", &__self_0),
FutureIncompatibilityReason::EditionSemanticsChange(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"EditionSemanticsChange", &__self_0),
FutureIncompatibilityReason::EditionAndFutureReleaseError(__self_0)
=>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"EditionAndFutureReleaseError", &__self_0),
FutureIncompatibilityReason::EditionAndFutureReleaseSemanticsChange(__self_0)
=>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"EditionAndFutureReleaseSemanticsChange", &__self_0),
FutureIncompatibilityReason::Custom(__self_0, __self_1) =>
::core::fmt::Formatter::debug_tuple_field2_finish(f, "Custom",
__self_0, &__self_1),
FutureIncompatibilityReason::Unreachable =>
::core::fmt::Formatter::write_str(f, "Unreachable"),
}
}
}Debug)]
407pub enum FutureIncompatibilityReason {
408/// This will be an error in a future release for all editions
409 ///
410 /// Choose this variant when you are first introducing a "future
411 /// incompatible" warning that is intended to eventually be fixed in the
412 /// future.
413 ///
414 /// After a lint has been in this state for a while and you feel like it is ready to graduate
415 /// to warning everyone, consider setting [`FutureIncompatibleInfo::report_in_deps`] to true.
416 /// (see its documentation for more guidance)
417 ///
418 /// After some period of time, lints with this variant can be turned into
419 /// hard errors (and the lint removed). Preferably when there is some
420 /// confidence that the number of impacted projects is very small (few
421 /// should have a broken dependency in their dependency tree).
422FutureReleaseError(ReleaseFcw),
423/// Code that changes meaning in some way in a
424 /// future release.
425 ///
426 /// Choose this variant when the semantics of existing code is changing,
427 /// (as opposed to [`FutureIncompatibilityReason::FutureReleaseError`],
428 /// which is for when code is going to be rejected in the future).
429FutureReleaseSemanticsChange(ReleaseFcw),
430/// Previously accepted code that will become an
431 /// error in the provided edition
432 ///
433 /// Choose this variant for code that you want to start rejecting across
434 /// an edition boundary. This will automatically include the lint in the
435 /// `rust-20xx-compatibility` lint group, which is used by `cargo fix
436 /// --edition` to do migrations. The lint *should* be auto-fixable with
437 /// [`Applicability::MachineApplicable`].
438 ///
439 /// The lint can either be `Allow` or `Warn` by default. If it is `Allow`,
440 /// users usually won't see this warning unless they are doing an edition
441 /// migration manually or there is a problem during the migration (cargo's
442 /// automatic migrations will force the level to `Warn`). If it is `Warn`
443 /// by default, users on all editions will see this warning (only do this
444 /// if you think it is important for everyone to be aware of the change,
445 /// and to encourage people to update their code on all editions).
446 ///
447 /// See also [`FutureIncompatibilityReason::EditionSemanticsChange`] if
448 /// you have code that is changing semantics across the edition (as
449 /// opposed to being rejected).
450EditionError(EditionFcw),
451/// Code that changes meaning in some way in
452 /// the provided edition
453 ///
454 /// This is the same as [`FutureIncompatibilityReason::EditionError`],
455 /// except for situations where the semantics change across an edition. It
456 /// slightly changes the text of the diagnostic, but is otherwise the
457 /// same.
458EditionSemanticsChange(EditionFcw),
459/// This will be an error in the provided edition *and* in a future
460 /// release.
461 ///
462 /// This variant a combination of [`FutureReleaseError`] and [`EditionError`].
463 /// This is useful in rare cases when we want to have "preview" of a breaking
464 /// change in an edition, but do a breaking change later on all editions anyway.
465 ///
466 /// [`EditionError`]: FutureIncompatibilityReason::EditionError
467 /// [`FutureReleaseError`]: FutureIncompatibilityReason::FutureReleaseError
468EditionAndFutureReleaseError(EditionFcw),
469/// This will change meaning in the provided edition *and* in a future
470 /// release.
471 ///
472 /// This variant a combination of [`FutureReleaseSemanticsChange`]
473 /// and [`EditionSemanticsChange`]. This is useful in rare cases when we
474 /// want to have "preview" of a breaking change in an edition, but do a
475 /// breaking change later on all editions anyway.
476 ///
477 /// [`EditionSemanticsChange`]: FutureIncompatibilityReason::EditionSemanticsChange
478 /// [`FutureReleaseSemanticsChange`]: FutureIncompatibilityReason::FutureReleaseSemanticsChange
479EditionAndFutureReleaseSemanticsChange(EditionFcw),
480/// A custom reason.
481 ///
482 /// Choose this variant if the built-in text of the diagnostic of the
483 /// other variants doesn't match your situation. This is behaviorally
484 /// equivalent to
485 /// [`FutureIncompatibilityReason::FutureReleaseError`].
486Custom(&'static str, ReleaseFcw),
487488/// Using the declare_lint macro a reason always needs to be specified.
489 /// So, this case can't actually be reached but a variant needs to exist for it.
490 /// Any code panics on seeing this variant. Do not use.
491Unreachable,
492}
493494impl FutureIncompatibleInfo {
495pub const fn default_fields_for_macro() -> Self {
496FutureIncompatibleInfo {
497 reason: FutureIncompatibilityReason::Unreachable,
498 explain_reason: true,
499 report_in_deps: false,
500 }
501 }
502}
503504impl FutureIncompatibilityReason {
505pub fn edition(self) -> Option<Edition> {
506match self {
507Self::EditionError(e)
508 | Self::EditionSemanticsChange(e)
509 | Self::EditionAndFutureReleaseError(e)
510 | Self::EditionAndFutureReleaseSemanticsChange(e) => Some(e.edition),
511512 FutureIncompatibilityReason::FutureReleaseError(_)
513 | FutureIncompatibilityReason::FutureReleaseSemanticsChange(_)
514 | FutureIncompatibilityReason::Custom(_, _) => None,
515Self::Unreachable => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
516 }
517 }
518519pub fn reference(&self) -> String {
520match self {
521Self::FutureReleaseSemanticsChange(release_fcw)
522 | Self::FutureReleaseError(release_fcw)
523 | Self::Custom(_, release_fcw) => release_fcw.to_string(),
524Self::EditionError(edition_fcw)
525 | Self::EditionSemanticsChange(edition_fcw)
526 | Self::EditionAndFutureReleaseError(edition_fcw)
527 | Self::EditionAndFutureReleaseSemanticsChange(edition_fcw) => edition_fcw.to_string(),
528Self::Unreachable => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
529 }
530 }
531}
532533impl Displayfor ReleaseFcw {
534fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
535let issue_number = self.issue_number;
536f.write_fmt(format_args!("issue #{0} <https://github.com/rust-lang/rust/issues/{0}>",
issue_number))write!(f, "issue #{issue_number} <https://github.com/rust-lang/rust/issues/{issue_number}>")537 }
538}
539540impl Displayfor EditionFcw {
541fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
542f.write_fmt(format_args!("<https://doc.rust-lang.org/edition-guide/{0}/{1}.html>",
match self.edition {
Edition::Edition2015 => "rust-2015",
Edition::Edition2018 => "rust-2018",
Edition::Edition2021 => "rust-2021",
Edition::Edition2024 => "rust-2024",
Edition::EditionFuture => "future",
}, self.page_slug))write!(
543f,
544"<https://doc.rust-lang.org/edition-guide/{}/{}.html>",
545match self.edition {
546 Edition::Edition2015 => "rust-2015",
547 Edition::Edition2018 => "rust-2018",
548 Edition::Edition2021 => "rust-2021",
549 Edition::Edition2024 => "rust-2024",
550 Edition::EditionFuture => "future",
551 },
552self.page_slug,
553 )554 }
555}
556557impl Lint {
558pub const fn default_fields_for_macro() -> Self {
559Lint {
560 name: "",
561 default_level: Level::Forbid,
562 desc: "",
563 edition_lint_opts: None,
564 is_externally_loaded: false,
565 report_in_external_macro: false,
566 future_incompatible: None,
567 feature_gate: None,
568 crate_level_only: false,
569 eval_always: false,
570 }
571 }
572573/// Gets the lint's name, with ASCII letters converted to lowercase.
574pub fn name_lower(&self) -> String {
575self.name.to_ascii_lowercase()
576 }
577578pub fn default_level(&self, edition: Edition) -> Level {
579self.edition_lint_opts
580 .filter(|(e, _)| *e <= edition)
581 .map(|(_, l)| l)
582 .unwrap_or(self.default_level)
583 }
584}
585586/// Identifies a lint known to the compiler.
587#[derive(#[automatically_derived]
impl ::core::clone::Clone for LintId {
#[inline]
fn clone(&self) -> LintId {
let _: ::core::clone::AssertParamIsClone<&'static Lint>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for LintId { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for LintId {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field1_finish(f, "LintId",
"lint", &&self.lint)
}
}Debug)]
588pub struct LintId {
589// Identity is based on pointer equality of this field.
590pub lint: &'static Lint,
591}
592593impl PartialEqfor LintId {
594fn eq(&self, other: &LintId) -> bool {
595 std::ptr::eq(self.lint, other.lint)
596 }
597}
598599impl Eqfor LintId {}
600601impl std::hash::Hashfor LintId {
602fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
603let ptr = self.lint as *const Lint;
604ptr.hash(state);
605 }
606}
607608impl LintId {
609/// Gets the `LintId` for a `Lint`.
610pub fn of(lint: &'static Lint) -> LintId {
611LintId { lint }
612 }
613614pub fn lint_name_raw(&self) -> &'static str {
615self.lint.name
616 }
617618/// Gets the name of the lint.
619pub fn to_string(&self) -> String {
620self.lint.name_lower()
621 }
622}
623624impl<Hcx> HashStable<Hcx> for LintId {
625#[inline]
626fn hash_stable(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
627self.lint_name_raw().hash_stable(hcx, hasher);
628 }
629}
630631impl<Hcx> ToStableHashKey<Hcx> for LintId {
632type KeyType = &'static str;
633634#[inline]
635fn to_stable_hash_key(&self, _: &mut Hcx) -> &'static str {
636self.lint_name_raw()
637 }
638}
639640impl StableComparefor LintId {
641const CAN_USE_UNSTABLE_SORT: bool = true;
642643fn stable_cmp(&self, other: &Self) -> std::cmp::Ordering {
644self.lint_name_raw().cmp(&other.lint_name_raw())
645 }
646}
647648#[derive(#[automatically_derived]
impl ::core::fmt::Debug for DeprecatedSinceKind {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
DeprecatedSinceKind::InEffect =>
::core::fmt::Formatter::write_str(f, "InEffect"),
DeprecatedSinceKind::InFuture =>
::core::fmt::Formatter::write_str(f, "InFuture"),
DeprecatedSinceKind::InVersion(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"InVersion", &__self_0),
}
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for DeprecatedSinceKind {
#[inline]
fn clone(&self) -> DeprecatedSinceKind {
match self {
DeprecatedSinceKind::InEffect => DeprecatedSinceKind::InEffect,
DeprecatedSinceKind::InFuture => DeprecatedSinceKind::InFuture,
DeprecatedSinceKind::InVersion(__self_0) =>
DeprecatedSinceKind::InVersion(::core::clone::Clone::clone(__self_0)),
}
}
}Clone)]
649pub enum DeprecatedSinceKind {
650 InEffect,
651 InFuture,
652 InVersion(String),
653}
654655#[derive(#[automatically_derived]
impl ::core::fmt::Debug for AttributeLintKind {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
AttributeLintKind::UnexpectedCfgName(__self_0, __self_1) =>
::core::fmt::Formatter::debug_tuple_field2_finish(f,
"UnexpectedCfgName", __self_0, &__self_1),
AttributeLintKind::UnexpectedCfgValue(__self_0, __self_1) =>
::core::fmt::Formatter::debug_tuple_field2_finish(f,
"UnexpectedCfgValue", __self_0, &__self_1),
AttributeLintKind::DocTestTakesList =>
::core::fmt::Formatter::write_str(f, "DocTestTakesList"),
AttributeLintKind::DocTestUnknown { name: __self_0 } =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"DocTestUnknown", "name", &__self_0),
AttributeLintKind::DocTestLiteral =>
::core::fmt::Formatter::write_str(f, "DocTestLiteral"),
AttributeLintKind::AttrCrateLevelOnly =>
::core::fmt::Formatter::write_str(f, "AttrCrateLevelOnly"),
AttributeLintKind::DoNotRecommendDoesNotExpectArgs =>
::core::fmt::Formatter::write_str(f,
"DoNotRecommendDoesNotExpectArgs"),
AttributeLintKind::CrateTypeUnknown {
span: __self_0, suggested: __self_1 } =>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"CrateTypeUnknown", "span", __self_0, "suggested",
&__self_1),
AttributeLintKind::MalformedDoc =>
::core::fmt::Formatter::write_str(f, "MalformedDoc"),
AttributeLintKind::ExpectedNoArgs =>
::core::fmt::Formatter::write_str(f, "ExpectedNoArgs"),
AttributeLintKind::ExpectedNameValue =>
::core::fmt::Formatter::write_str(f, "ExpectedNameValue"),
AttributeLintKind::MalFormedDiagnosticAttribute {
attribute: __self_0, options: __self_1, span: __self_2 } =>
::core::fmt::Formatter::debug_struct_field3_finish(f,
"MalFormedDiagnosticAttribute", "attribute", __self_0,
"options", __self_1, "span", &__self_2),
AttributeLintKind::MalformedDiagnosticFormat { warning: __self_0 }
=>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"MalformedDiagnosticFormat", "warning", &__self_0),
AttributeLintKind::DiagnosticWrappedParserError {
description: __self_0, label: __self_1, span: __self_2 } =>
::core::fmt::Formatter::debug_struct_field3_finish(f,
"DiagnosticWrappedParserError", "description", __self_0,
"label", __self_1, "span", &__self_2),
AttributeLintKind::IgnoredDiagnosticOption {
option_name: __self_0,
first_span: __self_1,
later_span: __self_2 } =>
::core::fmt::Formatter::debug_struct_field3_finish(f,
"IgnoredDiagnosticOption", "option_name", __self_0,
"first_span", __self_1, "later_span", &__self_2),
AttributeLintKind::MissingOptionsForDiagnosticAttribute {
attribute: __self_0, options: __self_1 } =>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"MissingOptionsForDiagnosticAttribute", "attribute",
__self_0, "options", &__self_1),
AttributeLintKind::NonMetaItemDiagnosticAttribute =>
::core::fmt::Formatter::write_str(f,
"NonMetaItemDiagnosticAttribute"),
}
}
}Debug)]
656pub enum AttributeLintKind {
657 UnexpectedCfgName((Symbol, Span), Option<(Symbol, Span)>),
658 UnexpectedCfgValue((Symbol, Span), Option<(Symbol, Span)>),
659 DocTestTakesList,
660 DocTestUnknown { name: Symbol },
661 DocTestLiteral,
662 AttrCrateLevelOnly,
663 DoNotRecommendDoesNotExpectArgs,
664 CrateTypeUnknown { span: Span, suggested: Option<Symbol> },
665 MalformedDoc,
666 ExpectedNoArgs,
667 ExpectedNameValue,
668 MalFormedDiagnosticAttribute { attribute: &'static str, options: &'static str, span: Span },
669 MalformedDiagnosticFormat { warning: FormatWarning },
670 DiagnosticWrappedParserError { description: String, label: String, span: Span },
671 IgnoredDiagnosticOption { option_name: Symbol, first_span: Span, later_span: Span },
672 MissingOptionsForDiagnosticAttribute { attribute: &'static str, options: &'static str },
673 NonMetaItemDiagnosticAttribute,
674}
675676#[derive(#[automatically_derived]
impl ::core::fmt::Debug for FormatWarning {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
FormatWarning::PositionalArgument { span: __self_0, help: __self_1
} =>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"PositionalArgument", "span", __self_0, "help", &__self_1),
FormatWarning::InvalidSpecifier { name: __self_0, span: __self_1 }
=>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"InvalidSpecifier", "name", __self_0, "span", &__self_1),
FormatWarning::DisallowedPlaceholder { span: __self_0 } =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"DisallowedPlaceholder", "span", &__self_0),
}
}
}Debug, #[automatically_derived]
impl ::core::clone::Clone for FormatWarning {
#[inline]
fn clone(&self) -> FormatWarning {
match self {
FormatWarning::PositionalArgument { span: __self_0, help: __self_1
} =>
FormatWarning::PositionalArgument {
span: ::core::clone::Clone::clone(__self_0),
help: ::core::clone::Clone::clone(__self_1),
},
FormatWarning::InvalidSpecifier { name: __self_0, span: __self_1 }
=>
FormatWarning::InvalidSpecifier {
name: ::core::clone::Clone::clone(__self_0),
span: ::core::clone::Clone::clone(__self_1),
},
FormatWarning::DisallowedPlaceholder { span: __self_0 } =>
FormatWarning::DisallowedPlaceholder {
span: ::core::clone::Clone::clone(__self_0),
},
}
}
}Clone, const _: () =
{
impl<__CTX> ::rustc_data_structures::stable_hasher::HashStable<__CTX>
for FormatWarning where __CTX: ::rustc_span::HashStableContext {
#[inline]
fn hash_stable(&self, __hcx: &mut __CTX,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
::std::mem::discriminant(self).hash_stable(__hcx, __hasher);
match *self {
FormatWarning::PositionalArgument {
span: ref __binding_0, help: ref __binding_1 } => {
{ __binding_0.hash_stable(__hcx, __hasher); }
{ __binding_1.hash_stable(__hcx, __hasher); }
}
FormatWarning::InvalidSpecifier {
name: ref __binding_0, span: ref __binding_1 } => {
{ __binding_0.hash_stable(__hcx, __hasher); }
{ __binding_1.hash_stable(__hcx, __hasher); }
}
FormatWarning::DisallowedPlaceholder { span: ref __binding_0
} => {
{ __binding_0.hash_stable(__hcx, __hasher); }
}
}
}
}
};HashStable_Generic)]
677pub enum FormatWarning {
678 PositionalArgument { span: Span, help: String },
679 InvalidSpecifier { name: String, span: Span },
680 DisallowedPlaceholder { span: Span },
681}
682683pub type RegisteredTools = FxIndexSet<Ident>;
684685/// Declares a static item of type `&'static Lint`.
686///
687/// See <https://rustc-dev-guide.rust-lang.org/diagnostics.html> for
688/// documentation and guidelines on writing lints.
689///
690/// The macro call should start with a doc comment explaining the lint
691/// which will be embedded in the rustc user documentation book. It should
692/// be written in markdown and have a format that looks like this:
693///
694/// ```rust,ignore (doc-example)
695/// /// The `my_lint_name` lint detects [short explanation here].
696/// ///
697/// /// ### Example
698/// ///
699/// /// ```rust
700/// /// [insert a concise example that triggers the lint]
701/// /// ```
702/// ///
703/// /// {{produces}}
704/// ///
705/// /// ### Explanation
706/// ///
707/// /// This should be a detailed explanation of *why* the lint exists,
708/// /// and also include suggestions on how the user should fix the problem.
709/// /// Try to keep the text simple enough that a beginner can understand,
710/// /// and include links to other documentation for terminology that a
711/// /// beginner may not be familiar with. If this is "allow" by default,
712/// /// it should explain why (are there false positives or other issues?). If
713/// /// this is a future-incompatible lint, it should say so, with text that
714/// /// looks roughly like this:
715/// ///
716/// /// This is a [future-incompatible] lint to transition this to a hard
717/// /// error in the future. See [issue #xxxxx] for more details.
718/// ///
719/// /// [issue #xxxxx]: https://github.com/rust-lang/rust/issues/xxxxx
720/// ```
721///
722/// The `{{produces}}` tag will be automatically replaced with the output from
723/// the example by the build system. If the lint example is too complex to run
724/// as a simple example (for example, it needs an extern crate), mark the code
725/// block with `ignore` and manually replace the `{{produces}}` line with the
726/// expected output in a `text` code block.
727///
728/// If this is a rustdoc-only lint, then only include a brief introduction
729/// with a link with the text `[rustdoc book]` so that the validator knows
730/// that this is for rustdoc only (see BROKEN_INTRA_DOC_LINKS as an example).
731///
732/// Commands to view and test the documentation:
733///
734/// * `./x.py doc --stage=1 src/doc/rustc --open`: Builds the rustc book and opens it.
735/// * `./x.py test src/tools/lint-docs`: Validates that the lint docs have the
736/// correct style, and that the code example actually emits the expected
737/// lint.
738///
739/// If you have already built the compiler, and you want to make changes to
740/// just the doc comments, then use the `--keep-stage=0` flag with the above
741/// commands to avoid rebuilding the compiler.
742#[macro_export]
743macro_rules!declare_lint {
744 ($(#[$attr:meta])* $vis: vis $NAME: ident, $Level: ident, $desc: expr) => (
745$crate::declare_lint!(
746 $(#[$attr])* $vis $NAME, $Level, $desc,
747 );
748 );
749 ($(#[$attr:meta])* $vis: vis $NAME: ident, $Level: ident, $desc: expr,
750 $(@eval_always = $eval_always:literal)?
751$(@feature_gate = $gate:ident;)?
752$(@future_incompatible = FutureIncompatibleInfo {
753 reason: $reason:expr,
754 $($field:ident : $val:expr),* $(,)*
755 }; )?
756$(@edition $lint_edition:ident => $edition_level:ident;)?
757$($v:ident),*) => (
758 $(#[$attr])*
759$vis static $NAME: &$crate::Lint = &$crate::Lint {
760 name: stringify!($NAME),
761 default_level: $crate::$Level,
762 desc: $desc,
763 is_externally_loaded: false,
764 $($v: true,)*
765 $(feature_gate: Some(rustc_span::sym::$gate),)?
766$(future_incompatible: Some($crate::FutureIncompatibleInfo {
767 reason: $reason,
768 $($field: $val,)*
769 ..$crate::FutureIncompatibleInfo::default_fields_for_macro()
770 }),)?
771$(edition_lint_opts: Some(($crate::Edition::$lint_edition, $crate::$edition_level)),)?
772$(eval_always: $eval_always,)?
773..$crate::Lint::default_fields_for_macro()
774 };
775 );
776}
777778#[macro_export]
779macro_rules!declare_tool_lint {
780 (
781 $(#[$attr:meta])* $vis:vis $tool:ident ::$NAME:ident, $Level: ident, $desc: expr
782 $(, @eval_always = $eval_always:literal)?
783$(, @feature_gate = $gate:ident;)?
784) => (
785$crate::declare_tool_lint!{$(#[$attr])* $vis $tool::$NAME, $Level, $desc, false $(, @eval_always = $eval_always)? $(, @feature_gate = $gate;)?}
786 );
787 (
788 $(#[$attr:meta])* $vis:vis $tool:ident ::$NAME:ident, $Level:ident, $desc:expr,
789 report_in_external_macro: $rep:expr
790 $(, @eval_always = $eval_always: literal)?
791$(, @feature_gate = $gate:ident;)?
792) => (
793$crate::declare_tool_lint!{$(#[$attr])* $vis $tool::$NAME, $Level, $desc, $rep $(, @eval_always = $eval_always)? $(, @feature_gate = $gate;)?}
794 );
795 (
796 $(#[$attr:meta])* $vis:vis $tool:ident ::$NAME:ident, $Level:ident, $desc:expr,
797$external:expr
798 $(, @eval_always = $eval_always: literal)?
799$(, @feature_gate = $gate:ident;)?
800) => (
801 $(#[$attr])*
802$vis static $NAME: &$crate::Lint = &$crate::Lint {
803 name: &concat!(stringify!($tool), "::", stringify!($NAME)),
804 default_level: $crate::$Level,
805 desc: $desc,
806 edition_lint_opts: None,
807 report_in_external_macro: $external,
808 future_incompatible: None,
809 is_externally_loaded: true,
810 $(feature_gate: Some(rustc_span::sym::$gate),)?
811crate_level_only: false,
812 $(eval_always: $eval_always,)?
813..$crate::Lint::default_fields_for_macro()
814 };
815 );
816}
817818pub type LintVec = Vec<&'static Lint>;
819820pub trait LintPass {
821fn name(&self) -> &'static str;
822fn get_lints(&self) -> LintVec;
823}
824825/// Implements `LintPass for $ty` with the given list of `Lint` statics.
826#[macro_export]
827macro_rules!impl_lint_pass {
828 ($ty:ty => [$($lint:expr),* $(,)?]) => {
829impl $crate::LintPass for $ty {
830fn name(&self) -> &'static str { stringify!($ty) }
831fn get_lints(&self) -> $crate::LintVec { vec![$($lint),*] }
832 }
833impl $ty {
834#[allow(unused)]
835pub fn lint_vec() -> $crate::LintVec { vec![$($lint),*] }
836 }
837 };
838}
839840/// Declares a type named `$name` which implements `LintPass`.
841/// To the right of `=>` a comma separated list of `Lint` statics is given.
842#[macro_export]
843macro_rules!declare_lint_pass {
844 ($(#[$m:meta])* $name:ident => [$($lint:expr),* $(,)?]) => {
845 $(#[$m])* #[derive(Copy, Clone)] pub struct $name;
846$crate::impl_lint_pass!($name => [$($lint),*]);
847 };
848}
849850#[macro_export]
851macro_rules!fcw {
852 (FutureReleaseError # $issue_number: literal) => {
853$crate:: FutureIncompatibilityReason::FutureReleaseError($crate::ReleaseFcw { issue_number: $issue_number })
854 };
855 (FutureReleaseSemanticsChange # $issue_number: literal) => {
856$crate::FutureIncompatibilityReason::FutureReleaseSemanticsChange($crate::ReleaseFcw {
857 issue_number: $issue_number,
858 })
859 };
860 ($description: literal # $issue_number: literal) => {
861$crate::FutureIncompatibilityReason::Custom($description, $crate::ReleaseFcw {
862 issue_number: $issue_number,
863 })
864 };
865 (EditionError $edition_name: tt $page_slug: literal) => {
866$crate::FutureIncompatibilityReason::EditionError($crate::EditionFcw {
867 edition: fcw!(@edition $edition_name),
868 page_slug: $page_slug,
869 })
870 };
871 (EditionSemanticsChange $edition_name: tt $page_slug: literal) => {
872$crate::FutureIncompatibilityReason::EditionSemanticsChange($crate::EditionFcw {
873 edition: fcw!(@edition $edition_name),
874 page_slug: $page_slug,
875 })
876 };
877 (EditionAndFutureReleaseSemanticsChange $edition_name: tt $page_slug: literal) => {
878$crate::FutureIncompatibilityReason::EditionAndFutureReleaseSemanticsChange($crate::EditionFcw {
879 edition: fcw!(@edition $edition_name),
880 page_slug: $page_slug,
881 })
882 };
883 (EditionAndFutureReleaseError $edition_name: tt $page_slug: literal) => {
884$crate::FutureIncompatibilityReason::EditionAndFutureReleaseError($crate::EditionFcw {
885 edition: fcw!(@edition $edition_name),
886 page_slug: $page_slug,
887 })
888 };
889 (@edition 2024) => {
890 rustc_span::edition::Edition::Edition2024
891 };
892 (@edition 2021) => {
893 rustc_span::edition::Edition::Edition2021
894 };
895 (@edition 2018) => {
896 rustc_span::edition::Edition::Edition2018
897 };
898}