Auto merge of #75321 - estebank:js-goes-gaga, r=davidtwco

Detect JS-style `===` and `!==` and recover

Fix #75312.
This commit is contained in:
bors 2020-08-12 08:40:36 +00:00
commit 5989bf4872
6 changed files with 66 additions and 0 deletions

View File

@ -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 {

View File

@ -0,0 +1,5 @@
fn main() {
if 1 == = 1 { //~ ERROR expected expression
println!("yup!");
}
}

View File

@ -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

View 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!");
}
}

View 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!");
}
}

View 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