Skip to content

Commit 603766c

Browse files
committed
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.
1 parent d81472f commit 603766c

File tree

2 files changed

+45
-36
lines changed

2 files changed

+45
-36
lines changed

compiler/rustc_attr_parsing/src/attributes/repr.rs

+27-18
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,12 @@ fn parse_repr(cx: &AcceptContext<'_>, param: &MetaItemParser<'_>) -> Option<Repr
9696

9797
// FIXME(jdonszelmann): invert the parsing here to match on the word first and then the
9898
// structure.
99-
let ident = param.path_without_args().word();
100-
let ident_span = ident.map_or(rustc_span::DUMMY_SP, |ident| ident.span);
101-
let name = ident.map(|ident| ident.name);
99+
let (name, ident_span) = if let Some(ident) = param.path_without_args().word() {
100+
(Some(ident.name), ident.span)
101+
} else {
102+
(None, rustc_span::DUMMY_SP)
103+
};
104+
102105
let args = param.args();
103106

104107
match (name, args) {
@@ -115,15 +118,15 @@ fn parse_repr(cx: &AcceptContext<'_>, param: &MetaItemParser<'_>) -> Option<Repr
115118
parse_repr_align(cx, l, param.span(), AlignKind::Packed)
116119
}
117120

118-
(Some(sym::align | sym::packed), ArgParser::NameValue(l)) => {
121+
(Some(name @ sym::align | name @ sym::packed), ArgParser::NameValue(l)) => {
119122
cx.emit_err(session_diagnostics::IncorrectReprFormatGeneric {
120123
span: param.span(),
121124
// FIXME(jdonszelmann) can just be a string in the diag type
122-
repr_arg: &ident.unwrap().to_string(),
125+
repr_arg: name,
123126
cause: IncorrectReprFormatGenericCause::from_lit_kind(
124127
param.span(),
125128
&l.value_as_lit().kind,
126-
ident.unwrap().as_str(),
129+
name,
127130
),
128131
});
129132
None
@@ -133,29 +136,35 @@ fn parse_repr(cx: &AcceptContext<'_>, param: &MetaItemParser<'_>) -> Option<Repr
133136
(Some(sym::C), ArgParser::NoArgs) => Some(ReprC),
134137
(Some(sym::simd), ArgParser::NoArgs) => Some(ReprSimd),
135138
(Some(sym::transparent), ArgParser::NoArgs) => Some(ReprTransparent),
136-
(Some(i @ int_pat!()), ArgParser::NoArgs) => {
139+
(Some(name @ int_pat!()), ArgParser::NoArgs) => {
137140
// int_pat!() should make sure it always parses
138-
Some(ReprInt(int_type_of_word(i).unwrap()))
141+
Some(ReprInt(int_type_of_word(name).unwrap()))
139142
}
140143

141144
(
142-
Some(sym::Rust | sym::C | sym::simd | sym::transparent | int_pat!()),
145+
Some(
146+
name @ sym::Rust
147+
| name @ sym::C
148+
| name @ sym::simd
149+
| name @ sym::transparent
150+
| name @ int_pat!(),
151+
),
143152
ArgParser::NameValue(_),
144153
) => {
145-
cx.emit_err(session_diagnostics::InvalidReprHintNoValue {
146-
span: param.span(),
147-
name: ident.unwrap().to_string(),
148-
});
154+
cx.emit_err(session_diagnostics::InvalidReprHintNoValue { span: param.span(), name });
149155
None
150156
}
151157
(
152-
Some(sym::Rust | sym::C | sym::simd | sym::transparent | int_pat!()),
158+
Some(
159+
name @ sym::Rust
160+
| name @ sym::C
161+
| name @ sym::simd
162+
| name @ sym::transparent
163+
| name @ int_pat!(),
164+
),
153165
ArgParser::List(_),
154166
) => {
155-
cx.emit_err(session_diagnostics::InvalidReprHintNoParen {
156-
span: param.span(),
157-
name: ident.unwrap().to_string(),
158-
});
167+
cx.emit_err(session_diagnostics::InvalidReprHintNoParen { span: param.span(), name });
159168
None
160169
}
161170

compiler/rustc_attr_parsing/src/session_diagnostics.rs

+18-18
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ pub(crate) struct InvalidReprHintNoParen {
204204
#[primary_span]
205205
pub span: Span,
206206

207-
pub name: String,
207+
pub name: Symbol,
208208
}
209209

210210
#[derive(Diagnostic)]
@@ -213,7 +213,7 @@ pub(crate) struct InvalidReprHintNoValue {
213213
#[primary_span]
214214
pub span: Span,
215215

216-
pub name: String,
216+
pub name: Symbol,
217217
}
218218

219219
/// Error code: E0565
@@ -295,58 +295,58 @@ pub(crate) struct IncorrectReprFormatExpectInteger {
295295

296296
#[derive(Diagnostic)]
297297
#[diag(attr_parsing_incorrect_repr_format_generic, code = E0693)]
298-
pub(crate) struct IncorrectReprFormatGeneric<'a> {
298+
pub(crate) struct IncorrectReprFormatGeneric {
299299
#[primary_span]
300300
pub span: Span,
301301

302-
pub repr_arg: &'a str,
302+
pub repr_arg: Symbol,
303303

304304
#[subdiagnostic]
305-
pub cause: Option<IncorrectReprFormatGenericCause<'a>>,
305+
pub cause: Option<IncorrectReprFormatGenericCause>,
306306
}
307307

308308
#[derive(Subdiagnostic)]
309-
pub(crate) enum IncorrectReprFormatGenericCause<'a> {
309+
pub(crate) enum IncorrectReprFormatGenericCause {
310310
#[suggestion(
311311
attr_parsing_suggestion,
312-
code = "{name}({int})",
312+
code = "{name}({value})",
313313
applicability = "machine-applicable"
314314
)]
315315
Int {
316316
#[primary_span]
317317
span: Span,
318318

319319
#[skip_arg]
320-
name: &'a str,
320+
name: Symbol,
321321

322322
#[skip_arg]
323-
int: u128,
323+
value: u128,
324324
},
325325

326326
#[suggestion(
327327
attr_parsing_suggestion,
328-
code = "{name}({symbol})",
328+
code = "{name}({value})",
329329
applicability = "machine-applicable"
330330
)]
331331
Symbol {
332332
#[primary_span]
333333
span: Span,
334334

335335
#[skip_arg]
336-
name: &'a str,
336+
name: Symbol,
337337

338338
#[skip_arg]
339-
symbol: Symbol,
339+
value: Symbol,
340340
},
341341
}
342342

343-
impl<'a> IncorrectReprFormatGenericCause<'a> {
344-
pub(crate) fn from_lit_kind(span: Span, kind: &ast::LitKind, name: &'a str) -> Option<Self> {
345-
match kind {
346-
ast::LitKind::Int(int, ast::LitIntType::Unsuffixed) => {
347-
Some(Self::Int { span, name, int: int.get() })
343+
impl IncorrectReprFormatGenericCause {
344+
pub(crate) fn from_lit_kind(span: Span, kind: &ast::LitKind, name: Symbol) -> Option<Self> {
345+
match *kind {
346+
ast::LitKind::Int(value, ast::LitIntType::Unsuffixed) => {
347+
Some(Self::Int { span, name, value: value.get() })
348348
}
349-
ast::LitKind::Str(symbol, _) => Some(Self::Symbol { span, name, symbol: *symbol }),
349+
ast::LitKind::Str(value, _) => Some(Self::Symbol { span, name, value }),
350350
_ => None,
351351
}
352352
}

0 commit comments

Comments
 (0)