Skip to content

Avoid some uses of empty identifiers #14580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 11, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 23 additions & 33 deletions clippy_lints/src/arbitrary_source_item_ordering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -365,28 +365,26 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
continue;
}

let ident = if let Some(ident) = item.kind.ident() {
ident
if let Some(ident) = item.kind.ident() {
if ident.name.as_str().starts_with('_') {
// Filters out unnamed macro-like impls for various derives,
// e.g. serde::Serialize or num_derive::FromPrimitive.
continue;
}

if ident.name == rustc_span::sym::std && item.span.is_dummy() {
if let ItemKind::ExternCrate(None, _) = item.kind {
// Filters the auto-included Rust standard library.
continue;
}
println!("Unknown item: {item:?}");
}
} 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?
// keep going below
} else {
continue;
};

if ident.name.as_str().starts_with('_') {
// Filters out unnamed macro-like impls for various derives,
// e.g. serde::Serialize or num_derive::FromPrimitive.
continue;
}

if ident.name == rustc_span::sym::std && item.span.is_dummy() {
if let ItemKind::ExternCrate(None, _) = item.kind {
// Filters the auto-included Rust standard library.
continue;
}
println!("Unknown item: {item:?}");
}

let item_kind = convert_module_item_kind(&item.kind);
Expand Down Expand Up @@ -495,7 +493,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 {
Expand All @@ -515,27 +513,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()),
}
}