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 540fb22

Browse files
authoredApr 18, 2025
Rollup merge of #139615 - nnethercote:rm-name_or_empty, r=jdonszelmann
Remove `name_or_empty` Another step towards #137978. r? ``@jdonszelmann``
2 parents 5e8bc7f + 846c10f commit 540fb22

File tree

42 files changed

+309
-226
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+309
-226
lines changed
 

‎compiler/rustc_ast/src/attr/mod.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ impl MetaItem {
305305
if let [PathSegment { ident, .. }] = self.path.segments[..] { Some(ident) } else { None }
306306
}
307307

308-
pub fn name_or_empty(&self) -> Symbol {
309-
self.ident().unwrap_or_else(Ident::empty).name
308+
pub fn name(&self) -> Option<Symbol> {
309+
self.ident().map(|ident| ident.name)
310310
}
311311

312312
pub fn has_name(&self, name: Symbol) -> bool {
@@ -511,13 +511,14 @@ impl MetaItemInner {
511511
}
512512
}
513513

514-
/// For a single-segment meta item, returns its name; otherwise, returns `None`.
514+
/// For a single-segment meta item, returns its identifier; otherwise, returns `None`.
515515
pub fn ident(&self) -> Option<Ident> {
516516
self.meta_item().and_then(|meta_item| meta_item.ident())
517517
}
518518

519-
pub fn name_or_empty(&self) -> Symbol {
520-
self.ident().unwrap_or_else(Ident::empty).name
519+
/// For a single-segment meta item, returns its name; otherwise, returns `None`.
520+
pub fn name(&self) -> Option<Symbol> {
521+
self.ident().map(|ident| ident.name)
521522
}
522523

523524
/// Returns `true` if this list item is a MetaItem with a name of `name`.
@@ -738,9 +739,9 @@ pub trait AttributeExt: Debug {
738739
fn id(&self) -> AttrId;
739740

740741
/// For a single-segment attribute (i.e., `#[attr]` and not `#[path::atrr]`),
741-
/// return the name of the attribute, else return the empty identifier.
742-
fn name_or_empty(&self) -> Symbol {
743-
self.ident().unwrap_or_else(Ident::empty).name
742+
/// return the name of the attribute; otherwise, returns `None`.
743+
fn name(&self) -> Option<Symbol> {
744+
self.ident().map(|ident| ident.name)
744745
}
745746

746747
/// Get the meta item list, `#[attr(meta item list)]`
@@ -752,7 +753,7 @@ pub trait AttributeExt: Debug {
752753
/// Gets the span of the value literal, as string, when using `#[attr = value]`
753754
fn value_span(&self) -> Option<Span>;
754755

755-
/// For a single-segment attribute, returns its name; otherwise, returns `None`.
756+
/// For a single-segment attribute, returns its ident; otherwise, returns `None`.
756757
fn ident(&self) -> Option<Ident>;
757758

758759
/// Checks whether the path of this attribute matches the name.
@@ -770,6 +771,11 @@ pub trait AttributeExt: Debug {
770771
self.ident().map(|x| x.name == name).unwrap_or(false)
771772
}
772773

774+
#[inline]
775+
fn has_any_name(&self, names: &[Symbol]) -> bool {
776+
names.iter().any(|&name| self.has_name(name))
777+
}
778+
773779
/// get the span of the entire attribute
774780
fn span(&self) -> Span;
775781

@@ -813,8 +819,8 @@ impl Attribute {
813819
AttributeExt::id(self)
814820
}
815821

816-
pub fn name_or_empty(&self) -> Symbol {
817-
AttributeExt::name_or_empty(self)
822+
pub fn name(&self) -> Option<Symbol> {
823+
AttributeExt::name(self)
818824
}
819825

820826
pub fn meta_item_list(&self) -> Option<ThinVec<MetaItemInner>> {
@@ -846,6 +852,11 @@ impl Attribute {
846852
AttributeExt::has_name(self, name)
847853
}
848854

855+
#[inline]
856+
pub fn has_any_name(&self, names: &[Symbol]) -> bool {
857+
AttributeExt::has_any_name(self, names)
858+
}
859+
849860
pub fn span(&self) -> Span {
850861
AttributeExt::span(self)
851862
}

‎compiler/rustc_ast_lowering/src/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1310,7 +1310,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
13101310
// create a fake body so that the entire rest of the compiler doesn't have to deal with
13111311
// this as a special case.
13121312
return self.lower_fn_body(decl, contract, |this| {
1313-
if attrs.iter().any(|a| a.name_or_empty() == sym::rustc_intrinsic) {
1313+
if attrs.iter().any(|a| a.has_name(sym::rustc_intrinsic)) {
13141314
let span = this.lower_span(span);
13151315
let empty_block = hir::Block {
13161316
hir_id: this.next_id(),

0 commit comments

Comments
 (0)
Please sign in to comment.