Skip to content

Commit d7ecac8

Browse files
committed
Refactor take_for_recovery call sites.
To make them more concise and similar to each other.
1 parent 83ba068 commit d7ecac8

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

27032700
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)