Skip to content

Commit f12bf6b

Browse files
some tidy ups
Signed-off-by: Richard Tjokroutomo <richard.tjokro2@gmail.com>
1 parent 35b1fab commit f12bf6b

4 files changed

Lines changed: 37 additions & 51 deletions

File tree

‎compiler/rustc_attr_ir/src/target.rs‎

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_macros::StableHash;
1515
pub enum AstTarget<'a> {
1616
// Target types that may correspond to different kinds of items.
1717
Delegation { target: DelegationAstTarget<'a>, mac: bool },
18-
MacroCall(MacroCallAstTarget<'a>),
18+
MacroCall,
1919

2020
// Target types that correspond exclusively to `AssocItem` kind. Mapping is obtained from `Target::from_assoc_item_kind`.
2121
AssocConst(&'a AssocItemKind),
@@ -79,13 +79,6 @@ pub enum DelegationAstTarget<'a> {
7979
Item(&'a ItemKind),
8080
}
8181

82-
#[derive(Clone, Copy, Debug)]
83-
pub enum MacroCallAstTarget<'a> {
84-
AssocItem(&'a AssocItemKind),
85-
ForeignItem(&'a ForeignItemKind),
86-
Item(&'a ItemKind),
87-
}
88-
8982
#[derive(Copy, Clone, PartialEq, Debug, Eq, StableHash)]
9083
pub enum MethodKind {
9184
/// Method in a `trait Trait` block
@@ -177,9 +170,7 @@ impl<'a> AstTarget<'a> {
177170
ForeignItemKind::Static(_) => AstTarget::ForeignStatic(kind),
178171
ForeignItemKind::Fn(_) => AstTarget::ForeignFn(kind),
179172
ForeignItemKind::TyAlias(_) => AstTarget::ForeignTy(kind),
180-
ForeignItemKind::MacCall(_) => {
181-
AstTarget::MacroCall(MacroCallAstTarget::ForeignItem(kind))
182-
}
173+
ForeignItemKind::MacCall(_) => AstTarget::MacroCall,
183174
}
184175
}
185176

@@ -194,7 +185,7 @@ impl<'a> AstTarget<'a> {
194185
AssocItemKind::DelegationMac(_) => {
195186
AstTarget::Delegation { target: DelegationAstTarget::AssocItem(kind), mac: true }
196187
}
197-
AssocItemKind::MacCall(_) => AstTarget::MacroCall(MacroCallAstTarget::AssocItem(kind)),
188+
AssocItemKind::MacCall(_) => AstTarget::MacroCall,
198189
}
199190
}
200191

@@ -218,17 +209,15 @@ impl<'a> AstTarget<'a> {
218209
ast::ItemKind::Impl(i) => {
219210
AstTarget::Impl { item: kind, of_trait: i.of_trait.is_some() }
220211
}
221-
ast::ItemKind::MacCall(..) => AstTarget::MacroCall(MacroCallAstTarget::Item(kind)),
212+
ast::ItemKind::MacCall(..) => AstTarget::MacroCall,
222213
ast::ItemKind::MacroDef(..) => AstTarget::MacroDef(kind),
223214
ast::ItemKind::Delegation(..) => {
224215
AstTarget::Delegation { target: DelegationAstTarget::Item(kind), mac: false }
225216
}
226217
ast::ItemKind::DelegationMac(..) => {
227218
AstTarget::Delegation { target: DelegationAstTarget::Item(kind), mac: true }
228219
}
229-
ast::ItemKind::TestBinderConstraints(..) => {
230-
AstTarget::MacroCall(MacroCallAstTarget::Item(kind))
231-
}
220+
ast::ItemKind::TestBinderConstraints(..) => AstTarget::MacroCall,
232221
}
233222
}
234223
}

‎compiler/rustc_attr_parsing/src/attributes/link_attrs.rs‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,15 @@ impl CombineAttributeParser for LinkParser {
262262
}
263263

264264
fn finalize_check(cx: &mut FinalizeCheckContext<'_, '_>, attr_span: Span) {
265-
match cx.ast_target {
266-
rustc_attr_ir::target::AstTarget::ForeignMod(ast_item)
267-
if let ItemKind::ForeignMod(fm) = &ast_item =>
268-
{
269-
let abi = fm.abi.map_or(ExternAbi::FALLBACK, |abi| {
265+
let rustc_attr_ir::target::AstTarget::ForeignMod(ast_item) = cx.ast_target else {
266+
return;
267+
};
268+
269+
let ItemKind::ForeignMod(fm) = &ast_item else {
270+
panic!("expected field ForeignMod for AstTarget::ForeignMod");
271+
};
272+
273+
let abi = fm.abi.map_or(ExternAbi::FALLBACK, |abi| {
270274
abi.symbol_unescaped.as_str().parse().unwrap_or_else(|_| {
271275
cx.dcx().span_delayed_bug(
272276
abi.span,
@@ -275,11 +279,8 @@ impl CombineAttributeParser for LinkParser {
275279
ExternAbi::Rust
276280
})
277281
});
278-
if matches!(abi, ExternAbi::Rust) {
279-
cx.emit_lint(UNUSED_ATTRIBUTES, Link, attr_span);
280-
}
281-
}
282-
_ => {}
282+
if matches!(abi, ExternAbi::Rust) {
283+
cx.emit_lint(UNUSED_ATTRIBUTES, Link, attr_span);
283284
}
284285
}
285286
}

‎compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,14 @@ impl SingleAttributeParser for MacroExportParser {
182182
}
183183

184184
fn finalize_check(cx: &mut FinalizeCheckContext<'_, '_>, attr_span: Span) {
185-
match cx.ast_target {
186-
AstTarget::MacroDef(item_kind) => {
187-
if let ItemKind::MacroDef(_, macro_def) = item_kind
188-
&& !macro_def.macro_rules
189-
{
190-
cx.emit_lint(UNUSED_ATTRIBUTES, MacroExport::OnDeclMacro, attr_span);
191-
}
192-
}
193-
_ => {}
185+
let AstTarget::MacroDef(item_kind) = cx.ast_target else {
186+
return;
187+
};
188+
189+
if let ItemKind::MacroDef(_, macro_def) = item_kind
190+
&& !macro_def.macro_rules
191+
{
192+
cx.emit_lint(UNUSED_ATTRIBUTES, MacroExport::OnDeclMacro, attr_span);
194193
}
195194
}
196195
}

‎compiler/rustc_attr_parsing/src/attributes/non_exhaustive.rs‎

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,21 @@ impl NoArgsAttributeParser for NonExhaustiveParser {
2222
const CREATE: fn(Span) -> AttributeKind = AttributeKind::NonExhaustive;
2323

2424
fn finalize_check(cx: &mut FinalizeCheckContext<'_, '_>, attr_span: Span) {
25-
if cx.target != Target::Struct {
25+
let rustc_attr_ir::target::AstTarget::Struct(ast_item) = cx.ast_target else {
2626
return;
27-
}
27+
};
28+
29+
let ItemKind::Struct(_, _, data) = ast_item else {
30+
panic!("expected struct AST target item for AstTarget::Struct");
31+
};
2832

29-
match cx.ast_target {
30-
rustc_attr_ir::target::AstTarget::Struct(ast_item)
31-
if let ItemKind::Struct(_, _, data) = &ast_item =>
32-
{
33-
if let VariantData::Struct { fields, .. } = data
34-
&& fields.iter().any(|f| f.default_value().is_some())
35-
{
36-
cx.emit_err(NonExhaustiveWithDefaultFieldValues {
37-
attr_span,
38-
defn_span: cx.target_span,
39-
});
40-
}
41-
}
42-
_ => {}
33+
if let VariantData::Struct { fields, .. } = data
34+
&& fields.iter().any(|f| f.default_value().is_some())
35+
{
36+
cx.emit_err(NonExhaustiveWithDefaultFieldValues {
37+
attr_span,
38+
defn_span: cx.target_span,
39+
});
4340
}
4441
}
4542
}

0 commit comments

Comments
 (0)