Avoid a kw::Empty usage.

`Option<Symbol>` is a much nicer and idiomatic way of representing "no
name" using empty symbols. And it works naturally for the item ordering
checking because `None < Some(_)` just like the empty string compares
less than all non-empty strings.

changelog: none
This commit is contained in:
Nicholas Nethercote 2025-04-10 16:39:46 +10:00
parent 529bb5f253
commit 822b931652

View file

@ -344,7 +344,7 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
struct CurItem<'a> {
item: &'a Item<'a>,
order: usize,
name: String,
name: Option<String>,
}
let mut cur_t: Option<CurItem<'_>> = None;
@ -368,7 +368,7 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
let ident = if let Some(ident) = item.kind.ident() {
ident
} else if let ItemKind::Impl(_) = item.kind
&& !get_item_name(item).is_empty()
&& get_item_name(item).is_some()
{
rustc_span::Ident::empty() // FIXME: a bit strange, is there a better way to do it?
} else {
@ -495,7 +495,7 @@ fn convert_module_item_kind(value: &ItemKind<'_>) -> SourceItemOrderingModuleIte
/// further in the [Rust Reference, Paths Chapter][rust_ref].
///
/// [rust_ref]: https://doc.rust-lang.org/reference/paths.html#crate-1
fn get_item_name(item: &Item<'_>) -> String {
fn get_item_name(item: &Item<'_>) -> Option<String> {
match item.kind {
ItemKind::Impl(im) => {
if let TyKind::Path(path) = im.self_ty.kind {
@ -515,27 +515,19 @@ fn get_item_name(item: &Item<'_>) -> String {
}
segs.push(String::new());
segs.join("!!")
Some(segs.join("!!"))
},
QPath::TypeRelative(_, _path_seg) => {
// This case doesn't exist in the clippy tests codebase.
String::new()
None
},
QPath::LangItem(_, _) => String::new(),
QPath::LangItem(_, _) => None,
}
} else {
// Impls for anything that isn't a named type can be skipped.
String::new()
None
}
},
// FIXME: `Ident::empty` for anonymous items is a bit strange, is there
// a better way to do it?
_ => item
.kind
.ident()
.unwrap_or(rustc_span::Ident::empty())
.name
.as_str()
.to_owned(),
_ => item.kind.ident().map(|name| name.as_str().to_owned()),
}
}