Document nonzero invariant on NonZero::unchecked_add#158598
Conversation
|
r? @joboet rustbot has assigned @joboet. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
fc61422 to
de2f8be
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
de2f8be to
6841cb7
Compare
|
This actually seems correct? It starts with a nonzero value and you're adding an unsigned value, so zero inherently cannot occur without wrapping — which is already called out as UB. |
|
Since overflow cannot occur, So I think this can be left to the developer's common sense. Also the type is named |
|
Aha, I was looking through this macro to try and understand this code and didn't realize this macro was only applying to unsigned types. Thanks!
From a safety review perspective I always prefer explicitly documented invariants. You'd be surprised how many people incorrectly call |
|
I definitely see that a fair amount as well. I'm not opposed to explicitly documenting it, but I don't think it's particularly necessary given the current wording 👍 |
Safety bug identified during agentic unsafe Rust audit.
This is kind of an obvious invariant but it's technically missing from the docs.