Skip to content

Commit 1b11860

Browse files
committed
Use chaining for diagnosics in parser.
1 parent dfad725 commit 1b11860

File tree

2 files changed

+155
-152
lines changed

2 files changed

+155
-152
lines changed

src/libsyntax/parse/diagnostics.rs

+33-33
Original file line numberDiff line numberDiff line change
@@ -549,8 +549,10 @@ impl<'a> Parser<'a> {
549549
ExprKind::Binary(op, _, _) if op.node.is_comparison() => {
550550
// respan to include both operators
551551
let op_span = op.span.to(self.token.span);
552-
let mut err = self.diagnostic().struct_span_err(op_span,
553-
"chained comparison operators require parentheses");
552+
let mut err = self.struct_span_err(
553+
op_span,
554+
"chained comparison operators require parentheses",
555+
);
554556
if op.node == BinOpKind::Lt &&
555557
*outer_op == AssocOp::Less || // Include `<` to provide this recommendation
556558
*outer_op == AssocOp::Greater // even in a case like the following:
@@ -1149,17 +1151,14 @@ impl<'a> Parser<'a> {
11491151
crate fn check_for_for_in_in_typo(&mut self, in_span: Span) {
11501152
if self.eat_keyword(kw::In) {
11511153
// a common typo: `for _ in in bar {}`
1152-
let mut err = self.sess.span_diagnostic.struct_span_err(
1153-
self.prev_span,
1154-
"expected iterable, found keyword `in`",
1155-
);
1156-
err.span_suggestion_short(
1157-
in_span.until(self.prev_span),
1158-
"remove the duplicated `in`",
1159-
String::new(),
1160-
Applicability::MachineApplicable,
1161-
);
1162-
err.emit();
1154+
self.struct_span_err(self.prev_span, "expected iterable, found keyword `in`")
1155+
.span_suggestion_short(
1156+
in_span.until(self.prev_span),
1157+
"remove the duplicated `in`",
1158+
String::new(),
1159+
Applicability::MachineApplicable,
1160+
)
1161+
.emit();
11631162
}
11641163
}
11651164

@@ -1172,12 +1171,12 @@ impl<'a> Parser<'a> {
11721171

11731172
crate fn eat_incorrect_doc_comment_for_arg_type(&mut self) {
11741173
if let token::DocComment(_) = self.token.kind {
1175-
let mut err = self.diagnostic().struct_span_err(
1174+
self.struct_span_err(
11761175
self.token.span,
11771176
"documentation comments cannot be applied to a function parameter's type",
1178-
);
1179-
err.span_label(self.token.span, "doc comments are not allowed here");
1180-
err.emit();
1177+
)
1178+
.span_label(self.token.span, "doc comments are not allowed here")
1179+
.emit();
11811180
self.bump();
11821181
} else if self.token == token::Pound && self.look_ahead(1, |t| {
11831182
*t == token::OpenDelim(token::Bracket)
@@ -1189,12 +1188,12 @@ impl<'a> Parser<'a> {
11891188
}
11901189
let sp = lo.to(self.token.span);
11911190
self.bump();
1192-
let mut err = self.diagnostic().struct_span_err(
1191+
self.struct_span_err(
11931192
sp,
11941193
"attributes cannot be applied to a function parameter's type",
1195-
);
1196-
err.span_label(sp, "attributes are not allowed here");
1197-
err.emit();
1194+
)
1195+
.span_label(sp, "attributes are not allowed here")
1196+
.emit();
11981197
}
11991198
}
12001199

@@ -1250,18 +1249,19 @@ impl<'a> Parser<'a> {
12501249
self.expect(&token::Colon)?;
12511250
let ty = self.parse_ty()?;
12521251

1253-
let mut err = self.diagnostic().struct_span_err_with_code(
1254-
pat.span,
1255-
"patterns aren't allowed in methods without bodies",
1256-
DiagnosticId::Error("E0642".into()),
1257-
);
1258-
err.span_suggestion_short(
1259-
pat.span,
1260-
"give this argument a name or use an underscore to ignore it",
1261-
"_".to_owned(),
1262-
Applicability::MachineApplicable,
1263-
);
1264-
err.emit();
1252+
self.diagnostic()
1253+
.struct_span_err_with_code(
1254+
pat.span,
1255+
"patterns aren't allowed in methods without bodies",
1256+
DiagnosticId::Error("E0642".into()),
1257+
)
1258+
.span_suggestion_short(
1259+
pat.span,
1260+
"give this argument a name or use an underscore to ignore it",
1261+
"_".to_owned(),
1262+
Applicability::MachineApplicable,
1263+
)
1264+
.emit();
12651265

12661266
// Pretend the pattern is `_`, to avoid duplicate errors from AST validation.
12671267
let pat = P(Pat {

0 commit comments

Comments
 (0)