Skip to content

Commit

Permalink
Refactor take_for_recovery call sites.
Browse files Browse the repository at this point in the history
To make them more concise and similar to each other.
  • Loading branch information
nnethercote committed Feb 20, 2024
1 parent 9e154ea commit a85e5d1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
29 changes: 13 additions & 16 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2681,23 +2681,20 @@ impl<'a> Parser<'a> {
branch_span: Span,
attrs: AttrWrapper,
) {
if attrs.is_empty() {
return;
if !attrs.is_empty()
&& let [x0 @ xn] | [x0, .., xn] = &*attrs.take_for_recovery(self.sess)
{
let attributes = x0.span.to(xn.span);
let last = xn.span;
let ctx = if is_ctx_else { "else" } else { "if" };
self.dcx().emit_err(errors::OuterAttributeNotAllowedOnIfElse {
last,
branch_span,
ctx_span,
ctx: ctx.to_string(),
attributes,
});
}

let attrs: &[ast::Attribute] = &attrs.take_for_recovery(self.sess);
let (attributes, last) = match attrs {
[] => return,
[x0 @ xn] | [x0, .., xn] => (x0.span.to(xn.span), xn.span),
};
let ctx = if is_ctx_else { "else" } else { "if" };
self.dcx().emit_err(errors::OuterAttributeNotAllowedOnIfElse {
last,
branch_span,
ctx_span,
ctx: ctx.to_string(),
attributes,
});
}

fn error_on_extra_if(&mut self, cond: &P<Expr>) -> PResult<'a, ()> {
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_parse/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,7 @@ impl<'a> Parser<'a> {
/// Also error if the previous token was a doc comment.
fn error_outer_attrs(&self, attrs: AttrWrapper) {
if !attrs.is_empty()
&& let attrs = attrs.take_for_recovery(self.sess)
&& let attrs @ [.., last] = &*attrs
&& let attrs @ [.., last] = &*attrs.take_for_recovery(self.sess)
{
if last.is_doc_comment() {
self.dcx().emit_err(errors::DocCommentDoesNotDocumentAnything {
Expand Down

0 comments on commit a85e5d1

Please sign in to comment.