Skip to content

Require parens for chained comparison binops#1643

Merged
dtolnay merged 2 commits intomasterfrom
chainedcompare
May 11, 2024
Merged

Require parens for chained comparison binops#1643
dtolnay merged 2 commits intomasterfrom
chainedcompare

Conversation

@dtolnay
Copy link
Copy Markdown
Owner

@dtolnay dtolnay commented May 11, 2024

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;
  |                          +++++++

@dtolnay dtolnay merged commit c79cea1 into master May 11, 2024
@dtolnay dtolnay deleted the chainedcompare branch May 11, 2024 03:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant