rust-lang / rust-lang/rust-clippy

Extend `integer_const` to cover `NonZero` type and `NonZero*` type aliases

Open
#17,481 4 comments 0 reactions 1 assignee View on GitHub

@arunsingh is already working on this.

Since Aug 5, 2026.

C-enhancement
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Description

clippy_utils::consts::integer_const cannot catch NonZero::BITS, NonZeroU32::BITS and so on.

Reproducer
// src
impl<'tcx> LateLintPass<'tcx> for Dummy {
    fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
        if let Some(_) = integer_const(cx, expr, expr.span.ctxt()) {
            println!("{:#?}", expr)
        }
    }
}
// ui
use std::num::{NonZero, NonZeroU32};

fn main() {
    let _ = NonZero::<u32>::BITS;
    let _ = NonZeroU32::BITS;
    let _ = u32::BITS;
}
Output: `integer_const` catches `u32::BITS` only
Expr {
    hir_id: HirId(DefId(0:6 ~ dummy[e3e9]::main).19),
    kind: Path(
        TypeRelative(
            Ty {
                hir_id: HirId(DefId(0:6 ~ dummy[e3e9]::main).21),
                span: tests/ui/dummy.rs:8:13: 8:16 (#0),
                kind: Path(
                    Resolved(
                        None,
                        Path {
                            span: tests/ui/dummy.rs:8:13: 8:16 (#0),
                            res: PrimTy(
                                Uint(
                                    u32,
                                ),
                            ),
                            segments: [
                                PathSegment {
                                    ident: u32#0,
                                    hir_id: HirId(DefId(0:6 ~ dummy[e3e9]::main).20),
                                    res: PrimTy(
                                        Uint(
                                            u32,
                                        ),
                                    ),
                                    args: None,
                                    infer_args: true,
                                    delegation_child_segment: false,
                                },
                            ],
                        },
                    ),
                ),
            },
            PathSegment {
                ident: BITS#0,
                hir_id: HirId(DefId(0:6 ~ dummy[e3e9]::main).22),
                res: Err,
                args: None,
                infer_args: true,
                delegation_child_segment: false,
            },
        ),
    ),
    span: tests/ui/dummy.rs:8:13: 8:22 (#0),
}
Version
rustc 1.100.0-nightly (0dfb098f3 2026-08-31)
binary: rustc
commit-hash: 0dfb098f3aeecbe38c2566ca090193280e7349e7
commit-date: 2026-08-31
host: x86_64-pc-windows-msvc
release: 1.100.0-nightly
LLVM version: 23.1.0
Additional Labels

@rustbot label +C-enhancement

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.