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
// structure.
let ident = param.path_without_args().word();
let ident_span = ident.map_or(rustc_span::DUMMY_SP, |ident| ident.span);
let name = ident.map(|ident| ident.name);
let (name, ident_span) = if let Some(ident) = param.path_without_args().word() {
(Some(ident.name), ident.span)
} else {
(None, rustc_span::DUMMY_SP)
};
let args = param.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)
}
(Some(sym::align | sym::packed), ArgParser::NameValue(l)) => {
(Some(name @ sym::align | name @ sym::packed), ArgParser::NameValue(l)) => {
cx.emit_err(session_diagnostics::IncorrectReprFormatGeneric {
span: param.span(),
// 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(
param.span(),
&l.value_as_lit().kind,
ident.unwrap().as_str(),
name,
),
});
None
@ -133,29 +136,35 @@ fn parse_repr(cx: &AcceptContext<'_>, param: &MetaItemParser<'_>) -> Option<Repr
(Some(sym::C), ArgParser::NoArgs) => Some(ReprC),
(Some(sym::simd), ArgParser::NoArgs) => Some(ReprSimd),
(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
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(_),
) => {
cx.emit_err(session_diagnostics::InvalidReprHintNoValue {
span: param.span(),
name: ident.unwrap().to_string(),
});
cx.emit_err(session_diagnostics::InvalidReprHintNoValue { span: param.span(), name });
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(_),
) => {
cx.emit_err(session_diagnostics::InvalidReprHintNoParen {
span: param.span(),
name: ident.unwrap().to_string(),
});
cx.emit_err(session_diagnostics::InvalidReprHintNoParen { span: param.span(), name });
None
}

View File

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