-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
non_zero_suggestions
is triggered when converting from a nonzero value to a larger unsigned value
#13757
Comments
non_zero_suggestions
non_zero_suggestions
is triggered when converting from a nonzero value to a larger unsigned value
I'm not an expert but this seems like expected behavior to me. What you're doing is creating a much more complicated, checked numeric type, with the overhead that entails, only to immediately convert it to a less-safe native type. If you do need to do something like this you could instead do either of the below:
|
You misunderstand that my example is just a minimal reproducible example not a realistic one. The point is if you have a Here is a more realistic example if you don't see how one doesn't always control the types involved: use external_crate_i_dont_control::Bar;
use other_crate_i_dont_control as other;
fn main() {
// `Bar` has a `NonZeroU32` field called `val`.
// `foo` takes a `u64` and prints it to `stdout`.
other::foo(Bar::new().val.get().into());
}
|
One approach to fix this is to ignore temporaries since they are clearly only used once. Even better would be to only recommend converting to |
I see what you mean now, looks like this was noticed during implementation but wasn't seen as worth fixing at the time: #13167 (comment) I don't think either of the approaches I suggested are better, I just mentioned them since they got around the basic example for me. As for the suggestion being wrong, I think it's allowed in general for suggestions to be incorrect. There's a concept of 'applicability' of suggestions, and this suggestion is marked as I also see it's in the Restriction lint group so that might have something to do with it being overly limiting/incorrect. Leaving this in case any of the context is helpful but I'm very new to Clippy development so I think this is over my head. |
Ah, indeed it was noticed. Good to know. I'll let the team close this if they still deem it not worthy to fix. |
Summary
T::from
is getting confused withG::from
whenT
is an unsigned integer (e.g.,u64
) andG
is the nonzero version (e.g.,NonZeroU64
). For example converting aNonZeroU32
into au64
triggers the lint incorrectly.Lint Name
non_zero_suggestions
Reproducer
I tried this code:
I saw this happen:
I expected to see this happen: Compilation to succeed since
u64::from
is not the same asNonZeroU64::from
.Version
Additional Labels
No response
The text was updated successfully, but these errors were encountered: