Skip to content

Commit a8a486c

Browse files
committed
Refactor take_for_recovery call sites.
To make them more concise and similar to each other.
1 parent 62b4e55 commit a8a486c

File tree

2 files changed

+14
-18
lines changed

2 files changed

+14
-18
lines changed

compiler/rustc_parse/src/parser/expr.rs

+13-16
Original file line numberDiff line numberDiff line change
@@ -2688,23 +2688,20 @@ impl<'a> Parser<'a> {
26882688
branch_span: Span,
26892689
attrs: AttrWrapper,
26902690
) {
2691-
if attrs.is_empty() {
2692-
return;
2691+
if !attrs.is_empty()
2692+
&& let [x0 @ xn] | [x0, .., xn] = &*attrs.take_for_recovery(self.sess)
2693+
{
2694+
let attributes = x0.span.to(xn.span);
2695+
let last = xn.span;
2696+
let ctx = if is_ctx_else { "else" } else { "if" };
2697+
self.dcx().emit_err(errors::OuterAttributeNotAllowedOnIfElse {
2698+
last,
2699+
branch_span,
2700+
ctx_span,
2701+
ctx: ctx.to_string(),
2702+
attributes,
2703+
});
26932704
}
2694-
2695-
let attrs: &[ast::Attribute] = &attrs.take_for_recovery(self.sess);
2696-
let (attributes, last) = match attrs {
2697-
[] => return,
2698-
[x0 @ xn] | [x0, .., xn] => (x0.span.to(xn.span), xn.span),
2699-
};
2700-
let ctx = if is_ctx_else { "else" } else { "if" };
2701-
self.dcx().emit_err(errors::OuterAttributeNotAllowedOnIfElse {
2702-
last,
2703-
branch_span,
2704-
ctx_span,
2705-
ctx: ctx.to_string(),
2706-
attributes,
2707-
});
27082705
}
27092706

27102707
fn error_on_extra_if(&mut self, cond: &P<Expr>) -> PResult<'a, ()> {

compiler/rustc_parse/src/parser/stmt.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,7 @@ impl<'a> Parser<'a> {
230230
/// Also error if the previous token was a doc comment.
231231
fn error_outer_attrs(&self, attrs: AttrWrapper) {
232232
if !attrs.is_empty()
233-
&& let attrs = attrs.take_for_recovery(self.sess)
234-
&& let attrs @ [.., last] = &*attrs
233+
&& let attrs @ [.., last] = &*attrs.take_for_recovery(self.sess)
235234
{
236235
if last.is_doc_comment() {
237236
self.dcx().emit_err(errors::DocCommentDoesNotDocumentAnything {

0 commit comments

Comments
 (0)