Skip to content

Commit 5989bf4

Browse files
committed
Auto merge of #75321 - estebank:js-goes-gaga, r=davidtwco
Detect JS-style `===` and `!==` and recover Fix #75312.
2 parents 4745cbe + 308b558 commit 5989bf4

6 files changed

+66
-0
lines changed

src/librustc_parse/parser/expr.rs

+23
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,29 @@ impl<'a> Parser<'a> {
195195
return Ok(expr);
196196
}
197197
}
198+
199+
if (op.node == AssocOp::Equal || op.node == AssocOp::NotEqual)
200+
&& self.token.kind == token::Eq
201+
&& self.prev_token.span.hi() == self.token.span.lo()
202+
{
203+
// Look for JS' `===` and `!==` and recover 😇
204+
let sp = op.span.to(self.token.span);
205+
let sugg = match op.node {
206+
AssocOp::Equal => "==",
207+
AssocOp::NotEqual => "!=",
208+
_ => unreachable!(),
209+
};
210+
self.struct_span_err(sp, &format!("invalid comparison operator `{}=`", sugg))
211+
.span_suggestion_short(
212+
sp,
213+
&format!("`{s}=` is not a valid comparison operator, use `{s}`", s = sugg),
214+
sugg.to_string(),
215+
Applicability::MachineApplicable,
216+
)
217+
.emit();
218+
self.bump();
219+
}
220+
198221
let op = op.node;
199222
// Special cases:
200223
if op == AssocOp::As {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
if 1 == = 1 { //~ ERROR expected expression
3+
println!("yup!");
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: expected expression, found `=`
2+
--> $DIR/js-style-comparison-op-separate-eq-token.rs:2:13
3+
|
4+
LL | if 1 == = 1 {
5+
| ^ expected expression
6+
7+
error: aborting due to previous error
8+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// run-rustfix
2+
fn main() {
3+
if 1 == 1 { //~ ERROR invalid comparison operator `===`
4+
println!("yup!");
5+
} else if 1 != 1 { //~ ERROR invalid comparison operator `!==`
6+
println!("nope!");
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// run-rustfix
2+
fn main() {
3+
if 1 === 1 { //~ ERROR invalid comparison operator `===`
4+
println!("yup!");
5+
} else if 1 !== 1 { //~ ERROR invalid comparison operator `!==`
6+
println!("nope!");
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: invalid comparison operator `===`
2+
--> $DIR/js-style-comparison-op.rs:3:10
3+
|
4+
LL | if 1 === 1 {
5+
| ^^^ help: `===` is not a valid comparison operator, use `==`
6+
7+
error: invalid comparison operator `!==`
8+
--> $DIR/js-style-comparison-op.rs:5:17
9+
|
10+
LL | } else if 1 !== 1 {
11+
| ^^^ help: `!==` is not a valid comparison operator, use `!=`
12+
13+
error: aborting due to 2 previous errors
14+

0 commit comments

Comments
 (0)