Skip to content

Commit e49a147

Browse files
committed
Factor out insert_or_error.
It appears identically as a closure in two functions. Also change its return type from `bool` to `Option<()>` so it can be used with `?`.
1 parent 108e541 commit e49a147

File tree

1 file changed

+23
-62
lines changed

1 file changed

+23
-62
lines changed

compiler/rustc_attr/src/builtin.rs

Lines changed: 23 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -353,28 +353,28 @@ pub fn find_body_stability(
353353
body_stab
354354
}
355355

356+
fn insert_or_error(sess: &Session, meta: &MetaItem, item: &mut Option<Symbol>) -> Option<()> {
357+
if item.is_some() {
358+
handle_errors(
359+
&sess.parse_sess,
360+
meta.span,
361+
AttrError::MultipleItem(pprust::path_to_string(&meta.path)),
362+
);
363+
None
364+
} else if let Some(v) = meta.value_str() {
365+
*item = Some(v);
366+
Some(())
367+
} else {
368+
sess.emit_err(session_diagnostics::IncorrectMetaItem { span: meta.span });
369+
None
370+
}
371+
}
372+
356373
/// Read the content of a `stable`/`rustc_const_stable` attribute, and return the feature name and
357374
/// its stability information.
358375
fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, StabilityLevel)> {
359376
let meta = attr.meta()?;
360377
let MetaItem { kind: MetaItemKind::List(ref metas), .. } = meta else { return None };
361-
let insert_or_error = |meta: &MetaItem, item: &mut Option<Symbol>| {
362-
if item.is_some() {
363-
handle_errors(
364-
&sess.parse_sess,
365-
meta.span,
366-
AttrError::MultipleItem(pprust::path_to_string(&meta.path)),
367-
);
368-
return false;
369-
}
370-
if let Some(v) = meta.value_str() {
371-
*item = Some(v);
372-
true
373-
} else {
374-
sess.emit_err(session_diagnostics::IncorrectMetaItem { span: meta.span });
375-
false
376-
}
377-
};
378378

379379
let mut feature = None;
380380
let mut since = None;
@@ -389,16 +389,8 @@ fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabilit
389389
};
390390

391391
match mi.name_or_empty() {
392-
sym::feature => {
393-
if !insert_or_error(mi, &mut feature) {
394-
return None;
395-
}
396-
}
397-
sym::since => {
398-
if !insert_or_error(mi, &mut since) {
399-
return None;
400-
}
401-
}
392+
sym::feature => insert_or_error(sess, mi, &mut feature)?,
393+
sym::since => insert_or_error(sess, mi, &mut since)?,
402394
_ => {
403395
handle_errors(
404396
&sess.parse_sess,
@@ -438,23 +430,6 @@ fn parse_stability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabilit
438430
fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, StabilityLevel)> {
439431
let meta = attr.meta()?;
440432
let MetaItem { kind: MetaItemKind::List(ref metas), .. } = meta else { return None };
441-
let insert_or_error = |meta: &MetaItem, item: &mut Option<Symbol>| {
442-
if item.is_some() {
443-
handle_errors(
444-
&sess.parse_sess,
445-
meta.span,
446-
AttrError::MultipleItem(pprust::path_to_string(&meta.path)),
447-
);
448-
return false;
449-
}
450-
if let Some(v) = meta.value_str() {
451-
*item = Some(v);
452-
true
453-
} else {
454-
sess.emit_err(session_diagnostics::IncorrectMetaItem { span: meta.span });
455-
false
456-
}
457-
};
458433

459434
let mut feature = None;
460435
let mut reason = None;
@@ -473,20 +448,10 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil
473448
};
474449

475450
match mi.name_or_empty() {
476-
sym::feature => {
477-
if !insert_or_error(mi, &mut feature) {
478-
return None;
479-
}
480-
}
481-
sym::reason => {
482-
if !insert_or_error(mi, &mut reason) {
483-
return None;
484-
}
485-
}
451+
sym::feature => insert_or_error(sess, mi, &mut feature)?,
452+
sym::reason => insert_or_error(sess, mi, &mut reason)?,
486453
sym::issue => {
487-
if !insert_or_error(mi, &mut issue) {
488-
return None;
489-
}
454+
insert_or_error(sess, mi, &mut issue)?;
490455

491456
// These unwraps are safe because `insert_or_error` ensures the meta item
492457
// is a name/value pair string literal.
@@ -515,11 +480,7 @@ fn parse_unstability(sess: &Session, attr: &Attribute) -> Option<(Symbol, Stabil
515480
}
516481
is_soft = true;
517482
}
518-
sym::implied_by => {
519-
if !insert_or_error(mi, &mut implied_by) {
520-
return None;
521-
}
522-
}
483+
sym::implied_by => insert_or_error(sess, mi, &mut implied_by)?,
523484
_ => {
524485
handle_errors(
525486
&sess.parse_sess,

0 commit comments

Comments
 (0)