Skip to content

When seeing x<-y in a boolean expression, suggest x < -y. #45651

Closed
@kennytm

Description

@kennytm

Test case:

fn main() {
    let x = -5;
    if x<-1 {
        println!("ok");
    }
}

The error currently looks like:

error: placement-in expression syntax is experimental and subject to change. (see issue #27779)
 --> src/main.rs:3:8
  |
3 |     if x<-1 {
  |        ^^^^
  |
  = help: add #![feature(placement_in_syntax)] to the crate attributes to enable

but the intent was not to use the placement-in, the suggestion is just confusing the user. If we do add #![feature(placement_in_syntax)] the error message would be even worse:

error[E0277]: the trait bound `{integer}: std::ops::Placer<_>` is not satisfied
 --> src/main.rs:5:8
  |
5 |     if x<-1 {
  |        ----
  |        |
  |        the trait `std::ops::Placer<_>` is not implemented for `{integer}`
  |        in this macro invocation
  |
  = help: the following implementations were found:
            <std::collections::linked_list::BackPlace<'a, T> as std::ops::Placer<T>>
            <std::collections::vec_deque::PlaceBack<'a, T> as std::ops::Placer<T>>
            <std::collections::hash_map::Entry<'a, K, V> as std::ops::Placer<V>>
            <&'a mut std::collections::BinaryHeap<T> as std::ops::Placer<T>>
          and 4 others
  = note: required by `std::ops::Placer::make_place`

The suggestion should actually mention inserting a space between < and - if a comparison with a negative number is intended, e.g.

error: placement-in expression syntax is experimental and subject to change. (see issue #27779)
 --> src/main.rs:3:8
  |
3 |     if x<-1 {
  |        ^^^^
  |        hint: for less-than comparison, try to add some spaces: `x < -1`
  |
  = help: add #![feature(placement_in_syntax)] to the crate attributes to enable

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-frontendArea: Compiler frontend (errors, parsing and HIR)C-feature-requestCategory: A feature request, i.e: not implemented / a PR.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions