Skip to content

Commit 5101c8e

Browse files
committed
Move ast::Item::ident into ast::ItemKind.
`ast::Item` has an `ident` field. - It's always non-empty for these item kinds: `ExternCrate`, `Static`, `Const`, `Fn`, `Mod`, `TyAlias`, `Enum`, `Struct`, `Union`, `Trait`, `TraitAlias`, `MacroDef`, `Delegation`. - It's always empty for these item kinds: `Use`, `ForeignMod`, `GlobalAsm`, `Impl`, `MacCall`, `DelegationMac`. There is a similar story for `AssocItemKind` and `ForeignItemKind`. Some sites that handle items check for an empty ident, some don't. This is a very C-like way of doing things, but this is Rust, we have sum types, we can do this properly and never forget to check for the exceptional case and never YOLO possibly empty identifiers (or possibly dummy spans) around and hope that things will work out. The commit is large but it's mostly obvious plumbing work. Some notable things. - `ast::Item` got 8 bytes bigger. This could be avoided by boxing the fields within some of the `ast::ItemKind` variants (specifically: `Struct`, `Union`, `Enum`). I might do that in a follow-up; this commit is big enough already. - For the visitors: `FnKind` no longer needs an `ident` field because the `Fn` within how has one. - In the parser, the `ItemInfo` typedef is no longer needed. It was used in various places to return an `Ident` alongside an `ItemKind`, but now the `Ident` (if present) is within the `ItemKind`. - In a few places I renamed identifier variables called `name` (or `foo_name`) as `ident` (or `foo_ident`), to better match the type, and because `name` is normally used for `Symbol`s. It's confusing to see something like `foo_name.name`.
1 parent 5791b9c commit 5101c8e

10 files changed

+89
-43
lines changed

clippy_lints/src/crate_in_macro_def.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ declare_lint_pass!(CrateInMacroDef => [CRATE_IN_MACRO_DEF]);
5353

