Port crate name to the new attribute system

This commit is contained in:
Jana Dönszelmann
2025-08-11 11:46:30 +02:00
parent 4b35cde904
commit 59ceb02d65
25 changed files with 458 additions and 366 deletions

View File

@@ -5,7 +5,7 @@ use rustc_hir::attrs::{AttributeKind, ReprAttr};
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::DefId;
use rustc_hir::intravisit::{FnKind, Visitor};
use rustc_hir::{AttrArgs, AttrItem, Attribute, GenericParamKind, PatExprKind, PatKind, find_attr};
use rustc_hir::{Attribute, GenericParamKind, PatExprKind, PatKind, find_attr};
use rustc_middle::hir::nested_filter::All;
use rustc_middle::ty;
use rustc_session::config::CrateType;
@@ -343,35 +343,27 @@ impl<'tcx> LateLintPass<'tcx> for NonSnakeCase {
let crate_ident = if let Some(name) = &cx.tcx.sess.opts.crate_name {
Some(Ident::from_str(name))
} else {
ast::attr::find_by_name(cx.tcx.hir_attrs(hir::CRATE_HIR_ID), sym::crate_name).and_then(
|attr| {
if let Attribute::Unparsed(n) = attr
&& let AttrItem { args: AttrArgs::Eq { eq_span: _, expr: lit }, .. } =
n.as_ref()
&& let ast::LitKind::Str(name, ..) = lit.kind
{
// Discard the double quotes surrounding the literal.
let sp = cx
.sess()
.source_map()
.span_to_snippet(lit.span)
.ok()
.and_then(|snippet| {
let left = snippet.find('"')?;
let right = snippet.rfind('"').map(|pos| snippet.len() - pos)?;
find_attr!(cx.tcx.hir_attrs(hir::CRATE_HIR_ID), AttributeKind::CrateName{name, name_span,..} => (name, name_span)).map(
|(&name, &span)| {
// Discard the double quotes surrounding the literal.
let sp = cx
.sess()
.source_map()
.span_to_snippet(span)
.ok()
.and_then(|snippet| {
let left = snippet.find('"')?;
let right = snippet.rfind('"').map(|pos| snippet.len() - pos)?;
Some(
lit.span
.with_lo(lit.span.lo() + BytePos(left as u32 + 1))
.with_hi(lit.span.hi() - BytePos(right as u32)),
)
})
.unwrap_or(lit.span);
Some(
span
.with_lo(span.lo() + BytePos(left as u32 + 1))
.with_hi(span.hi() - BytePos(right as u32)),
)
})
.unwrap_or(span);
Some(Ident::new(name, sp))
} else {
None
}
Ident::new(name, sp)
},
)
};