Skip to content

Recover for PAT = EXPR {} #145124

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 9, 2025
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
2 changes: 1 addition & 1 deletion compiler/rustc_parse/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ parse_missing_for_in_trait_impl = missing `for` in a trait impl
.suggestion = add `for` here

parse_missing_in_in_for_loop = missing `in` in `for` loop
.use_in_not_of = try using `in` here instead
.use_in = try using `in` here instead
.add_in = try adding `in` here

parse_missing_let_before_mut = missing keyword
Expand Down
11 changes: 5 additions & 6 deletions compiler/rustc_parse/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,14 +585,13 @@ pub(crate) struct MissingInInForLoop {

#[derive(Subdiagnostic)]
pub(crate) enum MissingInInForLoopSub {
// User wrote `for pat of expr {}`
// Has been misleading, at least in the past (closed Issue #48492), thus maybe-incorrect
#[suggestion(
parse_use_in_not_of,
style = "verbose",
applicability = "maybe-incorrect",
code = "in"
)]
#[suggestion(parse_use_in, style = "verbose", applicability = "maybe-incorrect", code = "in")]
InNotOf(#[primary_span] Span),
// User wrote `for pat = expr {}`
#[suggestion(parse_use_in, style = "verbose", applicability = "maybe-incorrect", code = "in")]
InNotEq(#[primary_span] Span),
#[suggestion(parse_add_in, style = "verbose", applicability = "maybe-incorrect", code = " in ")]
AddIn(#[primary_span] Span),
}
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3028,6 +3028,8 @@ impl<'a> Parser<'a> {
let span = self.token.span;
self.bump();
(span, errors::MissingInInForLoopSub::InNotOf)
} else if self.eat(exp!(Eq)) {
(self.prev_token.span, errors::MissingInInForLoopSub::InNotEq)
} else {
(self.prev_token.span.between(self.token.span), errors::MissingInInForLoopSub::AddIn)
};
Expand Down
7 changes: 3 additions & 4 deletions tests/ui/suggestions/for-loop-missing-in.fixed
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//@ run-rustfix

fn main() {
for _i in 0..2 { //~ ERROR missing `in`
}
for _i in 0..2 { //~ ERROR missing `in`
}
for _i in 0..2 {} //~ ERROR missing `in`
for _i in 0..2 {} //~ ERROR missing `in`
for _i in 0..2 {} //~ ERROR missing `in`
}
7 changes: 3 additions & 4 deletions tests/ui/suggestions/for-loop-missing-in.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//@ run-rustfix

fn main() {
for _i 0..2 { //~ ERROR missing `in`
}
for _i of 0..2 { //~ ERROR missing `in`
}
for _i 0..2 {} //~ ERROR missing `in`
for _i of 0..2 {} //~ ERROR missing `in`
for _i = 0..2 {} //~ ERROR missing `in`
}
26 changes: 19 additions & 7 deletions tests/ui/suggestions/for-loop-missing-in.stderr
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
error: missing `in` in `for` loop
--> $DIR/for-loop-missing-in.rs:4:11
|
LL | for _i 0..2 {
LL | for _i 0..2 {}
| ^
|
help: try adding `in` here
|
LL | for _i in 0..2 {
LL | for _i in 0..2 {}
| ++

error: missing `in` in `for` loop
--> $DIR/for-loop-missing-in.rs:6:12
--> $DIR/for-loop-missing-in.rs:5:12
|
LL | for _i of 0..2 {
LL | for _i of 0..2 {}
| ^^
|
help: try using `in` here instead
|
LL - for _i of 0..2 {
LL + for _i in 0..2 {
LL - for _i of 0..2 {}
LL + for _i in 0..2 {}
|

error: missing `in` in `for` loop
--> $DIR/for-loop-missing-in.rs:6:12
|
LL | for _i = 0..2 {}
| ^
|
help: try using `in` here instead
|
LL - for _i = 0..2 {}
LL + for _i in 0..2 {}
|

error: aborting due to 2 previous errors
error: aborting due to 3 previous errors

Loading