Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 7cdd87c

Browse files
committed
ignore generics and allow arbitrary threshold
1 parent 03c8db0 commit 7cdd87c

File tree

14 files changed

+270
-175
lines changed

14 files changed

+270
-175
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4958,6 +4958,7 @@ Released 2018-09-13
49584958
[`mem_replace_option_with_none`]: https://rust-lang.github.io/rust-clippy/master/index.html#mem_replace_option_with_none
49594959
[`mem_replace_with_default`]: https://rust-lang.github.io/rust-clippy/master/index.html#mem_replace_with_default
49604960
[`mem_replace_with_uninit`]: https://rust-lang.github.io/rust-clippy/master/index.html#mem_replace_with_uninit
4961+
[`min_ident_chars`]: https://rust-lang.github.io/rust-clippy/master/index.html#min_ident_chars
49614962
[`min_max`]: https://rust-lang.github.io/rust-clippy/master/index.html#min_max
49624963
[`misaligned_transmute`]: https://rust-lang.github.io/rust-clippy/master/index.html#misaligned_transmute
49634964
[`mismatched_target_os`]: https://rust-lang.github.io/rust-clippy/master/index.html#mismatched_target_os
@@ -5155,7 +5156,6 @@ Released 2018-09-13
51555156
[`significant_drop_tightening`]: https://rust-lang.github.io/rust-clippy/master/index.html#significant_drop_tightening
51565157
[`similar_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#similar_names
51575158
[`single_char_add_str`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_char_add_str
5158-
[`single_char_idents`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_char_idents
51595159
[`single_char_lifetime_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_char_lifetime_names
51605160
[`single_char_pattern`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_char_pattern
51615161
[`single_char_push_str`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_char_push_str

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
416416
crate::methods::VERBOSE_FILE_READS_INFO,
417417
crate::methods::WRONG_SELF_CONVENTION_INFO,
418418
crate::methods::ZST_OFFSET_INFO,
419+
crate::min_ident_chars::MIN_IDENT_CHARS_INFO,
419420
crate::minmax::MIN_MAX_INFO,
420421
crate::misc::SHORT_CIRCUIT_STATEMENT_INFO,
421422
crate::misc::TOPLEVEL_REF_ARG_INFO,
@@ -565,7 +566,6 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
565566
crate::shadow::SHADOW_SAME_INFO,
566567
crate::shadow::SHADOW_UNRELATED_INFO,
567568
crate::significant_drop_tightening::SIGNIFICANT_DROP_TIGHTENING_INFO,
568-
crate::single_char_idents::SINGLE_CHAR_IDENTS_INFO,
569569
crate::single_char_lifetime_names::SINGLE_CHAR_LIFETIME_NAMES_INFO,
570570
crate::single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS_INFO,
571571
crate::size_of_in_element_count::SIZE_OF_IN_ELEMENT_COUNT_INFO,

clippy_lints/src/lib.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ mod matches;
197197
mod mem_forget;
198198
mod mem_replace;
199199
mod methods;
200+
mod min_ident_chars;
200201
mod minmax;
201202
mod misc;
202203
mod misc_early;
@@ -284,7 +285,6 @@ mod semicolon_if_nothing_returned;
284285
mod serde_api;
285286
mod shadow;
286287
mod significant_drop_tightening;
287-
mod single_char_idents;
288288
mod single_char_lifetime_names;
289289
mod single_component_path_imports;
290290
mod size_of_in_element_count;
@@ -1034,10 +1034,12 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10341034
store.register_late_pass(|_| Box::new(redundant_type_annotations::RedundantTypeAnnotations));
10351035
store.register_late_pass(|_| Box::new(arc_with_non_send_sync::ArcWithNonSendSync));
10361036
store.register_late_pass(|_| Box::new(needless_if::NeedlessIf));
1037-
let allowed_idents = conf.allowed_idents.clone();
1038-
store.register_early_pass(move || {
1039-
Box::new(single_char_idents::SingleCharIdents {
1040-
allowed_idents: allowed_idents.clone(),
1037+
let allowed_idents_below_min_chars = conf.allowed_idents_below_min_chars.clone();
1038+
let min_ident_chars_threshold = conf.min_ident_chars_threshold;
1039+
store.register_late_pass(move |_| {
1040+
Box::new(min_ident_chars::MinIdentChars {
1041+
allowed_idents_below_min_chars: allowed_idents_below_min_chars.clone(),
1042+
min_ident_chars_threshold,
10411043
})
10421044
});
10431045
// add lints here, do not remove this comment, it's used in `new_lint`

clippy_lints/src/min_ident_chars.rs

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
}

clippy_lints/src/single_char_idents.rs

Lines changed: 0 additions & 59 deletions
This file was deleted.

clippy_lints/src/utils/conf.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const DEFAULT_DOC_VALID_IDENTS: &[&str] = &[
3434
"CamelCase",
3535
];
3636
const DEFAULT_DISALLOWED_NAMES: &[&str] = &["foo", "baz", "quux"];
37-
const DEFAULT_ALLOWED_IDENTS: &[char] = &['i', 'j', 'x', 'y', 'z', 'n'];
37+
const DEFAULT_ALLOWED_IDENTS_BELOW_MIN_CHARS: &[&str] = &["i", "j", "x", "y", "z", "n"];
3838

3939
/// Holds information used by `MISSING_ENFORCED_IMPORT_RENAMES` lint.
4040
#[derive(Clone, Debug, Deserialize)]
@@ -523,6 +523,15 @@ define_Conf! {
523523
///
524524
/// Whether to allow module inception if it's not public.
525525
(allow_private_module_inception: bool = false),
526+
/// Lint: MIN_IDENT_CHARS.
527+
///
528+
/// Allowed names below the minimum allowed characters.
529+
(allowed_idents_below_min_chars: rustc_data_structures::fx::FxHashSet<String> =
530+
super::DEFAULT_ALLOWED_IDENTS_BELOW_MIN_CHARS.iter().map(ToString::to_string).collect()),
531+
/// Lint: MIN_IDENT_CHARS.
532+
///
533+
/// Minimum chars an ident can have, anything below or equal to this will be linted.
534+
(min_ident_chars_threshold: u64 = 1),
526535
}
527536

528537
/// Search for the configuration file.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#![allow(nonstandard_style, unused)]
2+
3+
pub struct Aaa;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
allowed-idents-below-min-chars = ["Owo", "Uwu", "wha", "t_e", "lse", "_do", "_i_", "put", "her", "_e"]
2+
min-ident-chars-threshold = 3
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//@aux-build:extern_types.rs
2+
#![allow(nonstandard_style, unused)]
3+
#![warn(clippy::min_ident_chars)]
4+
5+
extern crate extern_types;
6+
use extern_types::Aaa;
7+
8+
struct Owo {
9+
Uwu: u128,
10+
aaa: Aaa,
11+
}
12+
13+
fn main() {
14+
let wha = 1;
15+
let vvv = 1;
16+
let uuu = 1;
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: this ident is too short (3 <= 3)
2+
--> $DIR/min_ident_chars.rs:6:19
3+
|
4+
LL | use extern_types::Aaa;
5+
| ^^^
6+
|
7+
= note: `-D clippy::min-ident-chars` implied by `-D warnings`
8+
9+
error: this ident is too short (3 <= 3)
10+
--> $DIR/min_ident_chars.rs:10:5
11+
|
12+
LL | aaa: Aaa,
13+
| ^^^
14+
15+
error: aborting due to 2 previous errors
16+

0 commit comments

Comments
 (0)