Skip to content

Error for chained less-than/greater-than comparisons misleading. #65659

Closed
@vext01

Description

@vext01

Hi,

As I understand, chaining inequality operators like <, >, <= and >= is not allowed. Yet trying to do so will ask you to add parenthesies:

fn main() {
    let x = 2;
    
    if 1 <= x <= 3 {
        println!("yes");
    } else {
        println!("no");
    }
}

Gives:

error: chained comparison operators require parentheses
 --> src/main.rs:4:10
  |
4 |     if 1 <= x <= 3 {
  |          ^^^^^^^

error[E0308]: mismatched types
 --> src/main.rs:4:18
  |
4 |     if 1 <= x <= 3 {
  |                  ^ expected bool, found integer
  |
  = note: expected type `bool`
             found type `{integer}`

The first error is false advice in the context of <=. It would have been ok for == though.

Thanks :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions