Auto merge of #75321 - estebank:js-goes-gaga, r=davidtwco
Detect JS-style `===` and `!==` and recover Fix #75312.
This commit is contained in:
commit
5989bf4872
6 changed files with 66 additions and 0 deletions
|
@ -195,6 +195,29 @@ impl<'a> Parser<'a> {
|
|||
return Ok(expr);
|
||||
}
|
||||
}
|
||||
|
||||
if (op.node == AssocOp::Equal || op.node == AssocOp::NotEqual)
|
||||
&& self.token.kind == token::Eq
|
||||
&& self.prev_token.span.hi() == self.token.span.lo()
|
||||
{
|
||||
// Look for JS' `===` and `!==` and recover 😇
|
||||
let sp = op.span.to(self.token.span);
|
||||
let sugg = match op.node {
|
||||
AssocOp::Equal => "==",
|
||||
AssocOp::NotEqual => "!=",
|
||||
_ => unreachable!(),
|
||||
};
|
||||
self.struct_span_err(sp, &format!("invalid comparison operator `{}=`", sugg))
|
||||
.span_suggestion_short(
|
||||
sp,
|
||||
&format!("`{s}=` is not a valid comparison operator, use `{s}`", s = sugg),
|
||||
sugg.to_string(),
|
||||
Applicability::MachineApplicable,
|
||||
)
|
||||
.emit();
|
||||
self.bump();
|
||||
}
|
||||
|
||||
let op = op.node;
|
||||
// Special cases:
|
||||
if op == AssocOp::As {
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
fn main() {
|
||||
if 1 == = 1 { //~ ERROR expected expression
|
||||
println!("yup!");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
error: expected expression, found `=`
|
||||
--> $DIR/js-style-comparison-op-separate-eq-token.rs:2:13
|
||||
|
|
||||
LL | if 1 == = 1 {
|
||||
| ^ expected expression
|
||||
|
||||
error: aborting due to previous error
|
||||
|
8
src/test/ui/suggestions/js-style-comparison-op.fixed
Normal file
8
src/test/ui/suggestions/js-style-comparison-op.fixed
Normal file
|
@ -0,0 +1,8 @@
|
|||
// run-rustfix
|
||||
fn main() {
|
||||
if 1 == 1 { //~ ERROR invalid comparison operator `===`
|
||||
println!("yup!");
|
||||
} else if 1 != 1 { //~ ERROR invalid comparison operator `!==`
|
||||
println!("nope!");
|
||||
}
|
||||
}
|
8
src/test/ui/suggestions/js-style-comparison-op.rs
Normal file
8
src/test/ui/suggestions/js-style-comparison-op.rs
Normal file
|
@ -0,0 +1,8 @@
|
|||
// run-rustfix
|
||||
fn main() {
|
||||
if 1 === 1 { //~ ERROR invalid comparison operator `===`
|
||||
println!("yup!");
|
||||
} else if 1 !== 1 { //~ ERROR invalid comparison operator `!==`
|
||||
println!("nope!");
|
||||
}
|
||||
}
|
14
src/test/ui/suggestions/js-style-comparison-op.stderr
Normal file
14
src/test/ui/suggestions/js-style-comparison-op.stderr
Normal file
|
@ -0,0 +1,14 @@
|
|||
error: invalid comparison operator `===`
|
||||
--> $DIR/js-style-comparison-op.rs:3:10
|
||||
|
|
||||
LL | if 1 === 1 {
|
||||
| ^^^ help: `===` is not a valid comparison operator, use `==`
|
||||
|
||||
error: invalid comparison operator `!==`
|
||||
--> $DIR/js-style-comparison-op.rs:5:17
|
||||
|
|
||||
LL | } else if 1 !== 1 {
|
||||
| ^^^ help: `!==` is not a valid comparison operator, use `!=`
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
Loading…
Reference in a new issue