Skip to content

Detect likely for foo of bar JS syntax #75320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/librustc_parse/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
1 change: 1 addition & 0 deletions src/librustc_span/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,7 @@ symbols! {
not,
note,
object_safe_for_dispatch,
of,
offset,
omit_gdb_pretty_printer_section,
on,
Expand Down
2 changes: 2 additions & 0 deletions src/test/ui/issues/issue-40782.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
fn main() {
for _i in 0..2 { //~ ERROR missing `in`
}
for _i in 0..2 { //~ ERROR missing `in`
}
}
2 changes: 2 additions & 0 deletions src/test/ui/issues/issue-40782.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
fn main() {
for _i 0..2 { //~ ERROR missing `in`
}
for _i of 0..2 { //~ ERROR missing `in`
}
}
8 changes: 7 additions & 1 deletion src/test/ui/issues/issue-40782.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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