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`.
This commit is contained in:
parent
43018eacb6
commit
df247968f2
54 changed files with 1072 additions and 864 deletions
|
@ -330,7 +330,6 @@ impl EarlyLintPass for UnsafeCode {
|
|||
if let FnKind::Fn(
|
||||
ctxt,
|
||||
_,
|
||||
_,
|
||||
ast::Fn {
|
||||
sig: ast::FnSig { header: ast::FnHeader { safety: ast::Safety::Unsafe(_), .. }, .. },
|
||||
body,
|
||||
|
@ -3116,6 +3115,7 @@ impl EarlyLintPass for SpecialModuleName {
|
|||
for item in &krate.items {
|
||||
if let ast::ItemKind::Mod(
|
||||
_,
|
||||
ident,
|
||||
ast::ModKind::Unloaded | ast::ModKind::Loaded(_, ast::Inline::No, _, _),
|
||||
) = item.kind
|
||||
{
|
||||
|
@ -3123,7 +3123,7 @@ impl EarlyLintPass for SpecialModuleName {
|
|||
continue;
|
||||
}
|
||||
|
||||
match item.ident.name.as_str() {
|
||||
match ident.name.as_str() {
|
||||
"lib" => cx.emit_span_lint(
|
||||
SPECIAL_MODULE_NAME,
|
||||
item.span,
|
||||
|
|
|
@ -172,20 +172,22 @@ impl EarlyLintPass for NonCamelCaseTypes {
|
|||
}
|
||||
|
||||
match &it.kind {
|
||||
ast::ItemKind::TyAlias(..)
|
||||
| ast::ItemKind::Enum(..)
|
||||
| ast::ItemKind::Struct(..)
|
||||
| ast::ItemKind::Union(..) => self.check_case(cx, "type", &it.ident),
|
||||
ast::ItemKind::Trait(..) => self.check_case(cx, "trait", &it.ident),
|
||||
ast::ItemKind::TraitAlias(..) => self.check_case(cx, "trait alias", &it.ident),
|
||||
ast::ItemKind::TyAlias(box ast::TyAlias { ident, .. })
|
||||
| ast::ItemKind::Enum(ident, ..)
|
||||
| ast::ItemKind::Struct(ident, ..)
|
||||
| ast::ItemKind::Union(ident, ..) => self.check_case(cx, "type", ident),
|
||||
ast::ItemKind::Trait(box ast::Trait { ident, .. }) => {
|
||||
self.check_case(cx, "trait", ident)
|
||||
}
|
||||
ast::ItemKind::TraitAlias(ident, _, _) => self.check_case(cx, "trait alias", ident),
|
||||
|
||||
// N.B. This check is only for inherent associated types, so that we don't lint against
|
||||
// trait impls where we should have warned for the trait definition already.
|
||||
ast::ItemKind::Impl(box ast::Impl { of_trait: None, items, .. }) => {
|
||||
for it in items {
|
||||
// FIXME: this doesn't respect `#[allow(..)]` on the item itself.
|
||||
if let ast::AssocItemKind::Type(..) = it.kind {
|
||||
self.check_case(cx, "associated type", &it.ident);
|
||||
if let ast::AssocItemKind::Type(alias) = &it.kind {
|
||||
self.check_case(cx, "associated type", &alias.ident);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -194,8 +196,8 @@ impl EarlyLintPass for NonCamelCaseTypes {
|
|||
}
|
||||
|
||||
fn check_trait_item(&mut self, cx: &EarlyContext<'_>, it: &ast::AssocItem) {
|
||||
if let ast::AssocItemKind::Type(..) = it.kind {
|
||||
self.check_case(cx, "associated type", &it.ident);
|
||||
if let ast::AssocItemKind::Type(alias) = &it.kind {
|
||||
self.check_case(cx, "associated type", &alias.ident);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue