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

Commit 736d114

Browse files
committed
Extract lint namespace and lint name from the meta item
Lints with more than two segments are unrecognized, instead of having the middle segments truncated.
1 parent 650e0c8 commit 736d114

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

clippy_lints/src/attrs/utils.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,18 @@ fn is_relevant_expr(cx: &LateContext<'_>, typeck_results: &ty::TypeckResults<'_>
7575

7676
/// Returns the lint name if it is clippy lint.
7777
pub(super) fn extract_clippy_lint(lint: &MetaItemInner) -> Option<Symbol> {
78-
if let Some(meta_item) = lint.meta_item()
79-
&& meta_item.path.segments.len() > 1
80-
&& let tool_name = meta_item.path.segments[0].ident
81-
&& tool_name.name == sym::clippy
82-
{
83-
let lint_name = meta_item.path.segments.last().unwrap().ident.name;
84-
return Some(lint_name);
78+
match namespace_and_lint(lint) {
79+
(Some(sym::clippy), name) => name,
80+
_ => None,
81+
}
82+
}
83+
84+
/// Returns the lint namespace, if any, as well as the lint name. (`None`, `None`) means
85+
/// the lint had less than 1 or more than 2 segments.
86+
pub(super) fn namespace_and_lint(lint: &MetaItemInner) -> (Option<Symbol>, Option<Symbol>) {
87+
match lint.meta_item().map(|m| m.path.segments.as_slice()).unwrap_or_default() {
88+
[name] => (None, Some(name.ident.name)),
89+
[namespace, name] => (Some(namespace.ident.name), Some(name.ident.name)),
90+
_ => (None, None),
8591
}
86-
None
8792
}

0 commit comments

Comments
 (0)