Skip to content

Commit cdca5cf

Browse files
committed
parser: extract error_outer_attrs
1 parent 467c86f commit cdca5cf

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/librustc_parse/parser/stmt.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -90,21 +90,9 @@ impl<'a> Parser<'a> {
9090
return Ok(Some(self.mk_stmt(lo.to(item.span), StmtKind::Item(item))));
9191
}
9292

93-
let unused_attrs = |attrs: &[Attribute], s: &mut Self| {
94-
if !attrs.is_empty() {
95-
if s.prev_token_kind == PrevTokenKind::DocComment {
96-
s.span_fatal_err(s.prev_span, Error::UselessDocComment).emit();
97-
} else if attrs.iter().any(|a| a.style == AttrStyle::Outer) {
98-
s.span_err(
99-
s.token.span, "expected statement after outer attribute"
100-
);
101-
}
102-
}
103-
};
104-
10593
// Do not attempt to parse an expression if we're done here.
10694
if self.token == token::Semi {
107-
unused_attrs(&attrs, self);
95+
self.error_outer_attrs(&attrs);
10896
self.bump();
10997
let mut last_semi = lo;
11098
while self.token == token::Semi {
@@ -122,7 +110,7 @@ impl<'a> Parser<'a> {
122110
}
123111

124112
if self.token == token::CloseDelim(token::Brace) {
125-
unused_attrs(&attrs, self);
113+
self.error_outer_attrs(&attrs);
126114
return Ok(None);
127115
}
128116

@@ -190,6 +178,18 @@ impl<'a> Parser<'a> {
190178
Ok(Some(self.mk_stmt(lo.to(hi), kind)))
191179
}
192180

181+
/// Error on outer attributes in this context.
182+
/// Also error if the previous token was a doc comment.
183+
fn error_outer_attrs(&self, attrs: &[Attribute]) {
184+
if !attrs.is_empty() {
185+
if self.prev_token_kind == PrevTokenKind::DocComment {
186+
self.span_fatal_err(self.prev_span, Error::UselessDocComment).emit();
187+
} else if attrs.iter().any(|a| a.style == AttrStyle::Outer) {
188+
self.span_err(self.token.span, "expected statement after outer attribute");
189+
}
190+
}
191+
}
192+
193193
/// Parses a local variable declaration.
194194
fn parse_local(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Local>> {
195195
let lo = self.prev_span;

0 commit comments

Comments
 (0)