Skip to content

Commit af2b232

Browse files
committed
Refactor take_for_recovery call sites.
To make them more concise and similar to each other.
1 parent 882d93d commit af2b232

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
@@ -2683,23 +2683,20 @@ impl<'a> Parser<'a> {
26832683
branch_span: Span,
26842684
attrs: AttrWrapper,
26852685
) {
2686-
if attrs.is_empty() {
2687-
return;
2686+
if !attrs.is_empty()
2687+
&& let [x0 @ xn] | [x0, .., xn] = &*attrs.take_for_recovery(self.sess)
2688+
{
2689+
let attributes = x0.span.to(xn.span);
2690+
let last = xn.span;
2691+
let ctx = if is_ctx_else { "else" } else { "if" };
2692+
self.dcx().emit_err(errors::OuterAttributeNotAllowedOnIfElse {
2693+
last,
2694+
branch_span,
2695+
ctx_span,
2696+
ctx: ctx.to_string(),
2697+
attributes,
2698+
});
26882699
}
2689-
2690-
let attrs: &[ast::Attribute] = &attrs.take_for_recovery(self.sess);
2691-
let (attributes, last) = match attrs {
2692-
[] => return,
2693-
[x0 @ xn] | [x0, .., xn] => (x0.span.to(xn.span), xn.span),
2694-
};
2695-
let ctx = if is_ctx_else { "else" } else { "if" };
2696-
self.dcx().emit_err(errors::OuterAttributeNotAllowedOnIfElse {
2697-
last,
2698-
branch_span,
2699-
ctx_span,
2700-
ctx: ctx.to_string(),
2701-
attributes,
2702-
});
27032700
}
27042701

27052702
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
@@ -229,8 +229,7 @@ impl<'a> Parser<'a> {
229229
/// Also error if the previous token was a doc comment.
230230
fn error_outer_attrs(&self, attrs: AttrWrapper) {
231231
if !attrs.is_empty()
232-
&& let attrs = attrs.take_for_recovery(self.sess)
233-
&& let attrs @ [.., last] = &*attrs
232+
&& let attrs @ [.., last] = &*attrs.take_for_recovery(self.sess)
234233
{
235234
if last.is_doc_comment() {
236235
self.dcx().emit_err(errors::DocCommentDoesNotDocumentAnything {

0 commit comments

Comments
 (0)