Skip to content

cmp_null suggests incorrect code, and --fix applies it #15010

Closed
@zkrising

Description

@zkrising

Summary

debug_assert!(ptr != std::ptr::null_mut()) will be suggested to be replaced with debug_assert!(ptr.is_null()), which has the opposite meaning

I found this by running clippy --fix on my codebase.

Reproducer

I tried this code:

let f: *mut i32 = std::ptr::null_mut();

debug_assert!(f != std::ptr::null_mut());

I expected to see this happen:

Use debug_assert!(!f.is_null())` instead

Instead, this happened:

warning: comparing with null is better expressed by the `.is_null()` method
   --> main.rs:165:17
    |
165 |         debug_assert!(f != std::ptr::null_mut());
    |                       ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `f.is_null()`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cmp_null
    = note: `#[warn(clippy::cmp_null)]` on by default

Version

rustc 1.89.0-nightly (4b27a04cc 2025-06-04)
binary: rustc
commit-hash: 4b27a04cc8ed4da10a546a871e23e665d03f7a79
commit-date: 2025-06-04
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5

Additional Labels

No response

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-bugIssue: The suggestion compiles but changes the code to behave in an unintended way

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions