Skip to content

Commit 1b53936

Browse files
committed
Suggest more uses of >= over =>
1 parent 13b668b commit 1b53936

File tree

4 files changed

+99
-28
lines changed

4 files changed

+99
-28
lines changed

compiler/rustc_parse/src/parser/diagnostics.rs

+10
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,16 @@ 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+
err.span_suggestion(
616+
self.token.span,
617+
"you probably meant to write a \"greater than or equal to\" comparison",
618+
">=",
619+
Applicability::Unspecified,
620+
);
621+
}
622+
613623
if let TokenKind::Ident(symbol, _) = &self.prev_token.kind {
614624
if ["def", "fun", "func", "function"].contains(&symbol.as_str()) {
615625
err.span_suggestion_short(

compiler/rustc_parse/src/parser/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2435,7 +2435,7 @@ impl<'a> Parser<'a> {
24352435
self.error_on_extra_if(&cond)?;
24362436
// Parse block, which will always fail, but we can add a nice note to the error
24372437
self.parse_block().map_err(|mut err| {
2438-
// Look for usages of '=>' where '>=' was probably intended.
2438+
// Look for usages of '=>' where '>=' was probably intended
24392439
if maybe_fatarrow.kind == token::FatArrow {
24402440
err.span_suggestion(
24412441
maybe_fatarrow.span,

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

+21-16
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,42 @@
22

33
fn main() {
44
let a = 0;
5-
let b = 4;
6-
if a => b { //~ERROR
7-
println!("yay!");
8-
}
5+
let b = 1;
6+
if a => b {} //~ERROR
97
}
108

119
fn foo() {
1210
let a = 0;
13-
if a => 4 { //~ERROR
14-
println!("yay!");
15-
}
11+
if a => 1 {} //~ERROR
12+
}
13+
14+
fn a() {
15+
let a = 0;
16+
if 1 => a {} //~ERROR
1617
}
1718

1819
fn bar() {
1920
let a = 0;
20-
let b = 4;
21-
if a => b && true { //~ERROR
22-
println!("yay!");
23-
}
21+
let b = 1;
22+
if a => b && a != b {} //~ERROR
2423
}
2524

2625
fn qux() {
2726
let a = 0;
28-
let b = 4;
29-
if true && a => b { //~ERROR
30-
println!("yay!");
31-
}
27+
let b = 1;
28+
if a != b && a => b {} //~ERROR
3229
}
3330

3431
fn baz() {
3532
let a = 0;
36-
let b = 4;
33+
let b = 1;
3734
let _ = a => b; //~ERROR
3835
}
36+
37+
fn b() {
38+
let a = 0;
39+
let b = 1;
40+
match a => b { //~ERROR
41+
_ => todo!(),
42+
}
43+
}

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

+67-11
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,81 @@
11
error: expected `{`, found `=>`
22
--> $DIR/eq-gt-to-gt-eq.rs:6:10
33
|
4-
LL | if a => b {
4+
LL | if a => b {}
55
| ^^ expected `{`
66
|
77
help: you probably meant to write a "greater than or equal to" comparison
88
|
9-
LL | if a >= b {
9+
LL | if a >= b {}
1010
| ~~
1111

12-
error: expected expression, found `>`
13-
--> $DIR/eq-gt-to-gt-eq.rs:14:12
12+
error: expected `{`, found `=>`
13+
--> $DIR/eq-gt-to-gt-eq.rs:11: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:16: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:22: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:28: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
1452
|
15-
LL | if a = >b {
16-
| ^ expected expression
53+
LL | if a != b && a >= b {}
54+
| ~~
1755

18-
error: expected expression, found `>`
19-
--> $DIR/eq-gt-to-gt-eq.rs:22:12
56+
error: expected one of `!`, `.`, `::`, `;`, `?`, `else`, `{`, or an operator, found `=>`
57+
--> $DIR/eq-gt-to-gt-eq.rs:34:15
58+
|
59+
LL | let _ = a => b;
60+
| ^^ expected one of 8 possible tokens
61+
|
62+
help: you probably meant to write a "greater than or equal to" comparison
63+
|
64+
LL | let _ = a >= b;
65+
| ~~
66+
67+
error: expected one of `!`, `.`, `::`, `?`, `{`, or an operator, found `=>`
68+
--> $DIR/eq-gt-to-gt-eq.rs:40:13
69+
|
70+
LL | match a => b {
71+
| ----- ^^ expected one of `!`, `.`, `::`, `?`, `{`, or an operator
72+
| |
73+
| while parsing this `match` expression
74+
|
75+
help: you probably meant to write a "greater than or equal to" comparison
2076
|
21-
LL | if a = > b {
22-
| ^ expected expression
77+
LL | match a >= b {
78+
| ~~
2379

24-
error: aborting due to 3 previous errors
80+
error: aborting due to 7 previous errors
2581

0 commit comments

Comments
 (0)