Skip to content

Commit d48d214

Browse files
committed
Suggest => --> >= in conditions
1 parent 41aa06e commit d48d214

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2425,6 +2425,7 @@ impl<'a> Parser<'a> {
24252425
}
24262426
} else {
24272427
let attrs = self.parse_outer_attributes()?; // For recovery.
2428+
let maybe_fatarrow = self.token.clone();
24282429
let block = if self.check(&token::OpenDelim(Delimiter::Brace)) {
24292430
self.parse_block()?
24302431
} else {
@@ -2434,10 +2435,20 @@ impl<'a> Parser<'a> {
24342435
self.error_on_extra_if(&cond)?;
24352436
// Parse block, which will always fail, but we can add a nice note to the error
24362437
self.parse_block().map_err(|mut err| {
2437-
err.span_note(
2438-
cond_span,
2439-
"the `if` expression is missing a block after this condition",
2440-
);
2438+
// Look for usages of '=>' where '>=' was probably intended.
2439+
if maybe_fatarrow.kind == token::FatArrow {
2440+
err.span_suggestion(
2441+
maybe_fatarrow.span,
2442+
"you probably meant to write a \"greater than or equal to\" comparison",
2443+
">=",
2444+
Applicability::MachineApplicable,
2445+
);
2446+
} else {
2447+
err.span_note(
2448+
cond_span,
2449+
"the `if` expression is missing a block after this condition",
2450+
);
2451+
}
24412452
err
24422453
})?
24432454
}

tests/ui/parser/eq-gt-to-gt-eq.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Check that we try to correct `=>` to `>=` in conditions.
2+
3+
fn foo() {
4+
let a = 0;
5+
let b = 4;
6+
if a => b { //~ERROR
7+
println!("yay!");
8+
}
9+
}
10+
11+
fn bar() {
12+
let a = 0;
13+
let b = 4;
14+
if a = >b { //~ERROR
15+
println!("yay!");
16+
}
17+
}
18+
19+
fn baz() {
20+
let a = 0;
21+
let b = 4;
22+
if a = > b { //~ERROR
23+
println!("yay!");
24+
}
25+
}
26+
27+
fn main() {}

tests/ui/parser/eq-gt-to-gt-eq.stderr

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error: expected `{`, found `=>`
2+
--> $DIR/eq-gt-to-gt-eq.rs:6:10
3+
|
4+
LL | if a => b {
5+
| ^^ expected `{`
6+
|
7+
help: you probably meant to write a "greater than or equal to" comparison
8+
|
9+
LL | if a >= b {
10+
| ~~
11+
12+
error: expected expression, found `>`
13+
--> $DIR/eq-gt-to-gt-eq.rs:14:12
14+
|
15+
LL | if a = >b {
16+
| ^ expected expression
17+
18+
error: expected expression, found `>`
19+
--> $DIR/eq-gt-to-gt-eq.rs:22:12
20+
|
21+
LL | if a = > b {
22+
| ^ expected expression
23+
24+
error: aborting due to 3 previous errors
25+

0 commit comments

Comments
 (0)