dtolnay / syn

Parser for Rust source code
Apache License 2.0
2.81k stars 308 forks source link

Require parens for chained comparison binops #1643

Closed dtolnay closed 4 months ago

dtolnay commented 4 months ago

Previously, syn incorrectly accepted $a == $b == $c as an expression:

Expr::Binary {
    left: Expr::Binary {
        left: $a,
        op: BinOp::Eq,
        right: $b,
    },
    op: BinOp::Eq,
    right: $c,
}

but this is not a legal expression in Rust. The expression either needs to be parenthesized (($a == $b) == $c or $a == ($b == $c)) or turned into a conjunction ($a == $b && $b == $c).

error: comparison operators cannot be chained
 --> src/main.rs:2:18
  |
2 |     let _ = true == true == true;
  |                  ^^      ^^
  |
help: split the comparison into two
  |
2 |     let _ = true == true && true == true;
  |                          +++++++