Skip to main content

rustc_lint/
nonstandard_style.rs

1use rustc_abi::ExternAbi;
2use rustc_ast as ast;
3use rustc_attr_parsing::AttributeParser;
4use rustc_errors::{Applicability, Diag, DiagCtxtHandle, Diagnostic, Level};
5use rustc_hir as hir;
6use rustc_hir::attrs::{AttributeKind, ReprAttr};
7use rustc_hir::def::{DefKind, Res};
8use rustc_hir::def_id::DefId;
9use rustc_hir::intravisit::{FnKind, Visitor};
10use rustc_hir::{Attribute, GenericParamKind, PatExprKind, PatKind, find_attr};
11use rustc_middle::hir::nested_filter::All;
12use rustc_middle::ty::AssocContainer;
13use rustc_session::config::CrateType;
14use rustc_session::{declare_lint, declare_lint_pass};
15use rustc_span::def_id::LocalDefId;
16use rustc_span::{BytePos, Ident, Span, sym};
17
18use crate::lints::{
19    NonCamelCaseType, NonCamelCaseTypeSub, NonSnakeCaseDiag, NonSnakeCaseDiagSub,
20    NonUpperCaseGlobal, NonUpperCaseGlobalSub, NonUpperCaseGlobalSubTool,
21};
22use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
23
24#[doc =
r" The `non_camel_case_types` lint detects types, variants, traits and"]
#[doc = r" type parameters that don't have camel case names."]
#[doc = r""]
#[doc = r" ### Example"]
#[doc = r""]
#[doc = r" ```rust"]
#[doc = r" struct my_struct;"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" {{produces}}"]
#[doc = r""]
#[doc = r" ### Explanation"]
#[doc = r""]
#[doc =
r#" The preferred style for these identifiers is to use "camel case", such"#]
#[doc =
r" as `MyStruct`, where the first letter should not be lowercase, and"]
#[doc =
r" should not use underscores between letters. Underscores are allowed at"]
#[doc = r" the beginning and end of the identifier, as well as between"]
#[doc = r" non-letters (such as `X86_64`)."]
pub static NON_CAMEL_CASE_TYPES: &::rustc_lint_defs::Lint =
    &::rustc_lint_defs::Lint {
            name: "NON_CAMEL_CASE_TYPES",
            default_level: ::rustc_lint_defs::Warn,
            desc: "types, variants, traits and type parameters should have camel case names",
            is_externally_loaded: false,
            ..::rustc_lint_defs::Lint::default_fields_for_macro()
        };declare_lint! {
25    /// The `non_camel_case_types` lint detects types, variants, traits and
26    /// type parameters that don't have camel case names.
27    ///
28    /// ### Example
29    ///
30    /// ```rust
31    /// struct my_struct;
32    /// ```
33    ///
34    /// {{produces}}
35    ///
36    /// ### Explanation
37    ///
38    /// The preferred style for these identifiers is to use "camel case", such
39    /// as `MyStruct`, where the first letter should not be lowercase, and
40    /// should not use underscores between letters. Underscores are allowed at
41    /// the beginning and end of the identifier, as well as between
42    /// non-letters (such as `X86_64`).
43    pub NON_CAMEL_CASE_TYPES,
44    Warn,
45    "types, variants, traits and type parameters should have camel case names"
46}
47
48pub struct NonCamelCaseTypes;
#[automatically_derived]
impl ::core::marker::Copy for NonCamelCaseTypes { }
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for NonCamelCaseTypes { }
#[automatically_derived]
impl ::core::clone::Clone for NonCamelCaseTypes {
    #[inline]
    fn clone(&self) -> NonCamelCaseTypes { *self }
}
impl ::rustc_lint_defs::LintPass for NonCamelCaseTypes {
    fn name(&self) -> &'static str { "NonCamelCaseTypes" }
    fn get_lints(&self) -> ::rustc_lint_defs::LintVec {
        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                [NON_CAMEL_CASE_TYPES]))
    }
}
impl NonCamelCaseTypes {
    #[allow(unused)]
    pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                [NON_CAMEL_CASE_TYPES]))
    }
}declare_lint_pass!(NonCamelCaseTypes => [NON_CAMEL_CASE_TYPES]);
49
50/// Some unicode characters *have* case, are considered upper, title, or lower case, but they *can't*
51/// be title cased or lower cased. For the purposes of the lint suggestion, we care about being able
52/// to change the char's case.
53fn char_has_case(c: char) -> bool {
54    !c.to_lowercase().eq(c.to_titlecase())
55}
56
57/// FIXME: we should add a more efficient version
58/// in the stdlib for this
59fn changes_when_titlecased(c: char) -> bool {
60    !c.to_titlecase().eq([c])
61}
62
63// contains a capitalisable character followed by, or preceded by, an underscore,
64// or contains an uppercase character that changes when titlecased,
65// or contains `__`
66fn not_camel_case(s: &str) -> bool {
67    let mut last = '\0';
68    s.chars().any(|snd| {
69        let fst = std::mem::replace(&mut last, snd);
70        match (fst, snd) {
71            ('_', '_') => return true,
72            ('_', _) if char_has_case(snd) => return true,
73            (_, '_') if char_has_case(fst) => return true,
74            _ => snd.is_uppercase() && changes_when_titlecased(snd),
75        }
76    })
77}
78
79fn is_upper_camel_case(name: &str) -> bool {
80    let name = name.trim_matches('_');
81    let Some(first) = name.chars().next() else {
82        return true;
83    };
84
85    // some scripts don't have a concept of upper/lowercase
86    !(changes_when_titlecased(first) || not_camel_case(name))
87}
88
89fn to_upper_camel_case(s: &str) -> String {
90    s.trim_matches('_')
91        .split('_')
92        .filter(|component| !component.is_empty())
93        .map(|component| {
94            let mut camel_cased_component = String::new();
95
96            let mut new_word = true;
97            let mut prev_is_lower_case = true;
98            let mut prev_is_lowercased_sigma = false;
99
100            for c in component.chars() {
101                // Preserve the case if an uppercase letter follows a lowercase letter, so that
102                // `camelCase` is converted to `CamelCase`.
103                if prev_is_lower_case && (c.is_uppercase() | c.is_titlecase()) {
104                    new_word = true;
105                }
106
107                if new_word {
108                    camel_cased_component.extend(c.to_titlecase());
109                } else {
110                    camel_cased_component.extend(c.to_lowercase());
111                }
112
113                prev_is_lower_case = c.is_lowercase() || c.is_titlecase();
114                prev_is_lowercased_sigma = !new_word && c == 'Σ';
115                new_word = false;
116            }
117
118            if prev_is_lowercased_sigma {
119                camel_cased_component.pop();
120                camel_cased_component.push('ς');
121            }
122
123            camel_cased_component
124        })
125        .fold((String::new(), None), |(acc, prev): (String, Option<String>), next| {
126            // separate two components with an underscore if their boundary cannot
127            // be distinguished using an uppercase/lowercase case distinction
128            let join = if let Some(prev) = prev {
129                let l = prev.chars().last().unwrap();
130                let f = next.chars().next().unwrap();
131                !char_has_case(l) && !char_has_case(f)
132            } else {
133                false
134            };
135            (acc + if join { "_" } else { "" } + &next, Some(next))
136        })
137        .0
138}
139
140impl NonCamelCaseTypes {
141    fn check_case(&self, cx: &EarlyContext<'_>, sort: &str, ident: &Ident) {
142        let name = ident.name.as_str();
143
144        if !is_upper_camel_case(name) {
145            let cc = to_upper_camel_case(name);
146            let sub = if *name != cc {
147                NonCamelCaseTypeSub::Suggestion { span: ident.span, replace: cc }
148            } else {
149                NonCamelCaseTypeSub::Label { span: ident.span }
150            };
151            cx.emit_span_lint(
152                NON_CAMEL_CASE_TYPES,
153                ident.span,
154                NonCamelCaseType { sort, name, sub },
155            );
156        }
157    }
158}
159
160impl EarlyLintPass for NonCamelCaseTypes {
161    fn check_item(&mut self, cx: &EarlyContext<'_>, it: &ast::Item) {
162        let has_repr_c = #[allow(non_exhaustive_omitted_patterns)] match AttributeParser::parse_limited(cx.sess(),
        &it.attrs, &[sym::repr]) {
    Some(Attribute::Parsed(AttributeKind::Repr { reprs, .. })) if
        reprs.iter().any(|(r, _)| r == &ReprAttr::ReprC) => true,
    _ => false,
}matches!(
163            AttributeParser::parse_limited(cx.sess(), &it.attrs, &[sym::repr]),
164            Some(Attribute::Parsed(AttributeKind::Repr { reprs, ..})) if reprs.iter().any(|(r, _)| r == &ReprAttr::ReprC)
165        );
166
167        if has_repr_c {
168            return;
169        }
170
171        match &it.kind {
172            ast::ItemKind::TyAlias(ast::TyAlias { ident, .. })
173            | ast::ItemKind::Enum(ident, ..)
174            | ast::ItemKind::Struct(ident, ..)
175            | ast::ItemKind::Union(ident, ..) => self.check_case(cx, "type", ident),
176            ast::ItemKind::Trait(ast::Trait { ident, .. }) => self.check_case(cx, "trait", ident),
177            ast::ItemKind::TraitAlias(ast::TraitAlias { ident, .. }) => {
178                self.check_case(cx, "trait alias", ident)
179            }
180
181            // N.B. This check is only for inherent associated types, so that we don't lint against
182            // trait impls where we should have warned for the trait definition already.
183            ast::ItemKind::Impl(ast::Impl { of_trait: None, items, .. }) => {
184                for it in items {
185                    // FIXME: this doesn't respect `#[allow(..)]` on the item itself.
186                    if let ast::AssocItemKind::Type(alias) = &it.kind {
187                        self.check_case(cx, "associated type", &alias.ident);
188                    }
189                }
190            }
191            _ => (),
192        }
193    }
194
195    fn check_trait_item(&mut self, cx: &EarlyContext<'_>, it: &ast::AssocItem) {
196        if let ast::AssocItemKind::Type(alias) = &it.kind {
197            self.check_case(cx, "associated type", &alias.ident);
198        }
199    }
200
201    fn check_variant(&mut self, cx: &EarlyContext<'_>, v: &ast::Variant) {
202        self.check_case(cx, "variant", &v.ident);
203    }
204
205    fn check_generic_param(&mut self, cx: &EarlyContext<'_>, param: &ast::GenericParam) {
206        if let ast::GenericParamKind::Type { .. } = param.kind {
207            self.check_case(cx, "type parameter", &param.ident);
208        }
209    }
210}
211
212#[doc = r" The `non_snake_case` lint detects variables, methods, functions,"]
#[doc = r" lifetime parameters and modules that don't have snake case names."]
#[doc = r""]
#[doc = r" ### Example"]
#[doc = r""]
#[doc = r" ```rust"]
#[doc = r" let MY_VALUE = 5;"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" {{produces}}"]
#[doc = r""]
#[doc = r" ### Explanation"]
#[doc = r""]
#[doc =
r#" The preferred style for these identifiers is to use "snake case","#]
#[doc =
r" where all the characters are in lowercase, with words separated with a"]
#[doc = r" single underscore, such as `my_value`."]
pub static NON_SNAKE_CASE: &::rustc_lint_defs::Lint =
    &::rustc_lint_defs::Lint {
            name: "NON_SNAKE_CASE",
            default_level: ::rustc_lint_defs::Warn,
            desc: "variables, methods, functions, lifetime parameters and modules should have snake case names",
            is_externally_loaded: false,
            ..::rustc_lint_defs::Lint::default_fields_for_macro()
        };declare_lint! {
213    /// The `non_snake_case` lint detects variables, methods, functions,
214    /// lifetime parameters and modules that don't have snake case names.
215    ///
216    /// ### Example
217    ///
218    /// ```rust
219    /// let MY_VALUE = 5;
220    /// ```
221    ///
222    /// {{produces}}
223    ///
224    /// ### Explanation
225    ///
226    /// The preferred style for these identifiers is to use "snake case",
227    /// where all the characters are in lowercase, with words separated with a
228    /// single underscore, such as `my_value`.
229    pub NON_SNAKE_CASE,
230    Warn,
231    "variables, methods, functions, lifetime parameters and modules should have snake case names"
232}
233
234pub struct NonSnakeCase;
#[automatically_derived]
impl ::core::marker::Copy for NonSnakeCase { }
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for NonSnakeCase { }
#[automatically_derived]
impl ::core::clone::Clone for NonSnakeCase {
    #[inline]
    fn clone(&self) -> NonSnakeCase { *self }
}
impl ::rustc_lint_defs::LintPass for NonSnakeCase {
    fn name(&self) -> &'static str { "NonSnakeCase" }
    fn get_lints(&self) -> ::rustc_lint_defs::LintVec {
        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                [NON_SNAKE_CASE]))
    }
}
impl NonSnakeCase {
    #[allow(unused)]
    pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                [NON_SNAKE_CASE]))
    }
}declare_lint_pass!(NonSnakeCase => [NON_SNAKE_CASE]);
235
236impl NonSnakeCase {
237    fn to_snake_case(mut name: &str) -> String {
238        let mut words = ::alloc::vec::Vec::new()vec![];
239        // Preserve leading underscores
240        name = name.trim_start_matches(|c: char| {
241            if c == '_' {
242                words.push(String::new());
243                true
244            } else {
245                false
246            }
247        });
248        for s in name.split('_') {
249            let mut last_upper = false;
250            let mut buf = String::new();
251            if s.is_empty() {
252                continue;
253            }
254            for ch in s.chars() {
255                if !buf.is_empty()
256                    && buf != "'"
257                    && (ch.is_uppercase() || ch.is_titlecase())
258                    && !last_upper
259                {
260                    // We lowercase only at the end, to handle final sigma correctly
261                    words.push(buf.to_lowercase());
262                    buf = String::new();
263                }
264                last_upper = ch.is_uppercase() || ch.is_titlecase();
265                buf.push(ch);
266            }
267            // We lowercase only at the end, to handle final sigma correctly
268            words.push(buf.to_lowercase());
269        }
270        words.join("_")
271    }
272
273    /// Checks if a given identifier is snake case, and reports a diagnostic if not.
274    fn check_snake_case(&self, cx: &LateContext<'_>, sort: &str, ident: &Ident) {
275        fn is_snake_case(ident: &str) -> bool {
276            if ident.is_empty() {
277                return true;
278            }
279            let ident = ident.trim_start_matches('\'');
280            let ident = ident.trim_matches('_');
281
282            if ident.contains("__") {
283                return false;
284            }
285
286            // This correctly handles letters in languages with and without
287            // cases, as well as numbers and underscores.
288            // FIXME: we should add a standard library impl of `c.to_lowercase().eq([c])`
289            ident.chars().all(|c| c.to_lowercase().eq([c]))
290        }
291
292        let name = ident.name.as_str();
293
294        if !is_snake_case(name) {
295            let span = ident.span;
296            let sc = NonSnakeCase::to_snake_case(name);
297            // We cannot provide meaningful suggestions
298            // if the characters are in the category of "Uppercase Letter".
299            let sub = if name != sc {
300                // We have a valid span in almost all cases, but we don't have one when linting a
301                // crate name provided via the command line.
302                if !span.is_dummy() {
303                    let sc_ident = Ident::from_str_and_span(&sc, span);
304                    if sc_ident.is_reserved() {
305                        // We shouldn't suggest a reserved identifier to fix non-snake-case
306                        // identifiers. Instead, recommend renaming the identifier entirely or, if
307                        // permitted, escaping it to create a raw identifier.
308                        if sc_ident.name.can_be_raw() {
309                            NonSnakeCaseDiagSub::RenameOrConvertSuggestion {
310                                span,
311                                suggestion: sc_ident,
312                            }
313                        } else {
314                            NonSnakeCaseDiagSub::SuggestionAndNote { span }
315                        }
316                    } else {
317                        NonSnakeCaseDiagSub::ConvertSuggestion { span, suggestion: sc.clone() }
318                    }
319                } else {
320                    NonSnakeCaseDiagSub::Help
321                }
322            } else {
323                NonSnakeCaseDiagSub::Label { span }
324            };
325            cx.emit_span_lint(NON_SNAKE_CASE, span, NonSnakeCaseDiag { sort, name, sc, sub });
326        }
327    }
328}
329
330impl<'tcx> LateLintPass<'tcx> for NonSnakeCase {
331    fn check_mod(&mut self, cx: &LateContext<'_>, _: &'tcx hir::Mod<'tcx>, id: hir::HirId) {
332        if id != hir::CRATE_HIR_ID {
333            return;
334        }
335
336        // Issue #45127: don't enforce `snake_case` for binary crates as binaries are not intended
337        // to be distributed and depended on like libraries. The lint is not suppressed for cdylib
338        // or staticlib because it's not clear what the desired lint behavior for those are.
339        if cx.tcx.crate_types().iter().all(|&crate_type| crate_type == CrateType::Executable) {
340            return;
341        }
342
343        let crate_ident = if let Some(name) = &cx.tcx.sess.opts.crate_name {
344            Some(Ident::from_str(name))
345        } else {
346            {
    'done:
        {
        for i in cx.tcx.hir_krate_attrs() {
            #[allow(unused_imports)]
            use rustc_hir::attrs::AttributeKind::*;
            let i: &rustc_hir::Attribute = i;
            match i {
                rustc_hir::Attribute::Parsed(CrateName { name, name_span, ..
                    }) => {
                    break 'done Some((name, name_span));
                }
                rustc_hir::Attribute::Unparsed(..) =>
                    {}
                    #[deny(unreachable_patterns)]
                    _ => {}
            }
        }
        None
    }
}find_attr!(cx.tcx, crate, CrateName{name, name_span,..} => (name, name_span)).map(
347                |(&name, &span)| {
348                    // Discard the double quotes surrounding the literal.
349                    let sp = cx
350                        .sess()
351                        .source_map()
352                        .span_to_snippet(span)
353                        .ok()
354                        .and_then(|snippet| {
355                            let left = snippet.find('"')?;
356                            let right = snippet.rfind('"').map(|pos| snippet.len() - pos)?;
357
358                            Some(
359                                span.with_lo(span.lo() + BytePos(left as u32 + 1))
360                                    .with_hi(span.hi() - BytePos(right as u32)),
361                            )
362                        })
363                        .unwrap_or(span);
364
365                    Ident::new(name, sp)
366                },
367            )
368        };
369
370        if let Some(ident) = &crate_ident {
371            self.check_snake_case(cx, "crate", ident);
372        }
373    }
374
375    fn check_generic_param(&mut self, cx: &LateContext<'_>, param: &hir::GenericParam<'_>) {
376        if let GenericParamKind::Lifetime { .. } = param.kind {
377            self.check_snake_case(cx, "lifetime", &param.name.ident());
378        }
379    }
380
381    fn check_fn(
382        &mut self,
383        cx: &LateContext<'_>,
384        fk: FnKind<'_>,
385        _: &hir::FnDecl<'_>,
386        _: &hir::Body<'_>,
387        _: Span,
388        id: LocalDefId,
389    ) {
390        match &fk {
391            FnKind::Method(ident, sig, ..) => match cx.tcx.associated_item(id).container {
392                AssocContainer::InherentImpl => {
393                    if sig.header.abi != ExternAbi::Rust && {
        {
            'done:
                {
                for i in ::rustc_hir::attrs::HasAttrs::get_attrs(id, &cx.tcx)
                    {
                    #[allow(unused_imports)]
                    use rustc_hir::attrs::AttributeKind::*;
                    let i: &rustc_hir::Attribute = i;
                    match i {
                        rustc_hir::Attribute::Parsed(NoMangle(..)) => {
                            break 'done Some(());
                        }
                        rustc_hir::Attribute::Unparsed(..) =>
                            {}
                            #[deny(unreachable_patterns)]
                            _ => {}
                    }
                }
                None
            }
        }
    }.is_some()find_attr!(cx.tcx, id, NoMangle(..)) {
394                        return;
395                    }
396                    self.check_snake_case(cx, "method", ident);
397                }
398                AssocContainer::Trait => {
399                    self.check_snake_case(cx, "trait method", ident);
400                }
401                AssocContainer::TraitImpl(_) => {}
402            },
403            FnKind::ItemFn(ident, _, header) => {
404                // Skip foreign-ABI #[no_mangle] functions (Issue #31924)
405                if header.abi != ExternAbi::Rust && {
        {
            'done:
                {
                for i in ::rustc_hir::attrs::HasAttrs::get_attrs(id, &cx.tcx)
                    {
                    #[allow(unused_imports)]
                    use rustc_hir::attrs::AttributeKind::*;
                    let i: &rustc_hir::Attribute = i;
                    match i {
                        rustc_hir::Attribute::Parsed(NoMangle(..)) => {
                            break 'done Some(());
                        }
                        rustc_hir::Attribute::Unparsed(..) =>
                            {}
                            #[deny(unreachable_patterns)]
                            _ => {}
                    }
                }
                None
            }
        }
    }.is_some()find_attr!(cx.tcx, id, NoMangle(..)) {
406                    return;
407                }
408                self.check_snake_case(cx, "function", ident);
409            }
410            FnKind::Closure => (),
411        }
412    }
413
414    fn check_item(&mut self, cx: &LateContext<'_>, it: &hir::Item<'_>) {
415        if let hir::ItemKind::Mod(ident, _) = it.kind {
416            self.check_snake_case(cx, "module", &ident);
417        }
418    }
419
420    fn check_ty(&mut self, cx: &LateContext<'_>, ty: &hir::Ty<'_, hir::AmbigArg>) {
421        if let hir::TyKind::FnPtr(hir::FnPtrTy { param_idents, .. }) = &ty.kind {
422            for param_ident in *param_idents {
423                if let Some(param_ident) = param_ident {
424                    self.check_snake_case(cx, "variable", param_ident);
425                }
426            }
427        }
428    }
429
430    fn check_trait_item(&mut self, cx: &LateContext<'_>, item: &hir::TraitItem<'_>) {
431        if let hir::TraitItemKind::Fn(_, hir::TraitFn::Required(param_idents)) = item.kind {
432            self.check_snake_case(cx, "trait method", &item.ident);
433            for param_ident in param_idents {
434                if let Some(param_ident) = param_ident {
435                    self.check_snake_case(cx, "variable", param_ident);
436                }
437            }
438        }
439    }
440
441    fn check_pat(&mut self, cx: &LateContext<'_>, p: &hir::Pat<'_>) {
442        if let PatKind::Binding(_, hid, ident, _) = p.kind {
443            if let hir::Node::PatField(field) = cx.tcx.parent_hir_node(hid) {
444                if !field.is_shorthand {
445                    // Only check if a new name has been introduced, to avoid warning
446                    // on both the struct definition and this pattern.
447                    self.check_snake_case(cx, "variable", &ident);
448                }
449                return;
450            }
451            self.check_snake_case(cx, "variable", &ident);
452        }
453    }
454
455    fn check_struct_def(&mut self, cx: &LateContext<'_>, s: &hir::VariantData<'_>) {
456        for sf in s.fields() {
457            self.check_snake_case(cx, "structure field", &sf.ident);
458        }
459    }
460}
461
462#[doc =
r" The `non_upper_case_globals` lint detects static items that don't have"]
#[doc = r" uppercase identifiers."]
#[doc = r""]
#[doc = r" ### Example"]
#[doc = r""]
#[doc = r" ```rust"]
#[doc = r" static max_points: i32 = 5;"]
#[doc = r" ```"]
#[doc = r""]
#[doc = r" {{produces}}"]
#[doc = r""]
#[doc = r" ### Explanation"]
#[doc = r""]
#[doc = r" The preferred style is for static item names to use all uppercase"]
#[doc = r" letters such as `MAX_POINTS`."]
pub static NON_UPPER_CASE_GLOBALS: &::rustc_lint_defs::Lint =
    &::rustc_lint_defs::Lint {
            name: "NON_UPPER_CASE_GLOBALS",
            default_level: ::rustc_lint_defs::Warn,
            desc: "static constants should have uppercase identifiers",
            is_externally_loaded: false,
            ..::rustc_lint_defs::Lint::default_fields_for_macro()
        };declare_lint! {
463    /// The `non_upper_case_globals` lint detects static items that don't have
464    /// uppercase identifiers.
465    ///
466    /// ### Example
467    ///
468    /// ```rust
469    /// static max_points: i32 = 5;
470    /// ```
471    ///
472    /// {{produces}}
473    ///
474    /// ### Explanation
475    ///
476    /// The preferred style is for static item names to use all uppercase
477    /// letters such as `MAX_POINTS`.
478    pub NON_UPPER_CASE_GLOBALS,
479    Warn,
480    "static constants should have uppercase identifiers"
481}
482
483pub struct NonUpperCaseGlobals;
#[automatically_derived]
impl ::core::marker::Copy for NonUpperCaseGlobals { }
#[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for NonUpperCaseGlobals { }
#[automatically_derived]
impl ::core::clone::Clone for NonUpperCaseGlobals {
    #[inline]
    fn clone(&self) -> NonUpperCaseGlobals { *self }
}
impl ::rustc_lint_defs::LintPass for NonUpperCaseGlobals {
    fn name(&self) -> &'static str { "NonUpperCaseGlobals" }
    fn get_lints(&self) -> ::rustc_lint_defs::LintVec {
        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                [NON_UPPER_CASE_GLOBALS]))
    }
}
impl NonUpperCaseGlobals {
    #[allow(unused)]
    pub fn lint_vec() -> ::rustc_lint_defs::LintVec {
        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                [NON_UPPER_CASE_GLOBALS]))
    }
}declare_lint_pass!(NonUpperCaseGlobals => [NON_UPPER_CASE_GLOBALS]);
484
485struct NonUpperCaseGlobalGenerator<'a, F: FnOnce() -> NonUpperCaseGlobal<'a>> {
486    callback: F,
487}
488
489impl<'a, 'b, F: FnOnce() -> NonUpperCaseGlobal<'b>> Diagnostic<'a, ()>
490    for NonUpperCaseGlobalGenerator<'b, F>
491{
492    fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
493        let Self { callback } = self;
494        callback().into_diag(dcx, level)
495    }
496}
497
498impl NonUpperCaseGlobals {
499    fn check_upper_case(cx: &LateContext<'_>, sort: &str, did: Option<LocalDefId>, ident: &Ident) {
500        let name = ident.name.as_str();
501        // FIXME: we should add a more efficient version
502        // in the stdlib for `c.to_uppercase().eq([c])`
503        if !name.chars().all(|c| c.to_uppercase().eq([c])) {
504            let uc = NonSnakeCase::to_snake_case(name).to_uppercase();
505
506            // If the item is exported, suggesting changing its name would be a breaking change
507            // and could break users without a "nice" applicable fix, so let's avoid it.
508            let can_change_usages = if let Some(did) = did {
509                !cx.tcx.effective_visibilities(()).is_exported(did)
510            } else {
511                false
512            };
513
514            // We cannot provide meaningful suggestions
515            // if the characters are in the category of "Lowercase Letter".
516            let sub = if *name != uc {
517                NonUpperCaseGlobalSub::Suggestion {
518                    span: ident.span,
519                    replace: uc.clone(),
520                    applicability: if can_change_usages {
521                        Applicability::MachineApplicable
522                    } else {
523                        Applicability::MaybeIncorrect
524                    },
525                }
526            } else {
527                NonUpperCaseGlobalSub::Label { span: ident.span }
528            };
529
530            struct UsageCollector<'a, 'tcx> {
531                cx: &'tcx LateContext<'a>,
532                did: DefId,
533                collected: Vec<Span>,
534            }
535
536            impl<'v, 'tcx> Visitor<'v> for UsageCollector<'v, 'tcx> {
537                type NestedFilter = All;
538
539                fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
540                    self.cx.tcx
541                }
542
543                fn visit_path(
544                    &mut self,
545                    path: &rustc_hir::Path<'v>,
546                    _id: rustc_hir::HirId,
547                ) -> Self::Result {
548                    if let Some(final_seg) = path.segments.last()
549                        && final_seg.res.opt_def_id() == Some(self.did)
550                    {
551                        self.collected.push(final_seg.ident.span);
552                    }
553                }
554            }
555
556            let callback = || {
557                // Compute usages lazily as it can expansive and useless when the lint is allowed.
558                // cf. https://github.com/rust-lang/rust/pull/142645#issuecomment-2993024625
559                let usages = if can_change_usages
560                    && *name != uc
561                    && let Some(did) = did
562                {
563                    let mut usage_collector =
564                        UsageCollector { cx, did: did.to_def_id(), collected: Vec::new() };
565                    cx.tcx.hir_walk_toplevel_module(&mut usage_collector);
566                    usage_collector
567                        .collected
568                        .into_iter()
569                        .map(|span| NonUpperCaseGlobalSubTool { span, replace: uc.clone() })
570                        .collect()
571                } else {
572                    ::alloc::vec::Vec::new()vec![]
573                };
574
575                NonUpperCaseGlobal { sort, name, sub, usages }
576            };
577            cx.emit_span_lint(
578                NON_UPPER_CASE_GLOBALS,
579                ident.span,
580                NonUpperCaseGlobalGenerator { callback },
581            );
582        }
583    }
584}
585
586impl<'tcx> LateLintPass<'tcx> for NonUpperCaseGlobals {
587    fn check_item(&mut self, cx: &LateContext<'_>, it: &hir::Item<'_>) {
588        let attrs = cx.tcx.hir_attrs(it.hir_id());
589        match it.kind {
590            hir::ItemKind::Static(_, ident, ..) if !{
    {
            'done:
                {
                for i in attrs {
                    #[allow(unused_imports)]
                    use rustc_hir::attrs::AttributeKind::*;
                    let i: &rustc_hir::Attribute = i;
                    match i {
                        rustc_hir::Attribute::Parsed(NoMangle(..)) => {
                            break 'done Some(());
                        }
                        rustc_hir::Attribute::Unparsed(..) =>
                            {}
                            #[deny(unreachable_patterns)]
                            _ => {}
                    }
                }
                None
            }
        }.is_some()
}find_attr!(attrs, NoMangle(..)) => {
591                NonUpperCaseGlobals::check_upper_case(
592                    cx,
593                    "static variable",
594                    Some(it.owner_id.def_id),
595                    &ident,
596                );
597            }
598            hir::ItemKind::Const(ident, ..) => {
599                NonUpperCaseGlobals::check_upper_case(
600                    cx,
601                    "constant",
602                    Some(it.owner_id.def_id),
603                    &ident,
604                );
605            }
606            _ => {}
607        }
608    }
609
610    fn check_trait_item(&mut self, cx: &LateContext<'_>, ti: &hir::TraitItem<'_>) {
611        if let hir::TraitItemKind::Const(..) = ti.kind {
612            NonUpperCaseGlobals::check_upper_case(cx, "associated constant", None, &ti.ident);
613        }
614    }
615
616    fn check_impl_item(&mut self, cx: &LateContext<'_>, ii: &hir::ImplItem<'_>) {
617        if let hir::ImplItemKind::Const(..) = ii.kind
618            && let hir::ImplItemImplKind::Inherent { .. } = ii.impl_kind
619        {
620            NonUpperCaseGlobals::check_upper_case(cx, "associated constant", None, &ii.ident);
621        }
622    }
623
624    fn check_pat(&mut self, cx: &LateContext<'_>, p: &hir::Pat<'_>) {
625        // Lint for constants that look like binding identifiers (#7526)
626        if let PatKind::Expr(hir::PatExpr {
627            kind: PatExprKind::Path(hir::QPath::Resolved(None, path)),
628            ..
629        }) = p.kind
630        {
631            if let Res::Def(DefKind::Const { .. }, _) = path.res
632                && let [segment] = path.segments
633            {
634                NonUpperCaseGlobals::check_upper_case(
635                    cx,
636                    "constant in pattern",
637                    None,
638                    &segment.ident,
639                );
640            }
641        }
642    }
643
644    fn check_generic_param(&mut self, cx: &LateContext<'_>, param: &hir::GenericParam<'_>) {
645        if let GenericParamKind::Const { .. } = param.kind {
646            NonUpperCaseGlobals::check_upper_case(
647                cx,
648                "const parameter",
649                Some(param.def_id),
650                &param.name.ident(),
651            );
652        }
653    }
654}
655
656#[cfg(test)]
657mod tests;