Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require parens for chained comparison binops #1643

Merged
merged 2 commits into from
May 11, 2024
Merged

Require parens for chained comparison binops #1643

merged 2 commits into from
May 11, 2024

Conversation

dtolnay
Copy link
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
29 checks passed
@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