Skip to content

cmp_null autofix generates incorrect code #15114

Closed
@yuvald-sweet-security

Description

@yuvald-sweet-security

Summary

the cmp_null autofix doesn't take into account the type of comparison, and instead always replaces the statement with a positive comparison on is_null().

Reproducer

I tried this code:

fn f(x: *mut i32) {
    if x != std::ptr::null_mut() {
        unsafe {
            *x += 1;
        }
    }
}

I expected to see this happen:

fn f(x: *mut i32) {
    if !x.is_null() {
        unsafe {
            *x += 1;
        }
    }
}

Instead, this happened:

fn f(x: *mut i32) {
    if x.is_null() {
        unsafe {
            *x += 1;
        }
    }
}

Version

clippy 0.1.87 (17067e9ac6 2025-05-09)

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions