Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a672c2e

Browse files
committedMar 16, 2025·
Address review comments.
1 parent 58f4c9d commit a672c2e

File tree

8 files changed

+39
-34
lines changed

8 files changed

+39
-34
lines changed
 

‎compiler/rustc_ast_lowering/src/item.rs

+21-20
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
111111
}
112112

113113
fn lower_foreign_item(&mut self, item: &ForeignItem) {
114-
assert_ne!(item.ident.name, kw::Empty);
114+
debug_assert_ne!(item.ident.name, kw::Empty);
115115
self.with_lctx(item.id, |lctx| hir::OwnerNode::ForeignItem(lctx.lower_foreign_item(item)))
116116
}
117117
}
@@ -180,26 +180,26 @@ impl<'hir> LoweringContext<'_, 'hir> {
180180
) -> hir::ItemKind<'hir> {
181181
match i {
182182
ItemKind::ExternCrate(orig_name) => {
183-
assert_ne!(ident.name, kw::Empty);
183+
debug_assert_ne!(ident.name, kw::Empty);
184184
let ident = self.lower_ident(ident);
185185
hir::ItemKind::ExternCrate(*orig_name, ident)
186186
}
187187
ItemKind::Use(use_tree) => {
188-
assert_eq!(ident.name, kw::Empty);
188+
debug_assert_eq!(ident.name, kw::Empty);
189189
// Start with an empty prefix.
190190
let prefix = Path { segments: ThinVec::new(), span: use_tree.span, tokens: None };
191191

192192
self.lower_use_tree(use_tree, &prefix, id, vis_span, attrs)
193193
}
194194
ItemKind::Static(box ast::StaticItem { ty: t, safety: _, mutability: m, expr: e }) => {
195-
assert_ne!(ident.name, kw::Empty);
195+
debug_assert_ne!(ident.name, kw::Empty);
196196
let ident = self.lower_ident(ident);
197197
let (ty, body_id) =
198198
self.lower_const_item(t, span, e.as_deref(), ImplTraitPosition::StaticTy);
199199
hir::ItemKind::Static(ident, ty, *m, body_id)
200200
}
201201
ItemKind::Const(box ast::ConstItem { generics, ty, expr, .. }) => {
202-
assert_ne!(ident.name, kw::Empty);
202+
debug_assert_ne!(ident.name, kw::Empty);
203203
let ident = self.lower_ident(ident);
204204
let (generics, (ty, body_id)) = self.lower_generics(
205205
generics,
@@ -219,7 +219,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
219219
define_opaque,
220220
..
221221
}) => {
222-
assert_ne!(ident.name, kw::Empty);
222+
debug_assert_ne!(ident.name, kw::Empty);
223223
self.with_new_scopes(*fn_sig_span, |this| {
224224
// Note: we don't need to change the return type from `T` to
225225
// `impl Future<Output = T>` here because lower_body
@@ -258,7 +258,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
258258
})
259259
}
260260
ItemKind::Mod(_, mod_kind) => {
261-
assert_ne!(ident.name, kw::Empty);
261+
debug_assert_ne!(ident.name, kw::Empty);
262262
let ident = self.lower_ident(ident);
263263
match mod_kind {
264264
ModKind::Loaded(items, _, spans, _) => {
@@ -268,7 +268,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
268268
}
269269
}
270270
ItemKind::ForeignMod(fm) => {
271-
assert_eq!(ident.name, kw::Empty);
271+
debug_assert_eq!(ident.name, kw::Empty);
272272
hir::ItemKind::ForeignMod {
273273
abi: fm.abi.map_or(ExternAbi::FALLBACK, |abi| self.lower_abi(abi)),
274274
items: self
@@ -277,14 +277,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
277277
}
278278
}
279279
ItemKind::GlobalAsm(asm) => {
280-
assert_eq!(ident.name, kw::Empty);
280+
debug_assert_eq!(ident.name, kw::Empty);
281281
let asm = self.lower_inline_asm(span, asm);
282282
let fake_body =
283283
self.lower_body(|this| (&[], this.expr(span, hir::ExprKind::InlineAsm(asm))));
284284
hir::ItemKind::GlobalAsm { asm, fake_body }
285285
}
286286
ItemKind::TyAlias(box TyAlias { generics, where_clauses, ty, .. }) => {
287-
assert_ne!(ident.name, kw::Empty);
287+
debug_assert_ne!(ident.name, kw::Empty);
288288
// We lower
289289
//
290290
// type Foo = impl Trait
@@ -322,7 +322,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
322322
hir::ItemKind::TyAlias(ident, ty, generics)
323323
}
324324
ItemKind::Enum(enum_definition, generics) => {
325-
assert_ne!(ident.name, kw::Empty);
325+
debug_assert_ne!(ident.name, kw::Empty);
326326
let ident = self.lower_ident(ident);
327327
let (generics, variants) = self.lower_generics(
328328
generics,
@@ -337,7 +337,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
337337
hir::ItemKind::Enum(ident, hir::EnumDef { variants }, generics)
338338
}
339339
ItemKind::Struct(struct_def, generics) => {
340-
assert_ne!(ident.name, kw::Empty);
340+
debug_assert_ne!(ident.name, kw::Empty);
341341
let ident = self.lower_ident(ident);
342342
let (generics, struct_def) = self.lower_generics(
343343
generics,
@@ -348,7 +348,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
348348
hir::ItemKind::Struct(ident, struct_def, generics)
349349
}
350350
ItemKind::Union(vdata, generics) => {
351-
assert_ne!(ident.name, kw::Empty);
351+
debug_assert_ne!(ident.name, kw::Empty);
352352
let ident = self.lower_ident(ident);
353353
let (generics, vdata) = self.lower_generics(
354354
generics,
@@ -368,7 +368,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
368368
self_ty: ty,
369369
items: impl_items,
370370
}) => {
371-
assert_eq!(ident.name, kw::Empty);
371+
debug_assert_eq!(ident.name, kw::Empty);
372372
// Lower the "impl header" first. This ordering is important
373373
// for in-band lifetimes! Consider `'a` here:
374374
//
@@ -434,7 +434,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
434434
}))
435435
}
436436
ItemKind::Trait(box Trait { is_auto, safety, generics, bounds, items }) => {
437-
assert_ne!(ident.name, kw::Empty);
437+
debug_assert_ne!(ident.name, kw::Empty);
438438
let ident = self.lower_ident(ident);
439439
let (generics, (safety, items, bounds)) = self.lower_generics(
440440
generics,
@@ -455,7 +455,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
455455
hir::ItemKind::Trait(*is_auto, safety, ident, generics, bounds, items)
456456
}
457457
ItemKind::TraitAlias(generics, bounds) => {
458-
assert_ne!(ident.name, kw::Empty);
458+
debug_assert_ne!(ident.name, kw::Empty);
459459
let ident = self.lower_ident(ident);
460460
let (generics, bounds) = self.lower_generics(
461461
generics,
@@ -471,7 +471,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
471471
hir::ItemKind::TraitAlias(ident, generics, bounds)
472472
}
473473
ItemKind::MacroDef(MacroDef { body, macro_rules }) => {
474-
assert_ne!(ident.name, kw::Empty);
474+
debug_assert_ne!(ident.name, kw::Empty);
475475
let ident = self.lower_ident(ident);
476476
let body = P(self.lower_delim_args(body));
477477
let def_id = self.local_def_id(id);
@@ -486,7 +486,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
486486
hir::ItemKind::Macro(ident, macro_def, macro_kind)
487487
}
488488
ItemKind::Delegation(box delegation) => {
489-
assert_ne!(ident.name, kw::Empty);
489+
debug_assert_ne!(ident.name, kw::Empty);
490490
let ident = self.lower_ident(ident);
491491
let delegation_results = self.lower_delegation(delegation, id);
492492
hir::ItemKind::Fn {
@@ -545,6 +545,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
545545

546546
let res = self.lower_import_res(id, path.span);
547547
let path = self.lower_use_path(res, &path, ParamMode::Explicit);
548+
let ident = self.lower_ident(ident);
548549
hir::ItemKind::Use(path, hir::UseKind::Single(ident))
549550
}
550551
UseTreeKind::Glob => {
@@ -797,7 +798,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
797798
}
798799

799800
fn lower_trait_item(&mut self, i: &AssocItem) -> &'hir hir::TraitItem<'hir> {
800-
assert_ne!(i.ident.name, kw::Empty);
801+
debug_assert_ne!(i.ident.name, kw::Empty);
801802
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
802803
let attrs = self.lower_attrs(hir_id, &i.attrs, i.span);
803804
let trait_item_def_id = hir_id.expect_owner();
@@ -945,7 +946,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
945946
}
946947

947948
fn lower_impl_item(&mut self, i: &AssocItem) -> &'hir hir::ImplItem<'hir> {
948-
assert_ne!(i.ident.name, kw::Empty);
949+
debug_assert_ne!(i.ident.name, kw::Empty);
949950
// Since `default impl` is not yet implemented, this is always true in impls.
950951
let has_value = true;
951952
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);

‎compiler/rustc_passes/src/check_attr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2784,7 +2784,7 @@ fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
27842784
.map(|id| tcx.hir_item(id))
27852785
.find(|item| !item.span.is_dummy()) // Skip prelude `use`s
27862786
.map(|item| errors::ItemFollowingInnerAttr {
2787-
span: item.kind.ident().unwrap().span,
2787+
span: if let Some(ident) = item.kind.ident() { ident.span } else { item.span },
27882788
kind: item.kind.descr(),
27892789
});
27902790
let err = tcx.dcx().create_err(errors::InvalidAttrAtCrateLevel {

‎src/librustdoc/clean/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2794,7 +2794,7 @@ fn clean_maybe_renamed_item<'tcx>(
27942794

27952795
let def_id = item.owner_id.to_def_id();
27962796
let mut name = renamed.unwrap_or_else(|| {
2797-
// using kw::Empty is a bit of a hack
2797+
// FIXME: using kw::Empty is a bit of a hack
27982798
cx.tcx.hir_opt_name(item.hir_id()).unwrap_or(kw::Empty)
27992799
});
28002800

‎src/librustdoc/visit_ast.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -509,8 +509,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
509509
hir::ItemKind::Const(..) => {
510510
// Underscore constants do not correspond to a nameable item and
511511
// so are never useful in documentation.
512-
let name = get_name();
513-
if name != kw::Underscore {
512+
if get_name() != kw::Underscore {
514513
self.add_to_current_mod(item, renamed, import_id);
515514
}
516515
}

‎src/tools/clippy/clippy_lints/src/arbitrary_source_item_ordering.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,12 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
351351
continue;
352352
}
353353

354-
// Sorting trait impls for unnamed types makes no sense.
355-
let ident = if let ItemKind::Impl(_) = item.kind && !get_item_name(item).is_empty() {
356-
rustc_span::Ident::empty() // a bit strange, is there a better way to do it?
357-
} else if let Some(ident) = item.kind.ident() {
354+
let ident = if let Some(ident) = item.kind.ident() {
358355
ident
356+
} else if let ItemKind::Impl(_) = item.kind
357+
&& !get_item_name(item).is_empty()
358+
{
359+
rustc_span::Ident::empty() // FIXME: a bit strange, is there a better way to do it?
359360
} else {
360361
continue;
361362
};
@@ -498,7 +499,8 @@ fn get_item_name(item: &Item<'_>) -> String {
498499
String::new()
499500
}
500501
},
501-
// `Ident::empty` for anonymous items is a bit strange, is there a better way to do it?
502+
// FIXME: `Ident::empty` for anonymous items is a bit strange, is there
503+
// a better way to do it?
502504
_ => item.kind.ident().unwrap_or(rustc_span::Ident::empty()).name.as_str().to_owned(),
503505
}
504506
}

‎src/tools/clippy/clippy_lints/src/attrs/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ mod utils;
1616
use clippy_config::Conf;
1717
use clippy_utils::msrvs::{self, Msrv, MsrvStack};
1818
use rustc_ast::{self as ast, Attribute, MetaItemInner, MetaItemKind};
19-
use rustc_hir::{ImplItem, Item, TraitItem};
19+
use rustc_hir::{ImplItem, Item, ItemKind, TraitItem};
2020
use rustc_lint::{EarlyContext, EarlyLintPass, LateContext, LateLintPass};
2121
use rustc_session::impl_lint_pass;
2222
use rustc_span::sym;
@@ -466,8 +466,7 @@ impl Attributes {
466466
impl<'tcx> LateLintPass<'tcx> for Attributes {
467467
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
468468
let attrs = cx.tcx.hir_attrs(item.hir_id());
469-
if is_relevant_item(cx, item) {
470-
let (ident, _, _, _) = item.expect_fn();
469+
if let ItemKind::Fn { ident, .. } = item.kind && is_relevant_item(cx, item) {
471470
inline_always::check(cx, item.span, ident.name, attrs);
472471
}
473472
repr_attributes::check(cx, item.span, attrs, self.msrv);

‎src/tools/clippy/clippy_lints/src/redundant_pub_crate.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ impl<'tcx> LateLintPass<'tcx> for RedundantPubCrate {
5252
&& is_not_macro_export(item)
5353
&& !item.span.in_external_macro(cx.sess().source_map())
5454
{
55-
let span = item.span.with_hi(item.kind.ident().unwrap().span.hi());
55+
// FIXME: `DUMMY_SP` isn't right here, because it causes the
56+
// resulting span to begin at the start of the file.
57+
let span = item.span.with_hi(
58+
item.kind.ident().map(|ident| ident.span.hi()).unwrap_or(rustc_span::DUMMY_SP.hi())
59+
);
5660
let descr = cx.tcx.def_kind(item.owner_id).descr(item.owner_id.to_def_id());
5761
span_lint_and_then(
5862
cx,

‎src/tools/clippy/clippy_utils/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ fn local_item_children_by_name(tcx: TyCtxt<'_>, local_id: LocalDefId, name: Symb
644644
let root_mod;
645645
let item_kind = match tcx.hir_node_by_def_id(local_id) {
646646
Node::Crate(r#mod) => {
647-
root_mod = ItemKind::Mod(Ident::empty(), r#mod);
647+
root_mod = ItemKind::Mod(Ident::dummy(), r#mod);
648648
&root_mod
649649
},
650650
Node::Item(item) => &item.kind,

0 commit comments

Comments
 (0)
Please sign in to comment.