Skip to content

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

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

Closed
leonardo-m opened this issue Oct 1, 2020 · 1 comment · Fixed by #77578
Closed

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

leonardo-m opened this issue Oct 1, 2020 · 1 comment · Fixed by #77578
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@leonardo-m
Copy link

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.

@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 1, 2020
@jyn514 jyn514 added the A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` label Oct 1, 2020
@varkor
Copy link
Member

varkor commented Oct 1, 2020

This is related to (or possibly a duplicate of) #76413.

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Oct 8, 2020
suggest `MAX` constant if -1 is assigned to unsigned type

Fixes rust-lang#76413.
Fixes rust-lang#77416.
@bors bors closed this as completed in 2359ecc Oct 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants