Skip to content

Commit a861c89

Browse files
committed
Auto merge of #117303 - sjwang05:issue-117245, r=estebank
Suggest `=>` --> `>=` in comparisons Fixes #117245
2 parents 15755f3 + 97cf1c8 commit a861c89

File tree

6 files changed

+230
-0
lines changed

6 files changed

+230
-0
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

+20
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,26 @@ impl<'a> Parser<'a> {
598598
// FIXME: translation requires list formatting (for `expect`)
599599
let mut err = self.dcx().struct_span_err(self.token.span, msg_exp);
600600

601+
// Look for usages of '=>' where '>=' was probably intended
602+
if self.token == token::FatArrow
603+
&& expected
604+
.iter()
605+
.any(|tok| matches!(tok, TokenType::Operator | TokenType::Token(TokenKind::Le)))
606+
&& !expected.iter().any(|tok| {
607+
matches!(
608+
tok,
609+
TokenType::Token(TokenKind::FatArrow) | TokenType::Token(TokenKind::Comma)
610+
)
611+
})
612+
{
613+
err.span_suggestion(
614+
self.token.span,
615+
"you might have meant to write a \"greater than or equal to\" comparison",
616+
">=",
617+
Applicability::MaybeIncorrect,
618+
);
619+
}
620+
601621
if let TokenKind::Ident(symbol, _) = &self.prev_token.kind {
602622
if ["def", "fun", "func", "function"].contains(&symbol.as_str()) {
603623
err.span_suggestion_short(

compiler/rustc_parse/src/parser/expr.rs

+10
Original file line numberDiff line numberDiff line change
@@ -2445,6 +2445,7 @@ impl<'a> Parser<'a> {
24452445
}
24462446
} else {
24472447
let attrs = self.parse_outer_attributes()?; // For recovery.
2448+
let maybe_fatarrow = self.token.clone();
24482449
let block = if self.check(&token::OpenDelim(Delimiter::Brace)) {
24492450
self.parse_block()?
24502451
} else {
@@ -2469,6 +2470,15 @@ impl<'a> Parser<'a> {
24692470
"you likely meant to continue parsing the let-chain starting here",
24702471
);
24712472
} else {
2473+
// Look for usages of '=>' where '>=' might be intended
2474+
if maybe_fatarrow.kind == token::FatArrow {
2475+
err.span_suggestion(
2476+
maybe_fatarrow.span,
2477+
"you might have meant to write a \"greater than or equal to\" comparison",
2478+
">=",
2479+
Applicability::MaybeIncorrect,
2480+
);
2481+
}
24722482
err.span_note(
24732483
cond_span,
24742484
"the `if` expression is missing a block after this condition",

tests/ui/missing/missing-block-hint.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ note: the `if` expression is missing a block after this condition
99
|
1010
LL | if (foo) => {}
1111
| ^^^^^
12+
help: you might have meant to write a "greater than or equal to" comparison
13+
|
14+
LL | if (foo) >= {}
15+
| ~~
1216

1317
error: expected `{`, found `bar`
1418
--> $DIR/missing-block-hint.rs:7:13

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

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
error: expected `{`, found `=>`
2+
--> $DIR/eq-gt-to-gt-eq.rs:8:10
3+
|
4+
LL | if a => b {}
5+
| ^^ expected `{`
6+
|
7+
note: the `if` expression is missing a block after this condition
8+
--> $DIR/eq-gt-to-gt-eq.rs:8:8
9+
|
10+
LL | if a => b {}
11+
| ^
12+
help: you might have meant to write a "greater than or equal to" comparison
13+
|
14+
LL | if a >= b {}
15+
| ~~
16+
17+
error: expected `{`, found `=>`
18+
--> $DIR/eq-gt-to-gt-eq.rs:13:10
19+
|
20+
LL | if a => 1 {}
21+
| ^^ expected `{`
22+
|
23+
note: the `if` expression is missing a block after this condition
24+
--> $DIR/eq-gt-to-gt-eq.rs:13:8
25+
|
26+
LL | if a => 1 {}
27+
| ^
28+
help: you might have meant to write a "greater than or equal to" comparison
29+
|
30+
LL | if a >= 1 {}
31+
| ~~
32+
33+
error: expected `{`, found `=>`
34+
--> $DIR/eq-gt-to-gt-eq.rs:18:10
35+
|
36+
LL | if 1 => a {}
37+
| ^^ expected `{`
38+
|
39+
note: the `if` expression is missing a block after this condition
40+
--> $DIR/eq-gt-to-gt-eq.rs:18:8
41+
|
42+
LL | if 1 => a {}
43+
| ^
44+
help: you might have meant to write a "greater than or equal to" comparison
45+
|
46+
LL | if 1 >= a {}
47+
| ~~
48+
49+
error: expected `{`, found `=>`
50+
--> $DIR/eq-gt-to-gt-eq.rs:24:10
51+
|
52+
LL | if a => b && a != b {}
53+
| ^^ expected `{`
54+
|
55+
note: the `if` expression is missing a block after this condition
56+
--> $DIR/eq-gt-to-gt-eq.rs:24:8
57+
|
58+
LL | if a => b && a != b {}
59+
| ^
60+
help: you might have meant to write a "greater than or equal to" comparison
61+
|
62+
LL | if a >= b && a != b {}
63+
| ~~
64+
65+
error: expected `{`, found `=>`
66+
--> $DIR/eq-gt-to-gt-eq.rs:30:20
67+
|
68+
LL | if a != b && a => b {}
69+
| ^^ expected `{`
70+
|
71+
note: the `if` expression is missing a block after this condition
72+
--> $DIR/eq-gt-to-gt-eq.rs:30:8
73+
|
74+
LL | if a != b && a => b {}
75+
| ^^^^^^^^^^^
76+
help: you might have meant to write a "greater than or equal to" comparison
77+
|
78+
LL | if a != b && a >= b {}
79+
| ~~
80+
81+
error: expected one of `!`, `.`, `::`, `;`, `?`, `else`, `{`, or an operator, found `=>`
82+
--> $DIR/eq-gt-to-gt-eq.rs:36:15
83+
|
84+
LL | let _ = a => b;
85+
| ^^ expected one of 8 possible tokens
86+
|
87+
help: you might have meant to write a "greater than or equal to" comparison
88+
|
89+
LL | let _ = a >= b;
90+
| ~~
91+
92+
error: expected one of `!`, `.`, `::`, `?`, `{`, or an operator, found `=>`
93+
--> $DIR/eq-gt-to-gt-eq.rs:42:13
94+
|
95+
LL | match a => b {
96+
| ----- ^^ expected one of `!`, `.`, `::`, `?`, `{`, or an operator
97+
| |
98+
| while parsing this `match` expression
99+
|
100+
help: you might have meant to write a "greater than or equal to" comparison
101+
|
102+
LL | match a >= b {
103+
| ~~
104+
105+
error: aborting due to 7 previous errors
106+

0 commit comments

Comments
 (0)