Skip to content

Commit a816d04

Browse files
committed
Fix min_ident_chars: ignore impl on trait impl
1 parent 7b3bd5b commit a816d04

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

clippy_lints/src/min_ident_chars.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use clippy_utils::is_from_proc_macro;
44
use rustc_data_structures::fx::FxHashSet;
55
use rustc_hir::def::{DefKind, Res};
66
use rustc_hir::intravisit::{Visitor, walk_item, walk_trait_item};
7-
use rustc_hir::{GenericParamKind, HirId, Item, ItemKind, ItemLocalId, Node, Pat, PatKind, TraitItem, UsePath};
7+
use rustc_hir::{
8+
GenericParamKind, HirId, Impl, ImplItemKind, Item, ItemKind, ItemLocalId, Node, Pat, PatKind, TraitItem, UsePath,
9+
};
810
use rustc_lint::{LateContext, LateLintPass, LintContext};
911
use rustc_session::impl_lint_pass;
1012
use rustc_span::Span;
@@ -84,6 +86,7 @@ impl LateLintPass<'_> for MinIdentChars {
8486
if let PatKind::Binding(_, _, ident, ..) = pat.kind
8587
&& let str = ident.as_str()
8688
&& self.is_ident_too_short(cx, str, ident.span)
89+
&& is_not_in_trait_impl(cx, pat)
8790
{
8891
emit_min_ident_chars(self, cx, str, ident.span);
8992
}
@@ -201,3 +204,27 @@ fn opt_as_use_node(node: Node<'_>) -> Option<&'_ UsePath<'_>> {
201204
None
202205
}
203206
}
207+
208+
/// Check if a pattern is a function param in an impl block for a trait.
209+
fn is_not_in_trait_impl(cx: &LateContext<'_>, pat: &Pat<'_>) -> bool {
210+
let parent_node = cx.tcx.parent_hir_node(pat.hir_id);
211+
if !matches!(parent_node, Node::Param(_)) {
212+
return true;
213+
}
214+
215+
for (_, parent_node) in cx.tcx.hir_parent_iter(pat.hir_id) {
216+
if let Node::ImplItem(impl_item) = parent_node
217+
&& matches!(impl_item.kind, ImplItemKind::Fn(_, _))
218+
{
219+
let impl_parent_node = cx.tcx.parent_hir_node(impl_item.hir_id());
220+
if let Node::Item(parent_item) = impl_parent_node
221+
&& let ItemKind::Impl(Impl { of_trait: Some(_), .. }) = &parent_item.kind
222+
{
223+
return false;
224+
}
225+
return true;
226+
}
227+
}
228+
229+
true
230+
}

tests/ui/min_ident_chars.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,11 @@ fn wrong_pythagoras(a: f32, b: f32) -> f32 {
124124
mod issue_11163 {
125125
struct Array<T, const N: usize>([T; N]);
126126
}
127+
128+
struct Issue13396;
129+
130+
impl core::fmt::Display for Issue13396 {
131+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
132+
write!(f, "Issue13396")
133+
}
134+
}

0 commit comments

Comments
 (0)