Skip to content

non_zero_suggestions is triggered when converting from a nonzero value to a larger unsigned value #13757

Open
@zacknewman

Description

@zacknewman

Summary

T::from is getting confused with G::from when T is an unsigned integer (e.g., u64) and G is the nonzero version (e.g., NonZeroU64). For example converting a NonZeroU32 into a u64 triggers the lint incorrectly.

Lint Name

non_zero_suggestions

Reproducer

I tried this code:

#![deny(clippy::non_zero_suggestions)]
use core::num::NonZeroU32;
fn main() {
    _ = u64::from(NonZeroU32::new(10).unwrap().get());
}

I saw this happen:

error: consider using `NonZeroU64::from()` for more efficient and type-safe conversion
 --> src/main.rs:4:9
  |
4 |     _ = u64::from(NonZeroU32::new(10).unwrap().get());
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `NonZeroU64::from(NonZeroU32::new(10).unwrap())`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#non_zero_suggestions
note: the lint level is defined here
 --> src/main.rs:1:9
  |
1 | #![deny(clippy::non_zero_suggestions)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: could not compile `bug` (bin "bug") due to 1 previous error

I expected to see this happen: Compilation to succeed since u64::from is not the same as NonZeroU64::from.

Version

rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: x86_64-unknown-linux-gnu
release: 1.83.0
LLVM version: 19.1.1

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions