rustc_lint/
nonstandard_style.rs

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