mirror of
https://github.com/rust-lang/rust.git
synced 2024-10-30 14:01:51 +00:00
Auto merge of #117303 - sjwang05:issue-117245, r=estebank
Suggest `=>` --> `>=` in comparisons Fixes #117245
This commit is contained in:
commit
a861c8965e
@ -598,6 +598,26 @@ impl<'a> Parser<'a> {
|
|||||||
// FIXME: translation requires list formatting (for `expect`)
|
// FIXME: translation requires list formatting (for `expect`)
|
||||||
let mut err = self.dcx().struct_span_err(self.token.span, msg_exp);
|
let mut err = self.dcx().struct_span_err(self.token.span, msg_exp);
|
||||||
|
|
||||||
|
// Look for usages of '=>' where '>=' was probably intended
|
||||||
|
if self.token == token::FatArrow
|
||||||
|
&& expected
|
||||||
|
.iter()
|
||||||
|
.any(|tok| matches!(tok, TokenType::Operator | TokenType::Token(TokenKind::Le)))
|
||||||
|
&& !expected.iter().any(|tok| {
|
||||||
|
matches!(
|
||||||
|
tok,
|
||||||
|
TokenType::Token(TokenKind::FatArrow) | TokenType::Token(TokenKind::Comma)
|
||||||
|
)
|
||||||
|
})
|
||||||
|
{
|
||||||
|
err.span_suggestion(
|
||||||
|
self.token.span,
|
||||||
|
"you might have meant to write a \"greater than or equal to\" comparison",
|
||||||
|
">=",
|
||||||
|
Applicability::MaybeIncorrect,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if let TokenKind::Ident(symbol, _) = &self.prev_token.kind {
|
if let TokenKind::Ident(symbol, _) = &self.prev_token.kind {
|
||||||
if ["def", "fun", "func", "function"].contains(&symbol.as_str()) {
|
if ["def", "fun", "func", "function"].contains(&symbol.as_str()) {
|
||||||
err.span_suggestion_short(
|
err.span_suggestion_short(
|
||||||
|
@ -2445,6 +2445,7 @@ impl<'a> Parser<'a> {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let attrs = self.parse_outer_attributes()?; // For recovery.
|
let attrs = self.parse_outer_attributes()?; // For recovery.
|
||||||
|
let maybe_fatarrow = self.token.clone();
|
||||||
let block = if self.check(&token::OpenDelim(Delimiter::Brace)) {
|
let block = if self.check(&token::OpenDelim(Delimiter::Brace)) {
|
||||||
self.parse_block()?
|
self.parse_block()?
|
||||||
} else {
|
} else {
|
||||||
@ -2469,6 +2470,15 @@ impl<'a> Parser<'a> {
|
|||||||
"you likely meant to continue parsing the let-chain starting here",
|
"you likely meant to continue parsing the let-chain starting here",
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
// Look for usages of '=>' where '>=' might be intended
|
||||||
|
if maybe_fatarrow.kind == token::FatArrow {
|
||||||
|
err.span_suggestion(
|
||||||
|
maybe_fatarrow.span,
|
||||||
|
"you might have meant to write a \"greater than or equal to\" comparison",
|
||||||
|
">=",
|
||||||
|
Applicability::MaybeIncorrect,
|
||||||
|
);
|
||||||
|
}
|
||||||
err.span_note(
|
err.span_note(
|
||||||
cond_span,
|
cond_span,
|
||||||
"the `if` expression is missing a block after this condition",
|
"the `if` expression is missing a block after this condition",
|
||||||
|
@ -9,6 +9,10 @@ note: the `if` expression is missing a block after this condition
|
|||||||
|
|
|
|
||||||
LL | if (foo) => {}
|
LL | if (foo) => {}
|
||||||
| ^^^^^
|
| ^^^^^
|
||||||
|
help: you might have meant to write a "greater than or equal to" comparison
|
||||||
|
|
|
||||||
|
LL | if (foo) >= {}
|
||||||
|
| ~~
|
||||||
|
|
||||||
error: expected `{`, found `bar`
|
error: expected `{`, found `bar`
|
||||||
--> $DIR/missing-block-hint.rs:7:13
|
--> $DIR/missing-block-hint.rs:7:13
|
||||||
|
45
tests/ui/parser/eq-gt-to-gt-eq.fixed
Normal file
45
tests/ui/parser/eq-gt-to-gt-eq.fixed
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
// run-rustfix
|
||||||
|
// Check that we try to correct `=>` to `>=` in conditions.
|
||||||
|
#![allow(unused)]
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let a = 0;
|
||||||
|
let b = 1;
|
||||||
|
if a >= b {} //~ERROR
|
||||||
|
}
|
||||||
|
|
||||||
|
fn foo() {
|
||||||
|
let a = 0;
|
||||||
|
if a >= 1 {} //~ERROR
|
||||||
|
}
|
||||||
|
|
||||||
|
fn a() {
|
||||||
|
let a = 0;
|
||||||
|
if 1 >= a {} //~ERROR
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bar() {
|
||||||
|
let a = 0;
|
||||||
|
let b = 1;
|
||||||
|
if a >= b && a != b {} //~ERROR
|
||||||
|
}
|
||||||
|
|
||||||
|
fn qux() {
|
||||||
|
let a = 0;
|
||||||
|
let b = 1;
|
||||||
|
if a != b && a >= b {} //~ERROR
|
||||||
|
}
|
||||||
|
|
||||||
|
fn baz() {
|
||||||
|
let a = 0;
|
||||||
|
let b = 1;
|
||||||
|
let _ = a >= b; //~ERROR
|
||||||
|
}
|
||||||
|
|
||||||
|
fn b() {
|
||||||
|
let a = 0;
|
||||||
|
let b = 1;
|
||||||
|
match a >= b { //~ERROR
|
||||||
|
_ => todo!(),
|
||||||
|
}
|
||||||
|
}
|
45
tests/ui/parser/eq-gt-to-gt-eq.rs
Normal file
45
tests/ui/parser/eq-gt-to-gt-eq.rs
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
// run-rustfix
|
||||||
|
// Check that we try to correct `=>` to `>=` in conditions.
|
||||||
|
#![allow(unused)]
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let a = 0;
|
||||||
|
let b = 1;
|
||||||
|
if a => b {} //~ERROR
|
||||||
|
}
|
||||||
|
|
||||||
|
fn foo() {
|
||||||
|
let a = 0;
|
||||||
|
if a => 1 {} //~ERROR
|
||||||
|
}
|
||||||
|
|
||||||
|
fn a() {
|
||||||
|
let a = 0;
|
||||||
|
if 1 => a {} //~ERROR
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bar() {
|
||||||
|
let a = 0;
|
||||||
|
let b = 1;
|
||||||
|
if a => b && a != b {} //~ERROR
|
||||||
|
}
|
||||||
|
|
||||||
|
fn qux() {
|
||||||
|
let a = 0;
|
||||||
|
let b = 1;
|
||||||
|
if a != b && a => b {} //~ERROR
|
||||||
|
}
|
||||||
|
|
||||||
|
fn baz() {
|
||||||
|
let a = 0;
|
||||||
|
let b = 1;
|
||||||
|
let _ = a => b; //~ERROR
|
||||||
|
}
|
||||||
|
|
||||||
|
fn b() {
|
||||||
|
let a = 0;
|
||||||
|
let b = 1;
|
||||||
|
match a => b { //~ERROR
|
||||||
|
_ => todo!(),
|
||||||
|
}
|
||||||
|
}
|
106
tests/ui/parser/eq-gt-to-gt-eq.stderr
Normal file
106
tests/ui/parser/eq-gt-to-gt-eq.stderr
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
error: expected `{`, found `=>`
|
||||||
|
--> $DIR/eq-gt-to-gt-eq.rs:8:10
|
||||||
|
|
|
||||||
|
LL | if a => b {}
|
||||||
|
| ^^ expected `{`
|
||||||
|
|
|
||||||
|
note: the `if` expression is missing a block after this condition
|
||||||
|
--> $DIR/eq-gt-to-gt-eq.rs:8:8
|
||||||
|
|
|
||||||
|
LL | if a => b {}
|
||||||
|
| ^
|
||||||
|
help: you might have meant to write a "greater than or equal to" comparison
|
||||||
|
|
|
||||||
|
LL | if a >= b {}
|
||||||
|
| ~~
|
||||||
|
|
||||||
|
error: expected `{`, found `=>`
|
||||||
|
--> $DIR/eq-gt-to-gt-eq.rs:13:10
|
||||||
|
|
|
||||||
|
LL | if a => 1 {}
|
||||||
|
| ^^ expected `{`
|
||||||
|
|
|
||||||
|
note: the `if` expression is missing a block after this condition
|
||||||
|
--> $DIR/eq-gt-to-gt-eq.rs:13:8
|
||||||
|
|
|
||||||
|
LL | if a => 1 {}
|
||||||
|
| ^
|
||||||
|
help: you might have meant to write a "greater than or equal to" comparison
|
||||||
|
|
|
||||||
|
LL | if a >= 1 {}
|
||||||
|
| ~~
|
||||||
|
|
||||||
|
error: expected `{`, found `=>`
|
||||||
|
--> $DIR/eq-gt-to-gt-eq.rs:18:10
|
||||||
|
|
|
||||||
|
LL | if 1 => a {}
|
||||||
|
| ^^ expected `{`
|
||||||
|
|
|
||||||
|
note: the `if` expression is missing a block after this condition
|
||||||
|
--> $DIR/eq-gt-to-gt-eq.rs:18:8
|
||||||
|
|
|
||||||
|
LL | if 1 => a {}
|
||||||
|
| ^
|
||||||
|
help: you might have meant to write a "greater than or equal to" comparison
|
||||||
|
|
|
||||||
|
LL | if 1 >= a {}
|
||||||
|
| ~~
|
||||||
|
|
||||||
|
error: expected `{`, found `=>`
|
||||||
|
--> $DIR/eq-gt-to-gt-eq.rs:24:10
|
||||||
|
|
|
||||||
|
LL | if a => b && a != b {}
|
||||||
|
| ^^ expected `{`
|
||||||
|
|
|
||||||
|
note: the `if` expression is missing a block after this condition
|
||||||
|
--> $DIR/eq-gt-to-gt-eq.rs:24:8
|
||||||
|
|
|
||||||
|
LL | if a => b && a != b {}
|
||||||
|
| ^
|
||||||
|
help: you might have meant to write a "greater than or equal to" comparison
|
||||||
|
|
|
||||||
|
LL | if a >= b && a != b {}
|
||||||
|
| ~~
|
||||||
|
|
||||||
|
error: expected `{`, found `=>`
|
||||||
|
--> $DIR/eq-gt-to-gt-eq.rs:30:20
|
||||||
|
|
|
||||||
|
LL | if a != b && a => b {}
|
||||||
|
| ^^ expected `{`
|
||||||
|
|
|
||||||
|
note: the `if` expression is missing a block after this condition
|
||||||
|
--> $DIR/eq-gt-to-gt-eq.rs:30:8
|
||||||
|
|
|
||||||
|
LL | if a != b && a => b {}
|
||||||
|
| ^^^^^^^^^^^
|
||||||
|
help: you might have meant to write a "greater than or equal to" comparison
|
||||||
|
|
|
||||||
|
LL | if a != b && a >= b {}
|
||||||
|
| ~~
|
||||||
|
|
||||||
|
error: expected one of `!`, `.`, `::`, `;`, `?`, `else`, `{`, or an operator, found `=>`
|
||||||
|
--> $DIR/eq-gt-to-gt-eq.rs:36:15
|
||||||
|
|
|
||||||
|
LL | let _ = a => b;
|
||||||
|
| ^^ expected one of 8 possible tokens
|
||||||
|
|
|
||||||
|
help: you might have meant to write a "greater than or equal to" comparison
|
||||||
|
|
|
||||||
|
LL | let _ = a >= b;
|
||||||
|
| ~~
|
||||||
|
|
||||||
|
error: expected one of `!`, `.`, `::`, `?`, `{`, or an operator, found `=>`
|
||||||
|
--> $DIR/eq-gt-to-gt-eq.rs:42:13
|
||||||
|
|
|
||||||
|
LL | match a => b {
|
||||||
|
| ----- ^^ expected one of `!`, `.`, `::`, `?`, `{`, or an operator
|
||||||
|
| |
|
||||||
|
| while parsing this `match` expression
|
||||||
|
|
|
||||||
|
help: you might have meant to write a "greater than or equal to" comparison
|
||||||
|
|
|
||||||
|
LL | match a >= b {
|
||||||
|
| ~~
|
||||||
|
|
||||||
|
error: aborting due to 7 previous errors
|
||||||
|
|
Loading…
Reference in New Issue
Block a user