Skip to content

Commit dfea9c0

Browse files
committed
Change name lint min_ident_chars to ident_chars.
Add `max-ident-chars-length` to config and is set to 100 by default.
1 parent 40bead0 commit dfea9c0

19 files changed

+288
-171
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5830,6 +5830,7 @@ Released 2018-09-13
58305830
[`get_last_with_len`]: https://rust-lang.github.io/rust-clippy/master/index.html#get_last_with_len
58315831
[`get_unwrap`]: https://rust-lang.github.io/rust-clippy/master/index.html#get_unwrap
58325832
[`host_endian_bytes`]: https://rust-lang.github.io/rust-clippy/master/index.html#host_endian_bytes
5833+
[`ident_chars`]: https://rust-lang.github.io/rust-clippy/master/index.html#ident_chars
58335834
[`identity_conversion`]: https://rust-lang.github.io/rust-clippy/master/index.html#identity_conversion
58345835
[`identity_op`]: https://rust-lang.github.io/rust-clippy/master/index.html#identity_op
58355836
[`if_let_mutex`]: https://rust-lang.github.io/rust-clippy/master/index.html#if_let_mutex
@@ -6531,6 +6532,7 @@ Released 2018-09-13
65316532
[`literal-representation-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#literal-representation-threshold
65326533
[`matches-for-let-else`]: https://doc.rust-lang.org/clippy/lint_configuration.html#matches-for-let-else
65336534
[`max-fn-params-bools`]: https://doc.rust-lang.org/clippy/lint_configuration.html#max-fn-params-bools
6535+
[`max-ident-chars-length`]: https://doc.rust-lang.org/clippy/lint_configuration.html#max-ident-chars-length
65346536
[`max-include-file-size`]: https://doc.rust-lang.org/clippy/lint_configuration.html#max-include-file-size
65356537
[`max-struct-bools`]: https://doc.rust-lang.org/clippy/lint_configuration.html#max-struct-bools
65366538
[`max-suggested-slice-pattern-length`]: https://doc.rust-lang.org/clippy/lint_configuration.html#max-suggested-slice-pattern-length

book/src/lint_configuration.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,16 @@ The maximum number of bool parameters a function can have
692692
* [`fn_params_excessive_bools`](https://rust-lang.github.io/rust-clippy/master/index.html#fn_params_excessive_bools)
693693

694694

695+
## `max-ident-chars-length`
696+
The maximum length of an identifier
697+
698+
**Default Value:** `100`
699+
700+
---
701+
**Affected lints:**
702+
* [`ident_chars`](https://rust-lang.github.io/rust-clippy/master/index.html#ident_chars)
703+
704+
695705
## `max-include-file-size`
696706
The maximum size of a file included via `include_bytes!()` or `include_str!()`, in bytes
697707

clippy_config/src/conf.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,9 @@ define_Conf! {
661661
/// The maximum number of bool parameters a function can have
662662
#[lints(fn_params_excessive_bools)]
663663
max_fn_params_bools: u64 = 3,
664+
/// The maximum length of an identifier
665+
#[lints(ident_chars)]
666+
max_ident_chars_length: u32 = 100,
664667
/// The maximum size of a file included via `include_bytes!()` or `include_str!()`, in bytes
665668
#[lints(large_include_file)]
666669
max_include_file_size: u64 = 1_000_000,
@@ -676,7 +679,7 @@ define_Conf! {
676679
#[lints(type_repetition_in_bounds)]
677680
max_trait_bounds: u64 = 3,
678681
/// Minimum chars an ident can have, anything below or equal to this will be linted.
679-
#[lints(min_ident_chars)]
682+
#[lints(ident_chars)]
680683
min_ident_chars_threshold: u64 = 1,
681684
/// Whether to allow fields starting with an underscore to skip documentation requirements
682685
#[lints(missing_docs_in_private_items)]

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ pub static LINTS: &[&crate::LintInfo] = &[
194194
crate::functions::TOO_MANY_ARGUMENTS_INFO,
195195
crate::functions::TOO_MANY_LINES_INFO,
196196
crate::future_not_send::FUTURE_NOT_SEND_INFO,
197+
crate::ident_chars::IDENT_CHARS_INFO,
197198
crate::if_let_mutex::IF_LET_MUTEX_INFO,
198199
crate::if_not_else::IF_NOT_ELSE_INFO,
199200
crate::if_then_some_else_none::IF_THEN_SOME_ELSE_NONE_INFO,
@@ -491,7 +492,6 @@ pub static LINTS: &[&crate::LintInfo] = &[
491492
crate::methods::WAKER_CLONE_WAKE_INFO,
492493
crate::methods::WRONG_SELF_CONVENTION_INFO,
493494
crate::methods::ZST_OFFSET_INFO,
494-
crate::min_ident_chars::MIN_IDENT_CHARS_INFO,
495495
crate::minmax::MIN_MAX_INFO,
496496
crate::misc::SHORT_CIRCUIT_STATEMENT_INFO,
497497
crate::misc::TOPLEVEL_REF_ARG_INFO,

clippy_lints/src/deprecated_lints.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,6 @@ declare_with_version! { RENAMED(RENAMED_VERSION) = [
194194
("clippy::transmute_float_to_int", "unnecessary_transmutes"),
195195
#[clippy::version = "1.88.0"]
196196
("clippy::transmute_num_to_bytes", "unnecessary_transmutes"),
197+
#[clippy::version = "1.88.0"]
198+
("clippy::min_ident_chars", "clippy::ident_chars"),
197199
]}

clippy_lints/src/min_ident_chars.rs renamed to clippy_lints/src/ident_chars.rs

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@ use std::borrow::Cow;
1212

1313
declare_clippy_lint! {
1414
/// ### What it does
15-
/// Checks for identifiers which consist of a single character (or fewer than the configured threshold).
15+
/// Checks for identifiers which are out of bounds, for example to short (lower than the configured
16+
/// threshold) or too long (higher than the configured threshold).
17+
/// which consist of a single character (or fewer than the configured threshold)
1618
///
1719
/// Note: This lint can be very noisy when enabled; it may be desirable to only enable it
1820
/// temporarily.
1921
///
2022
/// ### Why restrict this?
21-
/// To improve readability by requiring that every variable has a name more specific than a single letter can be.
23+
/// To improve readability of code and make it possible to enforce this lint to align a code
24+
/// style on this subject within a team.
2225
///
23-
/// ### Example
26+
/// ### Example of small identifiers
2427
/// ```rust,ignore
2528
/// for m in movies {
2629
/// let title = m.t;
@@ -33,22 +36,24 @@ declare_clippy_lint! {
3336
/// }
3437
/// ```
3538
#[clippy::version = "1.72.0"]
36-
pub MIN_IDENT_CHARS,
39+
pub IDENT_CHARS,
3740
restriction,
38-
"disallows idents that are too short"
41+
"disallows idents which length are out of bounds"
3942
}
40-
impl_lint_pass!(MinIdentChars => [MIN_IDENT_CHARS]);
43+
impl_lint_pass!(MinIdentChars => [IDENT_CHARS]);
4144

4245
pub struct MinIdentChars {
4346
allowed_idents_below_min_chars: FxHashSet<String>,
4447
min_ident_chars_threshold: u64,
48+
max_variable_name_length: u32,
4549
}
4650

4751
impl MinIdentChars {
4852
pub fn new(conf: &'static Conf) -> Self {
4953
Self {
5054
allowed_idents_below_min_chars: conf.allowed_idents_below_min_chars.iter().cloned().collect(),
5155
min_ident_chars_threshold: conf.min_ident_chars_threshold,
56+
max_variable_name_length: conf.max_ident_chars_length,
5257
}
5358
}
5459

@@ -60,6 +65,10 @@ impl MinIdentChars {
6065
&& !str.is_empty()
6166
&& !self.allowed_idents_below_min_chars.contains(str)
6267
}
68+
69+
fn is_ident_too_long(&self, cx: &LateContext<'_>, str: &str, span: Span) -> bool {
70+
!span.in_external_macro(cx.sess().source_map()) && str.len() > self.max_variable_name_length as usize
71+
}
6372
}
6473

6574
impl LateLintPass<'_> for MinIdentChars {
@@ -87,6 +96,13 @@ impl LateLintPass<'_> for MinIdentChars {
8796
{
8897
emit_min_ident_chars(self, cx, str, ident.span);
8998
}
99+
100+
if let PatKind::Binding(_, _, ident, ..) = pat.kind
101+
&& let str = ident.as_str()
102+
&& self.is_ident_too_long(cx, str, ident.span)
103+
{
104+
emit_max_ident_chars(self, cx, str, ident.span);
105+
}
90106
}
91107
}
92108

