fixes #101477: Recover from typo where == is used in place of =
This commit is contained in:
parent
e7c7aa7288
commit
ddb225f1f5
8 changed files with 73 additions and 0 deletions
|
@ -153,3 +153,6 @@ parser_left_arrow_operator = unexpected token: `<-`
|
||||||
|
|
||||||
parser_remove_let = expected pattern, found `let`
|
parser_remove_let = expected pattern, found `let`
|
||||||
.suggestion = remove the unnecessary `let` keyword
|
.suggestion = remove the unnecessary `let` keyword
|
||||||
|
|
||||||
|
parser_use_eq_instead = unexpected `==`
|
||||||
|
.suggestion = try using `=` instead
|
||||||
|
|
|
@ -713,6 +713,14 @@ pub(crate) struct RemoveLet {
|
||||||
pub span: Span,
|
pub span: Span,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(SessionDiagnostic)]
|
||||||
|
#[diag(parser::use_eq_instead)]
|
||||||
|
pub(crate) struct UseEqInstead {
|
||||||
|
#[primary_span]
|
||||||
|
#[suggestion_short(applicability = "machine-applicable", code = "=")]
|
||||||
|
pub span: Span,
|
||||||
|
}
|
||||||
|
|
||||||
// SnapshotParser is used to create a snapshot of the parser
|
// SnapshotParser is used to create a snapshot of the parser
|
||||||
// without causing duplicate errors being emitted when the `Parser`
|
// without causing duplicate errors being emitted when the `Parser`
|
||||||
// is dropped.
|
// is dropped.
|
||||||
|
@ -957,6 +965,14 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if self.token.kind == TokenKind::EqEq
|
||||||
|
&& self.prev_token.is_ident()
|
||||||
|
&& expected.iter().any(|tok| matches!(tok, TokenType::Token(TokenKind::Eq)))
|
||||||
|
{
|
||||||
|
// Likely typo: `=` → `==` in let expr or enum item
|
||||||
|
return Err(self.sess.create_err(UseEqInstead { span: self.token.span }));
|
||||||
|
}
|
||||||
|
|
||||||
let expect = tokens_to_string(&expected);
|
let expect = tokens_to_string(&expected);
|
||||||
let actual = super::token_descr(&self.token);
|
let actual = super::token_descr(&self.token);
|
||||||
let (msg_exp, (label_sp, label_exp)) = if expected.len() > 1 {
|
let (msg_exp, (label_sp, label_exp)) = if expected.len() > 1 {
|
||||||
|
|
10
src/test/ui/parser/issue-101477-enum.fixed
Normal file
10
src/test/ui/parser/issue-101477-enum.fixed
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
// run-rustfix
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
enum Demo {
|
||||||
|
A = 1,
|
||||||
|
B = 2 //~ ERROR unexpected `==`
|
||||||
|
//~^ expected item, found `==`
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
10
src/test/ui/parser/issue-101477-enum.rs
Normal file
10
src/test/ui/parser/issue-101477-enum.rs
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
// run-rustfix
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
enum Demo {
|
||||||
|
A = 1,
|
||||||
|
B == 2 //~ ERROR unexpected `==`
|
||||||
|
//~^ expected item, found `==`
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
14
src/test/ui/parser/issue-101477-enum.stderr
Normal file
14
src/test/ui/parser/issue-101477-enum.stderr
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
error: unexpected `==`
|
||||||
|
--> $DIR/issue-101477-enum.rs:6:7
|
||||||
|
|
|
||||||
|
LL | B == 2
|
||||||
|
| ^^ help: try using `=` instead
|
||||||
|
|
||||||
|
error: expected item, found `==`
|
||||||
|
--> $DIR/issue-101477-enum.rs:6:7
|
||||||
|
|
|
||||||
|
LL | B == 2
|
||||||
|
| ^^ expected item
|
||||||
|
|
||||||
|
error: aborting due to 2 previous errors
|
||||||
|
|
6
src/test/ui/parser/issue-101477-let.fixed
Normal file
6
src/test/ui/parser/issue-101477-let.fixed
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
// run-rustfix
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let x = 2; //~ ERROR unexpected `==`
|
||||||
|
println!("x: {}", x)
|
||||||
|
}
|
6
src/test/ui/parser/issue-101477-let.rs
Normal file
6
src/test/ui/parser/issue-101477-let.rs
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
// run-rustfix
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let x == 2; //~ ERROR unexpected `==`
|
||||||
|
println!("x: {}", x)
|
||||||
|
}
|
8
src/test/ui/parser/issue-101477-let.stderr
Normal file
8
src/test/ui/parser/issue-101477-let.stderr
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
error: unexpected `==`
|
||||||
|
--> $DIR/issue-101477-let.rs:4:11
|
||||||
|
|
|
||||||
|
LL | let x == 2;
|
||||||
|
| ^^ help: try using `=` instead
|
||||||
|
|
||||||
|
error: aborting due to previous error
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue