Detect likely for foo of bar JS syntax

Fix #75311.
This commit is contained in:
Esteban Küber 2020-08-08 20:53:40 -07:00
parent db870ea71b
commit 4dbe0ac928
5 changed files with 25 additions and 7 deletions

View File

@ -1733,13 +1733,20 @@ impl<'a> Parser<'a> {
Ok(self.mk_expr(lo.to(self.prev_token.span), kind, attrs))
}
fn error_missing_in_for_loop(&self) {
let in_span = self.prev_token.span.between(self.token.span);
self.struct_span_err(in_span, "missing `in` in `for` loop")
fn error_missing_in_for_loop(&mut self) {
let (span, msg, sugg) = if self.token.is_ident_named(sym::of) {
// Possibly using JS syntax (#75311).
let span = self.token.span;
self.bump();
(span, "try using `in` here instead", "in")
} else {
(self.prev_token.span.between(self.token.span), "try adding `in` here", " in ")
};
self.struct_span_err(span, "missing `in` in `for` loop")
.span_suggestion_short(
in_span,
"try adding `in` here",
" in ".into(),
span,
msg,
sugg.into(),
// Has been misleading, at least in the past (closed Issue #48492).
Applicability::MaybeIncorrect,
)

View File

@ -736,6 +736,7 @@ symbols! {
not,
note,
object_safe_for_dispatch,
of,
offset,
omit_gdb_pretty_printer_section,
on,

View File

@ -3,4 +3,6 @@
fn main() {
for _i in 0..2 { //~ ERROR missing `in`
}
for _i in 0..2 { //~ ERROR missing `in`
}
}

View File

@ -3,4 +3,6 @@
fn main() {
for _i 0..2 { //~ ERROR missing `in`
}
for _i of 0..2 { //~ ERROR missing `in`
}
}

View File

@ -4,5 +4,11 @@ error: missing `in` in `for` loop
LL | for _i 0..2 {
| ^ help: try adding `in` here
error: aborting due to previous error
error: missing `in` in `for` loop
--> $DIR/issue-40782.rs:6:12
|
LL | for _i of 0..2 {
| ^^ help: try using `in` here instead
error: aborting due to 2 previous errors