@@ -172,6 +188,33 @@ impl Visitor<'_> for IdentVisitor<'_, '_> {
172188

173189
emit_min_ident_chars(conf, cx, str, ident.span);
174190
}
191+
192+
if conf.is_ident_too_long(cx, str, ident.span) {
193+
// Check whether the node is part of a `use` statement. We don't want to emit a warning if the user
194+
// has no control over the type.
195+
let usenode = opt_as_use_node(node).or_else(|| {
196+
cx.tcx
197+
.hir_parent_iter(hir_id)
198+
.find_map(|(_, node)| opt_as_use_node(node))
199+
});
200+
201+
// If the name of the identifier is the same as the one of the imported item, this means that we
202+
// found a `use foo::bar`. We can early-return to not emit the warning.
203+
// If however the identifier is different, this means it is an alias (`use foo::bar as baz`). In
204+
// this case, we need to emit the warning for `baz`.
205+
if let Some(imported_item_path) = usenode
206+
&& let Some(Res::Def(_, imported_item_defid)) = imported_item_path.res.first()
207+
&& cx.tcx.item_name(*imported_item_defid).as_str() == str
208+
{
209+
return;
210+
}
211+
212+
if is_from_proc_macro(cx, &ident) {
213+
return;
214+
}
215+
216+
emit_min_ident_chars(conf, cx, str, ident.span);
217+
}
175218
}
176219
}
177220