5454
impl EarlyLintPass for CrateInMacroDef {
5555
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
56-
if let ItemKind::MacroDef(macro_def) = &item.kind
56+
if let ItemKind::MacroDef(_, macro_def) = &item.kind
5757
&& item.attrs.iter().any(is_macro_export)
5858
&& let Some(span) = contains_unhygienic_crate_reference(&macro_def.body.tokens)
5959
{

clippy_lints/src/doc/needless_doctest_main.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ use rustc_parse::parser::ForceCollect;
1313
use rustc_session::parse::ParseSess;
1414
use rustc_span::edition::Edition;
1515
use rustc_span::source_map::{FilePathMapping, SourceMap};
16-
use rustc_span::{FileName, Pos, sym};
16+
use rustc_span::{FileName, Ident, Pos, sym};
1717

1818
use super::Fragments;
1919

20-
fn get_test_spans(item: &Item, test_attr_spans: &mut Vec<Range<usize>>) {
20+
fn get_test_spans(item: &Item, ident: Ident, test_attr_spans: &mut Vec<Range<usize>>) {
2121
test_attr_spans.extend(
2222
item.attrs
2323
.iter()
2424
.find(|attr| attr.has_name(sym::test))
25-
.map(|attr| attr.span.lo().to_usize()..item.ident.span.hi().to_usize()),
25+
.map(|attr| attr.span.lo().to_usize()..ident.span.hi().to_usize()),
2626
);
2727
}
2828

@@ -64,10 +64,10 @@ pub fn check(
6464
match parser.parse_item(ForceCollect::No) {
6565
Ok(Some(item)) => match &item.kind {
6666
ItemKind::Fn(box Fn {
67-
sig, body: Some(block), ..
68-
}) if item.ident.name == sym::main => {
67+
ident, sig, body: Some(block), ..
68+
}) if ident.name == sym::main => {
6969
if !ignore {
70-
get_test_spans(&item, &mut test_attr_spans);
70+
get_test_spans(&item, *ident, &mut test_attr_spans);
7171
}
7272
let is_async = matches!(sig.header.coroutine_kind, Some(CoroutineKind::Async { .. }));
7373
let returns_nothing = match &sig.decl.output {
@@ -85,10 +85,10 @@ pub fn check(
8585
}
8686
},
8787
// Another function was found; this case is ignored for needless_doctest_main
88-
ItemKind::Fn(box Fn { .. }) => {
88+
ItemKind::Fn(fn_) => {
8989
eligible = false;
9090
if !ignore {
91-
get_test_spans(&item, &mut test_attr_spans);
91+
get_test_spans(&item, fn_.ident, &mut test_attr_spans);
9292
}
9393
},
9494
// Tests with one of these items are ignored

clippy_lints/src/duplicate_mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl_lint_pass!(DuplicateMod => [DUPLICATE_MOD]);
6363

6464
impl EarlyLintPass for DuplicateMod {
6565
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
66-
if let ItemKind::Mod(_, ModKind::Loaded(_, Inline::No, mod_spans, _)) = &item.kind
66+
if let ItemKind::Mod(_, _, ModKind::Loaded(_, Inline::No, mod_spans, _)) = &item.kind
6767
&& let FileName::Real(real) = cx.sess().source_map().span_to_filename(mod_spans.inner_span)
6868
&& let Some(local_path) = real.into_local_path()
6969
&& let Ok(absolute_path) = local_path.canonicalize()

clippy_lints/src/empty_line_after.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_lexer::TokenKind;
99
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
1010
use rustc_session::impl_lint_pass;
1111
use rustc_span::symbol::kw;
12-
use rustc_span::{BytePos, ExpnKind, Ident, InnerSpan, Span, SpanData, Symbol};
12+
use rustc_span::{BytePos, ExpnKind, Ident, InnerSpan, Span, SpanData, Symbol, sym};
1313

1414
declare_clippy_lint! {
1515
/// ### What it does
@@ -375,21 +375,21 @@ impl EmptyLineAfter {
375375
&mut self,
376376
cx: &EarlyContext<'_>,
377377
kind: &ItemKind,
378-
ident: &Ident,
378+
ident: &Option<Ident>,
379379
span: Span,
380380
attrs: &[Attribute],
381381
id: NodeId,
382382
) {
383383
self.items.push(ItemInfo {
384384
kind: kind.descr(),
385-
name: ident.name,
386-
span: if span.contains(ident.span) {
385+
name: if let Some(ident) = ident { ident.name } else { sym::dummy },
386+
span: if let Some(ident) = ident {
387387
span.with_hi(ident.span.hi())
388388
} else {
389389
span.with_hi(span.lo())
390390
},
391391
mod_items: match kind {
392-
ItemKind::Mod(_, ModKind::Loaded(items, _, _, _)) => items
392+
ItemKind::Mod(_, _, ModKind::Loaded(items, _, _, _)) => items
393393
.iter()
394394
.filter(|i| !matches!(i.span.ctxt().outer_expn_data().kind, ExpnKind::AstPass(_)))
395395
.map(|i| i.id)
@@ -471,7 +471,7 @@ impl EarlyLintPass for EmptyLineAfter {
471471
self.check_item_kind(
472472
cx,
473473
&item.kind.clone().into(),
474-
&item.ident,
474+
&item.kind.ident(),
475475
item.span,
476476
&item.attrs,
477477
item.id,
@@ -482,14 +482,14 @@ impl EarlyLintPass for EmptyLineAfter {
482482
self.check_item_kind(
483483
cx,
484484
&item.kind.clone().into(),
485-
&item.ident,
485+
&item.kind.ident(),
486486
item.span,
487487
&item.attrs,
488488
item.id,
489489
);
490490
}
491491

492492
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
493-
self.check_item_kind(cx, &item.kind, &item.ident, item.span, &item.attrs, item.id);
493+
self.check_item_kind(cx, &item.kind, &item.kind.ident(), item.span, &item.attrs, item.id);
494494
}
495495
}

clippy_lints/src/empty_with_brackets.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,9 @@ declare_lint_pass!(EmptyWithBrackets => [EMPTY_STRUCTS_WITH_BRACKETS, EMPTY_ENUM
7474

7575
impl EarlyLintPass for EmptyWithBrackets {
7676
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
77-
let span_after_ident = item.span.with_lo(item.ident.span.hi());
78-
79-
if let ItemKind::Struct(var_data, _) = &item.kind
77+
if let ItemKind::Struct(ident, var_data, _) = &item.kind
8078
&& has_brackets(var_data)
79+
&& let span_after_ident = item.span.with_lo(ident.span.hi())
8180
&& has_no_fields(cx, var_data, span_after_ident)
8281
{
8382
span_lint_and_then(

clippy_lints/src/field_scoped_visibility_modifiers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ declare_lint_pass!(FieldScopedVisibilityModifiers => [FIELD_SCOPED_VISIBILITY_MO
5151

5252
impl EarlyLintPass for FieldScopedVisibilityModifiers {
5353
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
54-
let ItemKind::Struct(ref st, _) = item.kind else {
54+
let ItemKind::Struct(_, ref st, _) = item.kind else {
5555
return;
5656
};
5757
for field in st.fields() {

clippy_lints/src/multiple_bound_locations.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ declare_lint_pass!(MultipleBoundLocations => [MULTIPLE_BOUND_LOCATIONS]);
3939

4040
impl EarlyLintPass for MultipleBoundLocations {
4141
fn check_fn(&mut self, cx: &EarlyContext<'_>, kind: FnKind<'_>, _: Span, _: NodeId) {
42-
if let FnKind::Fn(_, _, _, Fn { generics, .. }) = kind
42+
if let FnKind::Fn(_, _, Fn { generics, .. }) = kind
4343
&& !generics.params.is_empty()
4444
&& !generics.where_clause.predicates.is_empty()
4545
{

clippy_lints/src/partial_pub_fields.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ declare_lint_pass!(PartialPubFields => [PARTIAL_PUB_FIELDS]);
4141

4242
impl EarlyLintPass for PartialPubFields {
4343
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
44-
let ItemKind::Struct(ref st, _) = item.kind else {
44+
let ItemKind::Struct(_, ref st, _) = item.kind else {
4545
return;
4646
};
4747

clippy_lints/src/single_component_path_imports.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,11 @@ impl SingleComponentPathImports {
174174
}
175175

176176
match &item.kind {
177-
ItemKind::Mod(_, ModKind::Loaded(items, ..)) => {
177+
ItemKind::Mod(_, _, ModKind::Loaded(items, ..)) => {
178178
self.check_mod(items);
179179
},
180-
ItemKind::MacroDef(MacroDef { macro_rules: true, .. }) => {
181-
macros.push(item.ident.name);
180+
ItemKind::MacroDef(ident, MacroDef { macro_rules: true, .. }) => {
181+
macros.push(ident.name);
182182
},
183183
ItemKind::Use(use_tree) => {
184184
let segments = &use_tree.prefix.segments;

0 commit comments

Comments
 (0)