Skip to content

Commit 725a73c

Browse files
committed
Suggest => --> >= in conditions
1 parent ed086d8 commit 725a73c

File tree

5 files changed

+183
-0
lines changed

5 files changed

+183
-0
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

+12
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,18 @@ impl<'a> Parser<'a> {
610610
// FIXME: translation requires list formatting (for `expect`)
611611
let mut err = self.struct_span_err(self.token.span, msg_exp);
612612

613+
// Look for usages of '=>' where '>=' was probably intended
614+
if self.token == token::FatArrow
615+
&& expected.iter().any(|tok| matches!(tok, TokenType::Token(TokenKind::Le)))
616+
{
617+
err.span_suggestion(
618+
self.token.span,
619+
"you might have meant to write a \"greater than or equal to\" comparison",
620+
">=",
621+
Applicability::Unspecified,
622+
);
623+
}
624+
613625
if let TokenKind::Ident(symbol, _) = &self.prev_token.kind {
614626
if ["def", "fun", "func", "function"].contains(&symbol.as_str()) {
615627
err.span_suggestion_short(

compiler/rustc_parse/src/parser/expr.rs

+10
Original file line numberDiff line numberDiff line change
@@ -2432,6 +2432,7 @@ impl<'a> Parser<'a> {
24322432
}
24332433
} else {
24342434
let attrs = self.parse_outer_attributes()?; // For recovery.
2435+
let maybe_fatarrow = self.token.clone();
24352436
let block = if self.check(&token::OpenDelim(Delimiter::Brace)) {
24362437
self.parse_block()?
24372438
} else {
@@ -2455,6 +2456,15 @@ impl<'a> Parser<'a> {
24552456
self.token.span.to(maybe_let.span),
24562457
"you likely meant to continue parsing the let-chain starting here",
24572458
);
2459+
}
2460+
// Look for usages of '=>' where '>=' was probably intended
2461+
if maybe_fatarrow.kind == token::FatArrow {
2462+
err.span_suggestion(
2463+
maybe_fatarrow.span,
2464+
"you probably meant to write a \"greater than or equal to\" comparison",
2465+
">=",
2466+
Applicability::MachineApplicable,
2467+
);
24582468
} else {
24592469
err.span_note(
24602470
cond_span,

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

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// run-rustfix
2+
// Check that we try to correct `=>` to `>=` in conditions.
3+
#![allow(unused)]
4+
5+
fn main() {
6+
let a = 0;
7+
let b = 1;
8+
if a >= b {} //~ERROR
9+
}
10+
11+
fn foo() {
12+
let a = 0;
13+
if a >= 1 {} //~ERROR
14+
}
15+
16+
fn a() {
17+
let a = 0;
18+
if 1 >= a {} //~ERROR
19+
}
20+
21+
fn bar() {
22+
let a = 0;
23+
let b = 1;
24+
if a >= b && a != b {} //~ERROR
25+
}
26+
27+
fn qux() {
28+
let a = 0;
29+
let b = 1;
30+
if a != b && a >= b {} //~ERROR
31+
}
32+
33+
fn baz() {
34+
let a = 0;
35+
let b = 1;
36+
let _ = a => b; //~ERROR
37+
}
38+
39+
fn b() {
40+
let a = 0;
41+
let b = 1;
42+
match a => b { //~ERROR
43+
_ => todo!(),
44+
}
45+
}

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

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// run-rustfix
2+
// Check that we try to correct `=>` to `>=` in conditions.
3+
#![allow(unused)]
4+
5+
fn main() {
6+
let a = 0;
7+
let b = 1;
8+
if a => b {} //~ERROR
9+
}
10+
11+
fn foo() {
12+
let a = 0;
13+
if a => 1 {} //~ERROR
14+
}
15+
16+
fn a() {
17+
let a = 0;
18+
if 1 => a {} //~ERROR
19+
}
20+
21+
fn bar() {
22+
let a = 0;
23+
let b = 1;
24+
if a => b && a != b {} //~ERROR
25+
}
26+
27+
fn qux() {
28+
let a = 0;
29+
let b = 1;
30+
if a != b && a => b {} //~ERROR
31+
}
32+
33+
fn baz() {
34+
let a = 0;
35+
let b = 1;
36+
let _ = a => b; //~ERROR
37+
}
38+
39+
fn b() {
40+
let a = 0;
41+
let b = 1;
42+
match a => b { //~ERROR
43+
_ => todo!(),
44+
}
45+
}

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

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
error: expected `{`, found `=>`
2+
--> $DIR/eq-gt-to-gt-eq.rs:8: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 `{`, found `=>`
13+
--> $DIR/eq-gt-to-gt-eq.rs:13:10
14+
|
15+
LL | if a => 1 {}
16+
| ^^ expected `{`
17+
|
18+
help: you probably meant to write a "greater than or equal to" comparison
19+
|
20+
LL | if a >= 1 {}
21+
| ~~
22+
23+
error: expected `{`, found `=>`
24+
--> $DIR/eq-gt-to-gt-eq.rs:18:10
25+
|
26+
LL | if 1 => a {}
27+
| ^^ expected `{`
28+
|
29+
help: you probably meant to write a "greater than or equal to" comparison
30+
|
31+
LL | if 1 >= a {}
32+
| ~~
33+
34+
error: expected `{`, found `=>`
35+
--> $DIR/eq-gt-to-gt-eq.rs:24:10
36+
|
37+
LL | if a => b && a != b {}
38+
| ^^ expected `{`
39+
|
40+
help: you probably meant to write a "greater than or equal to" comparison
41+
|
42+
LL | if a >= b && a != b {}
43+
| ~~
44+
45+
error: expected `{`, found `=>`
46+
--> $DIR/eq-gt-to-gt-eq.rs:30:20
47+
|
48+
LL | if a != b && a => b {}
49+
| ^^ expected `{`
50+
|
51+
help: you probably meant to write a "greater than or equal to" comparison
52+
|
53+
LL | if a != b && a >= b {}
54+
| ~~
55+
56+
error: expected one of `!`, `.`, `::`, `;`, `?`, `else`, `{`, or an operator, found `=>`
57+
--> $DIR/eq-gt-to-gt-eq.rs:36:15
58+
|
59+
LL | let _ = a => b;
60+
| ^^ expected one of 8 possible tokens
61+
62+
error: expected one of `!`, `.`, `::`, `?`, `{`, or an operator, found `=>`
63+
--> $DIR/eq-gt-to-gt-eq.rs:42:13
64+
|
65+
LL | match a => b {
66+
| ----- ^^ expected one of `!`, `.`, `::`, `?`, `{`, or an operator
67+
| |
68+
| while parsing this `match` expression
69+
70+
error: aborting due to 7 previous errors
71+

0 commit comments

Comments
 (0)