@@ -185,7 +228,16 @@ fn emit_min_ident_chars(conf: &MinIdentChars, cx: &impl LintContext, ident: &str
185228
conf.min_ident_chars_threshold,
186229
))
187230
};
188-
span_lint(cx, MIN_IDENT_CHARS, span, help);
231+
span_lint(cx, IDENT_CHARS, span, help);
232+
}
233+
234+
fn emit_max_ident_chars(conf: &MinIdentChars, cx: &impl LintContext, ident: &str, span: Span) {
235+
let help = Cow::Owned(format!(
236+
"this ident is too long ({} > {})",
237+
ident.len(),
238+
conf.max_variable_name_length,
239+
));
240+
span_lint(cx, IDENT_CHARS, span, help);
189241
}
190242

191243
/// Attempt to convert the node to an [`ItemKind::Use`] node.

clippy_lints/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ mod from_raw_with_void_ptr;
154154
mod from_str_radix_10;
155155
mod functions;
156156
mod future_not_send;
157+
mod ident_chars;
157158
mod if_let_mutex;
158159
mod if_not_else;
159160
mod if_then_some_else_none;
@@ -230,7 +231,6 @@ mod match_result_ok;
230231
mod matches;
231232
mod mem_replace;
232233
mod methods;
233-
mod min_ident_chars;
234234
mod minmax;
235235
mod misc;
236236
mod misc_early;
@@ -877,7 +877,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
877877
store.register_late_pass(|_| Box::new(redundant_type_annotations::RedundantTypeAnnotations));
878878
store.register_late_pass(|_| Box::new(arc_with_non_send_sync::ArcWithNonSendSync));
879879
store.register_late_pass(|_| Box::new(needless_if::NeedlessIf));
880-
store.register_late_pass(move |_| Box::new(min_ident_chars::MinIdentChars::new(conf)));
880+
store.register_late_pass(move |_| Box::new(ident_chars::MinIdentChars::new(conf)));
881881
store.register_late_pass(move |_| Box::new(large_stack_frames::LargeStackFrames::new(conf)));
882882
store.register_late_pass(|_| Box::new(single_range_in_vec_init::SingleRangeInVecInit));
883883
store.register_late_pass(move |_| Box::new(needless_pass_by_ref_mut::NeedlessPassByRefMut::new(conf)));
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
allowed-idents-below-min-chars = ["Owo", "Uwu", "wha", "t_e", "lse", "_do", "_i_", "put", "her", "_e"]
22
min-ident-chars-threshold = 3
3+
4+
# override the default length of variables to test the configuration
5+
max-ident-chars-length = 50
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![warn(clippy::ident_chars)]
2+
3+
fn a_function(ferris_singlehandedly_refactored_the_monolith_while_juggling_crates_and_lifetimes: &str) {
4+
//~^ ident_chars
5+
}
6+
7+
fn another_function(just_a_short_name: &str) {
8+
// should not cause a problem
9+
}
10+
11+
fn main() {
12+
// `ferris_singlehandedly_refactored_the_monolith_while_juggling_crates_and_lifetimes` is too long
13+
let ferris_singlehandedly_refactored_the_monolith_while_juggling_crates_and_lifetimes = "very long indeed";
14+
//~^ ident_chars
15+
}

0 commit comments

Comments
 (0)