Skip to content

[ER] Propose the use of XX::MAX instead of -1 for unsigned integers #77416

Closed
@leonardo-m

Description

@leonardo-m

Another of those little enhancement suggestions. This code:

fn main() {
    let x: u32 = -1;
}

With rustc 1.48.0-nightly gives this error message:

error[E0600]: cannot apply unary operator `-` to type `u32`
 --> ...\temp.rs:2:18
  |
2 |     let x: u32 = -1;
  |                  ^^ cannot apply unary operator `-`
  |
  = note: unsigned values cannot be negated

I suggest to modify the note, to suggest that u32::MAX may be desired instead, because in C code often you see people use -1 to get the max unsigned value.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.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