|
| 1 | +use clippy_utils::{diagnostics::span_lint, is_from_proc_macro}; |
| 2 | +use rustc_data_structures::fx::FxHashSet; |
| 3 | +use rustc_hir::{ |
| 4 | + def::{DefKind, Res}, |
| 5 | + intravisit::{walk_item, Visitor}, |
| 6 | + GenericParamKind, HirId, Item, ItemKind, ItemLocalId, Node, |
| 7 | +}; |
| 8 | +use rustc_lint::{LateContext, LateLintPass, LintContext}; |
| 9 | +use rustc_middle::lint::in_external_macro; |
| 10 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; |
| 11 | +use std::borrow::Cow; |
| 12 | + |
| 13 | +declare_clippy_lint! { |
| 14 | + /// ### What it does |
| 15 | + /// Checks for idents which comprise of a single letter. |
| 16 | + /// |
| 17 | + /// Note: This lint can be very noisy when enabled; it may be desirable to only enable it |
| 18 | + /// temporarily. |
| 19 | + /// |
| 20 | + /// ### Why is this bad? |
| 21 | + /// In many cases it's not, but at times it can severely hinder readability. Some codebases may |
| 22 | + /// wish to disallow this to improve readability. |
| 23 | + /// |
| 24 | + /// ### Example |
| 25 | + /// ```rust,ignore |
| 26 | + /// for m in movies { |
| 27 | + /// let title = m.t; |
| 28 | + /// } |
| 29 | + /// ``` |
| 30 | + /// Use instead: |
| 31 | + /// ```rust,ignore |
| 32 | + /// for movie in movies { |
| 33 | + /// let title = movie.title; |
| 34 | + /// } |
| 35 | + /// ``` |
| 36 | + /// ``` |
| 37 | + #[clippy::version = "1.72.0"] |
| 38 | + pub MIN_IDENT_CHARS, |
| 39 | + restriction, |
| 40 | + "disallows idents that are too short" |
| 41 | +} |
| 42 | +impl_lint_pass!(MinIdentChars => [MIN_IDENT_CHARS]); |
| 43 | + |
| 44 | +#[derive(Clone)] |
| 45 | +pub struct MinIdentChars { |
| 46 | + pub allowed_idents_below_min_chars: FxHashSet<String>, |
| 47 | + pub min_ident_chars_threshold: u64, |
| 48 | +} |
| 49 | + |
| 50 | +impl LateLintPass<'_> for MinIdentChars { |
| 51 | + fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) { |
| 52 | + if self.min_ident_chars_threshold == 0 { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + walk_item(&mut IdentVisitor { conf: self, cx }, item); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +struct IdentVisitor<'cx, 'tcx> { |
| 61 | + conf: &'cx MinIdentChars, |
| 62 | + cx: &'cx LateContext<'tcx>, |
| 63 | +} |
| 64 | + |
| 65 | +#[expect(clippy::cast_possible_truncation)] |
| 66 | +impl Visitor<'_> for IdentVisitor<'_, '_> { |
| 67 | + fn visit_id(&mut self, hir_id: HirId) { |
| 68 | + let Self { conf, cx } = *self; |
| 69 | + // Reimplementation of `find`, as it uses indexing, which can (and will in async functions) panic. |
| 70 | + // This should probably be fixed on the rustc side, this is just a temporary workaround. |
| 71 | + // FIXME: Remove me if/when this is fixed in rustc |
| 72 | + let node = if hir_id.local_id == ItemLocalId::from_u32(0) { |
| 73 | + // In this case, we can just use `find`, `Owner`'s `node` field is private anyway so we can't |
| 74 | + // reimplement it even if we wanted to |
| 75 | + cx.tcx.hir().find(hir_id) |
| 76 | + } else { |
| 77 | + let Some(owner) = cx.tcx.hir_owner_nodes(hir_id.owner).as_owner() else { |
| 78 | + return; |
| 79 | + }; |
| 80 | + owner.nodes.get(hir_id.local_id).copied().flatten().map(|p| p.node) |
| 81 | + }; |
| 82 | + let Some(node) = node else { |
| 83 | + return; |
| 84 | + }; |
| 85 | + let Some(ident) = node.ident() else { |
| 86 | + return; |
| 87 | + }; |
| 88 | + |
| 89 | + let str = ident.as_str(); |
| 90 | + if !in_external_macro(cx.sess(), ident.span) |
| 91 | + && str.len() <= conf.min_ident_chars_threshold as usize |
| 92 | + && !str.is_empty() |
| 93 | + && conf.allowed_idents_below_min_chars.get(&str.to_owned()).is_none() |
| 94 | + { |
| 95 | + if let Node::Item(item) = node && let ItemKind::Use(..) = item.kind { |
| 96 | + return; |
| 97 | + } |
| 98 | + // `struct Awa<T>(T)` |
| 99 | + // ^ |
| 100 | + if let Node::PathSegment(path) = node { |
| 101 | + if let Res::Def(def_kind, ..) = path.res && let DefKind::TyParam = def_kind { |
| 102 | + return; |
| 103 | + } |
| 104 | + if matches!(path.res, Res::PrimTy(..)) || path.res.opt_def_id().is_some_and(|def_id| !def_id.is_local()) |
| 105 | + { |
| 106 | + return; |
| 107 | + } |
| 108 | + } |
| 109 | + // `struct Awa<T>(T)` |
| 110 | + // ^ |
| 111 | + if let Node::GenericParam(generic_param) = node |
| 112 | + && let GenericParamKind::Type { .. } = generic_param.kind |
| 113 | + { |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + if is_from_proc_macro(cx, &ident) { |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + let help = if conf.min_ident_chars_threshold == 1 { |
| 122 | + Cow::Borrowed("this ident consists of a single char") |
| 123 | + } else { |
| 124 | + Cow::Owned(format!( |
| 125 | + "this ident is too short ({} <= {}) ", |
| 126 | + str.len(), |
| 127 | + conf.min_ident_chars_threshold |
| 128 | + )) |
| 129 | + }; |
| 130 | + span_lint(cx, MIN_IDENT_CHARS, ident.span, &help); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments