Avoid some unwraps.

By using `@` patterns more.

Also, use `Symbol` more in a couple of errors to avoid some unnecessary
conversions to strings. This even removes a lifetime.
This commit is contained in:
Nicholas Nethercote
2025-05-07 12:50:56 +10:00
parent d81472f266
commit 603766cd72
2 changed files with 45 additions and 36 deletions

View File

@@ -96,9 +96,12 @@ fn parse_repr(cx: &AcceptContext<'_>, param: &MetaItemParser<'_>) -> Option<Repr
// FIXME(jdonszelmann): invert the parsing here to match on the word first and then the // FIXME(jdonszelmann): invert the parsing here to match on the word first and then the
// structure. // structure.
let ident = param.path_without_args().word(); let (name, ident_span) = if let Some(ident) = param.path_without_args().word() {
let ident_span = ident.map_or(rustc_span::DUMMY_SP, |ident| ident.span); (Some(ident.name), ident.span)
let name = ident.map(|ident| ident.name); } else {
(None, rustc_span::DUMMY_SP)
};
let args = param.args(); let args = param.args();
match (name, args) { match (name, args) {
@@ -115,15 +118,15 @@ fn parse_repr(cx: &AcceptContext<'_>, param: &MetaItemParser<'_>) -> Option<Repr
parse_repr_align(cx, l, param.span(), AlignKind::Packed) parse_repr_align(cx, l, param.span(), AlignKind::Packed)
} }
(Some(sym::align | sym::packed), ArgParser::NameValue(l)) => { (Some(name @ sym::align | name @ sym::packed), ArgParser::NameValue(l)) => {
cx.emit_err(session_diagnostics::IncorrectReprFormatGeneric { cx.emit_err(session_diagnostics::IncorrectReprFormatGeneric {
span: param.span(), span: param.span(),
// FIXME(jdonszelmann) can just be a string in the diag type // FIXME(jdonszelmann) can just be a string in the diag type
repr_arg: &ident.unwrap().to_string(), repr_arg: name,
cause: IncorrectReprFormatGenericCause::from_lit_kind( cause: IncorrectReprFormatGenericCause::from_lit_kind(
param.span(), param.span(),
&l.value_as_lit().kind, &l.value_as_lit().kind,
ident.unwrap().as_str(), name,
), ),
}); });
None None
@@ -133,29 +136,35 @@ fn parse_repr(cx: &AcceptContext<'_>, param: &MetaItemParser<'_>) -> Option<Repr
(Some(sym::C), ArgParser::NoArgs) => Some(ReprC), (Some(sym::C), ArgParser::NoArgs) => Some(ReprC),
(Some(sym::simd), ArgParser::NoArgs) => Some(ReprSimd), (Some(sym::simd), ArgParser::NoArgs) => Some(ReprSimd),
(Some(sym::transparent), ArgParser::NoArgs) => Some(ReprTransparent), (Some(sym::transparent), ArgParser::NoArgs) => Some(ReprTransparent),
(Some(i @ int_pat!()), ArgParser::NoArgs) => { (Some(name @ int_pat!()), ArgParser::NoArgs) => {
// int_pat!() should make sure it always parses // int_pat!() should make sure it always parses
Some(ReprInt(int_type_of_word(i).unwrap())) Some(ReprInt(int_type_of_word(name).unwrap()))
} }
( (
Some(sym::Rust | sym::C | sym::simd | sym::transparent | int_pat!()), Some(
name @ sym::Rust
| name @ sym::C
| name @ sym::simd
| name @ sym::transparent
| name @ int_pat!(),
),
ArgParser::NameValue(_), ArgParser::NameValue(_),
) => { ) => {
cx.emit_err(session_diagnostics::InvalidReprHintNoValue { cx.emit_err(session_diagnostics::InvalidReprHintNoValue { span: param.span(), name });
span: param.span(),
name: ident.unwrap().to_string(),
});
None None
} }
( (
Some(sym::Rust | sym::C | sym::simd | sym::transparent | int_pat!()), Some(
name @ sym::Rust
| name @ sym::C
| name @ sym::simd
| name @ sym::transparent
| name @ int_pat!(),
),
ArgParser::List(_), ArgParser::List(_),
) => { ) => {
cx.emit_err(session_diagnostics::InvalidReprHintNoParen { cx.emit_err(session_diagnostics::InvalidReprHintNoParen { span: param.span(), name });
span: param.span(),
name: ident.unwrap().to_string(),
});
None None
} }

View File

@@ -204,7 +204,7 @@ pub(crate) struct InvalidReprHintNoParen {
#[primary_span] #[primary_span]
pub span: Span, pub span: Span,
pub name: String, pub name: Symbol,
} }
#[derive(Diagnostic)] #[derive(Diagnostic)]
@@ -213,7 +213,7 @@ pub(crate) struct InvalidReprHintNoValue {
#[primary_span] #[primary_span]
pub span: Span, pub span: Span,
pub name: String, pub name: Symbol,
} }
/// Error code: E0565 /// Error code: E0565
@@ -295,21 +295,21 @@ pub(crate) struct IncorrectReprFormatExpectInteger {
#[derive(Diagnostic)] #[derive(Diagnostic)]
#[diag(attr_parsing_incorrect_repr_format_generic, code = E0693)] #[diag(attr_parsing_incorrect_repr_format_generic, code = E0693)]
pub(crate) struct IncorrectReprFormatGeneric<'a> { pub(crate) struct IncorrectReprFormatGeneric {
#[primary_span] #[primary_span]
pub span: Span, pub span: Span,
pub repr_arg: &'a str, pub repr_arg: Symbol,
#[subdiagnostic] #[subdiagnostic]
pub cause: Option<IncorrectReprFormatGenericCause<'a>>, pub cause: Option<IncorrectReprFormatGenericCause>,
} }
#[derive(Subdiagnostic)] #[derive(Subdiagnostic)]
pub(crate) enum IncorrectReprFormatGenericCause<'a> { pub(crate) enum IncorrectReprFormatGenericCause {
#[suggestion( #[suggestion(
attr_parsing_suggestion, attr_parsing_suggestion,
code = "{name}({int})", code = "{name}({value})",
applicability = "machine-applicable" applicability = "machine-applicable"
)] )]
Int { Int {
@@ -317,15 +317,15 @@ pub(crate) enum IncorrectReprFormatGenericCause<'a> {
span: Span, span: Span,
#[skip_arg] #[skip_arg]
name: &'a str, name: Symbol,
#[skip_arg] #[skip_arg]
int: u128, value: u128,
}, },
#[suggestion( #[suggestion(
attr_parsing_suggestion, attr_parsing_suggestion,
code = "{name}({symbol})", code = "{name}({value})",
applicability = "machine-applicable" applicability = "machine-applicable"
)] )]
Symbol { Symbol {
@@ -333,20 +333,20 @@ pub(crate) enum IncorrectReprFormatGenericCause<'a> {
span: Span, span: Span,
#[skip_arg] #[skip_arg]
name: &'a str, name: Symbol,
#[skip_arg] #[skip_arg]
symbol: Symbol, value: Symbol,
}, },
} }
impl<'a> IncorrectReprFormatGenericCause<'a> { impl IncorrectReprFormatGenericCause {
pub(crate) fn from_lit_kind(span: Span, kind: &ast::LitKind, name: &'a str) -> Option<Self> { pub(crate) fn from_lit_kind(span: Span, kind: &ast::LitKind, name: Symbol) -> Option<Self> {
match kind { match *kind {
ast::LitKind::Int(int, ast::LitIntType::Unsuffixed) => { ast::LitKind::Int(value, ast::LitIntType::Unsuffixed) => {
Some(Self::Int { span, name, int: int.get() }) Some(Self::Int { span, name, value: value.get() })
} }
ast::LitKind::Str(symbol, _) => Some(Self::Symbol { span, name, symbol: *symbol }), ast::LitKind::Str(value, _) => Some(Self::Symbol { span, name, value }),
_ => None, _ => None,
} }
} }