Skip to main content

rustc_lint_defs/
lint_name.rs

1use std::fmt;
2
3use rustc_macros::{Decodable, Encodable, StableHash};
4use rustc_span::{Symbol, sym};
5
6#[derive(#[automatically_derived]impl ::core::marker::Copy for LintName { }Copy, #[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
    for LintName {
}
#[automatically_derived]impl ::core::clone::Clone for LintName {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        let _: ::core::clone::AssertParamIsClone<Option<Symbol>>;
        let _: ::core::clone::AssertParamIsClone<Symbol>;
        *self
    }
}Clone, #[automatically_derived]impl ::core::fmt::Debug for LintName {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f,
            "LintName tool name", &self.tool, &&self.name)
    }
}Debug, #[automatically_derived]impl ::core::cmp::Eq for LintName {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<Option<Symbol>>;
        let _: ::core::cmp::AssertParamIsEq<Symbol>;
    }
}Eq, #[automatically_derived]impl ::core::hash::Hash for LintName {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        ::core::hash::Hash::hash(&self.tool, state);
        ::core::hash::Hash::hash(&self.name, state)
    }
}Hash, #[automatically_derived]impl ::core::marker::StructuralPartialEq for LintName
    {
}
#[automatically_derived]impl ::core::cmp::PartialEq for LintName {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.tool == other.tool && self.name == other.name
    }
}PartialEq, #[automatically_derived]impl ::core::cmp::Ord for LintName {
    #[inline]
    fn cmp(&self, other: &Self) -> ::core::cmp::Ordering {
        match ::core::cmp::Ord::cmp(&self.tool, &other.tool) {
            ::core::cmp::Ordering::Equal =>
                ::core::cmp::Ord::cmp(&self.name, &other.name),
            cmp => cmp,
        }
    }
}Ord, #[automatically_derived]impl ::core::cmp::PartialOrd for LintName {
    #[inline]
    fn partial_cmp(&self, other: &Self)
        -> ::core::option::Option<::core::cmp::Ordering> {
        ::core::option::Option::Some(::core::cmp::Ord::cmp(self, other))
    }
}PartialOrd)]
7#[derive(const _: () =
    {
        impl ::rustc_data_structures::stable_hash::StableHash for LintName {
            #[inline]
            fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
                __hcx: &mut __Hcx,
                __hasher:
                    &mut ::rustc_data_structures::stable_hash::StableHasher) {
                match *self {
                    LintName { tool: ref __binding_0, name: ref __binding_1 } =>
                        {
                        { __binding_0.stable_hash(__hcx, __hasher); }
                        { __binding_1.stable_hash(__hcx, __hasher); }
                    }
                }
            }
        }
    };StableHash, const _: () =
    {
        impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
            for LintName {
            fn encode(&self, __encoder: &mut __E) {
                let LintName { tool: ref __binding_0, name: ref __binding_1
                        } = *self;
                ::rustc_serialize::Encodable::<__E>::encode(__binding_0,
                    __encoder);
                ::rustc_serialize::Encodable::<__E>::encode(__binding_1,
                    __encoder);
            }
        }
    };Encodable, const _: () =
    {
        impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
            for LintName {
            fn decode(__decoder: &mut __D) -> Self {
                LintName {
                    tool: ::rustc_serialize::Decodable::decode(__decoder),
                    name: ::rustc_serialize::Decodable::decode(__decoder),
                }
            }
        }
    };Decodable)]
8pub struct LintName {
9    /// The lint's tool name, if present.
10    pub tool: Option<Symbol>,
11    /// The lint's name, minus the tool name if any.
12    ///
13    /// For example:
14    /// - `clippy::blah` -> `blah`
15    /// - `unstable_imports` -> `unstable_imports`
16    /// - `custom_tool::blah::bleh` -> `blah::bleh`
17    pub name: Symbol,
18}
19
20impl LintName {
21    pub fn new(segments: impl Iterator<Item = Symbol>) -> LintName {
22        let segments = segments.collect::<Vec<_>>();
23        let (tool, name) = match &*segments {
24            [] => ::core::panicking::unreachable_default_msg()unreachable!(),
25            [lint_name] => (None, *lint_name),
26            [tool, lint_name] => (Some(*tool), *lint_name),
27            // Not reachable for any builtin lints,
28            // but reachable using `register_tool`.
29            [tool, lint_name, rest @ ..] => {
30                let mut lint_name = String::from(lint_name.as_str());
31                for seg in rest {
32                    lint_name.push_str("::");
33                    lint_name.push_str(seg.as_str());
34                }
35                (Some(*tool), Symbol::intern(&lint_name))
36            }
37        };
38        LintName { tool, name }
39    }
40
41    /// `lint` must not be empty.
42    pub fn from_str(lint: &str) -> LintName {
43        if lint.is_empty() {
44            if truecfg!(debug_assertions) {
45                ::rustc_span::macros::bug_impl(None, format_args!("`lint` is empty"),
    Location::caller())rustc_span::bug!("`lint` is empty")
46            } else {
47                return LintName { tool: None, name: sym::empty };
48            }
49        }
50        if let Some((tool, name)) = lint.split_once("::") {
51            LintName { tool: Some(Symbol::intern(tool)), name: Symbol::intern(name) }
52        } else {
53            LintName { tool: None, name: Symbol::intern(lint) }
54        }
55    }
56
57    pub fn to_lowercase(mut self) -> LintName {
58        fn lowercase(s: &mut Symbol) {
59            let lower = s.as_str().to_lowercase();
60            *s = Symbol::intern(&lower);
61        }
62        if let Some(tool) = &mut self.tool {
63            lowercase(tool);
64        }
65        lowercase(&mut self.name);
66        self
67    }
68
69    pub fn is(&self, lint: Symbol) -> bool {
70        lint == self.name && self.tool.is_none()
71    }
72}
73
74impl fmt::Display for LintName {
75    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76        if let Some(tool) = self.tool {
77            f.write_fmt(format_args!("{0}::", tool))write!(f, "{tool}::")?
78        }
79        f.write_fmt(format_args!("{0}", self.name))write!(f, "{}", self.name)?;
80        Ok(())
81    }
82}
83
84impl rustc_error_messages::IntoDiagArg for LintName {
85    fn into_diag_arg(
86        self,
87        _: &mut Option<std::path::PathBuf>,
88    ) -> rustc_error_messages::DiagArgValue {
89        self.to_string().into_diag_arg(&mut None)
90    }
91}