Skip to content

Commit ba091c5

Browse files
committed
Ignore imported items in min_ident_chars
Suppress the `min_ident_chars` warning for items whose name we cannot control. Do not warn for `use a::b`, but warn for `use a::b as c`, since `c` is a local identifier. Fixes rust-lang#8573
1 parent 7dfa6ce commit ba091c5

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

clippy_lints/src/min_ident_chars.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::is_from_proc_macro;
33
use rustc_data_structures::fx::FxHashSet;
44
use rustc_hir::def::{DefKind, Res};
55
use rustc_hir::intravisit::{walk_item, Visitor};
6-
use rustc_hir::{GenericParamKind, HirId, Item, ItemKind, ItemLocalId, Node, Pat, PatKind};
6+
use rustc_hir::{GenericParamKind, HirId, Item, ItemKind, ItemLocalId, Node, Pat, PatKind, UsePath};
77
use rustc_lint::{LateContext, LateLintPass, LintContext};
88
use rustc_middle::lint::in_external_macro;
99
use rustc_session::impl_lint_pass;
@@ -105,11 +105,26 @@ impl Visitor<'_> for IdentVisitor<'_, '_> {
105105

106106
let str = ident.as_str();
107107
if conf.is_ident_too_short(cx, str, ident.span) {
108-
if let Node::Item(item) = node
109-
&& let ItemKind::Use(..) = item.kind
108+
// Check whether the node is part of a `use statement. We don't want to emit a warning if the user
109+
// has no control over the type.
110+
let usenode = opt_as_use_node(node).or_else(|| {
111+
cx.tcx
112+
.hir()
113+
.parent_iter(hir_id)
114+
.find_map(|(_, node)| opt_as_use_node(node))
115+
});
116+
117+
// If the name of the identifier is the same from the one of the imported item, this means that we
118+
// found a `use foo::bar`. We can early-return to not emit the warning.
119+
// If however the identifier is different, this means it is an alias (`use foo::bar as baz`). In
120+
// this case, we need to emit the warning for `baz`.
121+
if let Some(imported_item_path) = usenode
122+
&& let Some(Res::Def(_, imported_item_defid)) = imported_item_path.res.first()
123+
&& cx.tcx.item_name(*imported_item_defid).as_str() == str
110124
{
111125
return;
112126
}
127+
113128
// `struct Awa<T>(T)`
114129
// ^
115130
if let Node::PathSegment(path) = node {
@@ -160,3 +175,16 @@ fn emit_min_ident_chars(conf: &MinIdentChars, cx: &impl LintContext, ident: &str
160175
};
161176
span_lint(cx, MIN_IDENT_CHARS, span, &help);
162177
}
178+
179+
/// Attempt to convert the node to an [`ItemKind::Use`] node.
180+
///
181+
/// If it is, return the [`UsePath`] contained within.
182+
fn opt_as_use_node(node: Node<'_>) -> Option<&'_ UsePath<'_>> {
183+
if let Node::Item(item) = node
184+
&& let ItemKind::Use(path, _) = item.kind
185+
{
186+
Some(path)
187+
} else {
188+
None
189+
}
190+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
#![allow(nonstandard_style, unused)]
22

33
pub struct Aaa;
4+
pub struct Bbb;
5+
6+
pub const N: u32 = 3;

tests/ui-toml/min_ident_chars/min_ident_chars.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![warn(clippy::min_ident_chars)]
44

55
extern crate extern_types;
6-
use extern_types::Aaa;
6+
use extern_types::{Aaa, N as W};
77

88
struct Owo {
99
Uwu: u128,

tests/ui-toml/min_ident_chars/min_ident_chars.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: this ident is too short (3 <= 3)
2-
--> $DIR/min_ident_chars.rs:6:19
1+
error: this ident is too short (1 <= 3)
2+
--> $DIR/min_ident_chars.rs:6:30
33
|
4-
LL | use extern_types::Aaa;
5-
| ^^^
4+
LL | use extern_types::{Aaa, N as W};
5+
| ^
66
|
77
= note: `-D clippy::min-ident-chars` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::min_ident_chars)]`

0 commit comments

Comments
 (0)