Skip to content

Commit c063203

Browse files
committed
ast: Fix naming conventions in AST structures
TraitKind -> Trait TyAliasKind -> TyAlias ImplKind -> Impl FnKind -> Fn All `*Kind`s in AST are supposed to be enums. Tuple structs are converted to braced structs for the types above, and fields are reordered in syntactic order. Also, mutable AST visitor now correctly visit spans in defaultness, unsafety, impl polarity and constness.
1 parent e674d0a commit c063203

File tree

5 files changed

+29
-22
lines changed

5 files changed

+29
-22
lines changed

clippy_lints/src/doc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
55
use clippy_utils::{is_entrypoint_fn, is_expn_of, match_panic_def_id, method_chain_args, return_ty};
66
use if_chain::if_chain;
77
use itertools::Itertools;
8-
use rustc_ast::ast::{Async, AttrKind, Attribute, FnKind, FnRetTy, ItemKind};
8+
use rustc_ast::ast::{Async, AttrKind, Attribute, Fn, FnRetTy, ItemKind};
99
use rustc_ast::token::CommentKind;
1010
use rustc_data_structures::fx::FxHashSet;
1111
use rustc_data_structures::sync::Lrc;
@@ -639,7 +639,7 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) {
639639
| ItemKind::ExternCrate(..)
640640
| ItemKind::ForeignMod(..) => return false,
641641
// We found a main function ...
642-
ItemKind::Fn(box FnKind(_, sig, _, Some(block))) if item.ident.name == sym::main => {
642+
ItemKind::Fn(box Fn { sig, body: Some(block), .. }) if item.ident.name == sym::main => {
643643
let is_async = matches!(sig.header.asyncness, Async::Yes { .. });
644644
let returns_nothing = match &sig.decl.output {
645645
FnRetTy::Default(..) => true,

clippy_lints/src/excessive_bools.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
22
use clippy_utils::in_macro;
3-
use rustc_ast::ast::{AssocItemKind, Extern, FnKind, FnSig, ImplKind, Item, ItemKind, TraitKind, Ty, TyKind};
3+
use rustc_ast::ast::{AssocItemKind, Extern, Fn, FnSig, Impl, Item, ItemKind, Trait, Ty, TyKind};
44
use rustc_lint::{EarlyContext, EarlyLintPass};
55
use rustc_session::{declare_tool_lint, impl_lint_pass};
66
use rustc_span::{sym, Span};
@@ -162,17 +162,17 @@ impl EarlyLintPass for ExcessiveBools {
162162
);
163163
}
164164
},
165-
ItemKind::Impl(box ImplKind {
165+
ItemKind::Impl(box Impl {
166166
of_trait: None, items, ..
167167
})
168-
| ItemKind::Trait(box TraitKind(.., items)) => {
168+
| ItemKind::Trait(box Trait { items, .. }) => {
169169
for item in items {
170-
if let AssocItemKind::Fn(box FnKind(_, fn_sig, _, _)) = &item.kind {
171-
self.check_fn_sig(cx, fn_sig, item.span);
170+
if let AssocItemKind::Fn(box Fn { sig, .. }) = &item.kind {
171+
self.check_fn_sig(cx, sig, item.span);
172172
}
173173
}
174174
},
175-
ItemKind::Fn(box FnKind(_, fn_sig, _, _)) => self.check_fn_sig(cx, fn_sig, item.span),
175+
ItemKind::Fn(box Fn { sig, .. }) => self.check_fn_sig(cx, sig, item.span),
176176
_ => (),
177177
}
178178
}

clippy_lints/src/non_expressive_names.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
22
use rustc_ast::ast::{
3-
Arm, AssocItem, AssocItemKind, Attribute, Block, FnDecl, FnKind, Item, ItemKind, Local, Pat, PatKind,
3+
self, Arm, AssocItem, AssocItemKind, Attribute, Block, FnDecl, Item, ItemKind, Local, Pat, PatKind,
44
};
55
use rustc_ast::visit::{walk_block, walk_expr, walk_pat, Visitor};
66
use rustc_lint::{EarlyContext, EarlyLintPass};
@@ -357,7 +357,7 @@ impl EarlyLintPass for NonExpressiveNames {
357357
return;
358358
}
359359

360-
if let ItemKind::Fn(box FnKind(_, ref sig, _, Some(ref blk))) = item.kind {
360+
if let ItemKind::Fn(box ast::Fn { ref sig, body: Some(ref blk), .. }) = item.kind {
361361
do_check(self, cx, &item.attrs, &sig.decl, blk);
362362
}
363363
}
@@ -367,7 +367,7 @@ impl EarlyLintPass for NonExpressiveNames {
367367
return;
368368
}
369369

370-
if let AssocItemKind::Fn(box FnKind(_, ref sig, _, Some(ref blk))) = item.kind {
370+
if let AssocItemKind::Fn(box ast::Fn { ref sig, body: Some(ref blk), .. }) = item.kind {
371371
do_check(self, cx, &item.attrs, &sig.decl, blk);
372372
}
373373
}

clippy_lints/src/write.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::ops::{Deref, Range};
44

55
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then};
66
use clippy_utils::source::{snippet_opt, snippet_with_applicability};
7-
use rustc_ast::ast::{Expr, ExprKind, ImplKind, Item, ItemKind, MacCall, Path, StrLit, StrStyle};
7+
use rustc_ast::ast::{Expr, ExprKind, Impl, Item, ItemKind, MacCall, Path, StrLit, StrStyle};
88
use rustc_ast::token::{self, LitKind};
99
use rustc_ast::tokenstream::TokenStream;
1010
use rustc_errors::Applicability;
@@ -243,7 +243,7 @@ impl_lint_pass!(Write => [
243243

244244
impl EarlyLintPass for Write {
245245
fn check_item(&mut self, _: &EarlyContext<'_>, item: &Item) {
246-
if let ItemKind::Impl(box ImplKind {
246+
if let ItemKind::Impl(box Impl {
247247
of_trait: Some(trait_ref),
248248
..
249249
}) = &item.kind

clippy_utils/src/ast_utils.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,8 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
250250
(Use(l), Use(r)) => eq_use_tree(l, r),
251251
(Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
252252
(Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
253-
(Fn(box FnKind(ld, lf, lg, lb)), Fn(box FnKind(rd, rf, rg, rb))) => {
253+
(Fn(box ast::Fn { defaultness: ld, sig: lf, generics: lg, body: lb }),
254+
Fn(box ast::Fn { defaultness: rd, sig: rf, generics: rg, body: rb })) => {
254255
eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
255256
},
256257
(Mod(lu, lmk), Mod(ru, rmk)) => {
@@ -266,7 +267,8 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
266267
(ForeignMod(l), ForeignMod(r)) => {
267268
both(&l.abi, &r.abi, eq_str_lit) && over(&l.items, &r.items, |l, r| eq_item(l, r, eq_foreign_item_kind))
268269
},
269-
(TyAlias(box TyAliasKind(ld, lg, lb, lt)), TyAlias(box TyAliasKind(rd, rg, rb, rt))) => {
270+
(TyAlias(box ast::TyAlias { defaultness: ld, generics: lg, bounds: lb, ty: lt }),
271+
TyAlias(box ast::TyAlias { defaultness: rd, generics: rg, bounds: rb, ty: rt })) => {
270272
eq_defaultness(*ld, *rd)
271273
&& eq_generics(lg, rg)
272274
&& over(lb, rb, eq_generic_bound)
@@ -276,7 +278,8 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
276278
(Struct(lv, lg), Struct(rv, rg)) | (Union(lv, lg), Union(rv, rg)) => {
277279
eq_variant_data(lv, rv) && eq_generics(lg, rg)
278280
},
279-
(Trait(box TraitKind(la, lu, lg, lb, li)), Trait(box TraitKind(ra, ru, rg, rb, ri))) => {
281+
(Trait(box ast::Trait { is_auto: la, unsafety: lu, generics: lg, bounds: lb, items: li }),
282+
Trait(box ast::Trait { is_auto: ra, unsafety: ru, generics: rg, bounds: rb, items: ri })) => {
280283
la == ra
281284
&& matches!(lu, Unsafe::No) == matches!(ru, Unsafe::No)
282285
&& eq_generics(lg, rg)
@@ -285,7 +288,7 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
285288
},
286289
(TraitAlias(lg, lb), TraitAlias(rg, rb)) => eq_generics(lg, rg) && over(lb, rb, eq_generic_bound),
287290
(
288-
Impl(box ImplKind {
291+
Impl(box ast::Impl {
289292
unsafety: lu,
290293
polarity: lp,
291294
defaultness: ld,
@@ -295,7 +298,7 @@ pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
295298
self_ty: lst,
296299
items: li,
297300
}),
298-
Impl(box ImplKind {
301+
Impl(box ast::Impl {
299302
unsafety: ru,
300303
polarity: rp,
301304
defaultness: rd,
@@ -325,10 +328,12 @@ pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool {
325328
use ForeignItemKind::*;
326329
match (l, r) {
327330
(Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
328-
(Fn(box FnKind(ld, lf, lg, lb)), Fn(box FnKind(rd, rf, rg, rb))) => {
331+
(Fn(box ast::Fn { defaultness: ld, sig: lf, generics: lg, body: lb }),
332+
Fn(box ast::Fn { defaultness: rd, sig: rf, generics: rg, body: rb })) => {
329333
eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
330334
},
331-
(TyAlias(box TyAliasKind(ld, lg, lb, lt)), TyAlias(box TyAliasKind(rd, rg, rb, rt))) => {
335+
(TyAlias(box ast::TyAlias { defaultness: ld, generics: lg, bounds: lb, ty: lt }),
336+
TyAlias(box ast::TyAlias { defaultness: rd, generics: rg, bounds: rb, ty: rt })) => {
332337
eq_defaultness(*ld, *rd)
333338
&& eq_generics(lg, rg)
334339
&& over(lb, rb, eq_generic_bound)
@@ -343,10 +348,12 @@ pub fn eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool {
343348
use AssocItemKind::*;
344349
match (l, r) {
345350
(Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
346-
(Fn(box FnKind(ld, lf, lg, lb)), Fn(box FnKind(rd, rf, rg, rb))) => {
351+
(Fn(box ast::Fn { defaultness: ld, sig: lf, generics: lg, body: lb }),
352+
Fn(box ast::Fn { defaultness: rd, sig: rf, generics: rg, body: rb })) => {
347353
eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
348354
},
349-
(TyAlias(box TyAliasKind(ld, lg, lb, lt)), TyAlias(box TyAliasKind(rd, rg, rb, rt))) => {
355+
(TyAlias(box ast::TyAlias { defaultness: ld, generics: lg, bounds: lb, ty: lt }),
356+
TyAlias(box ast::TyAlias { defaultness: rd, generics: rg, bounds: rb, ty: rt })) => {
350357
eq_defaultness(*ld, *rd)
351358
&& eq_generics(lg, rg)
352359
&& over(lb, rb, eq_generic_bound)

0 commit comments

Comments
 (0)