rustc_lint/
nonstandard_style.rs

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