Skip to content

ambiguous_wide_pointer_comparisons lint does not trigger for NonNull comparisons #121264

Closed
@rj00a

Description

@rj00a

Code

#![allow(dead_code)]

use std::ptr::NonNull;

trait Foo {}

fn test(a: NonNull<dyn Foo>, b: NonNull<dyn Foo>) -> bool {
    a == b
}

fn main() {}

Current output

<no warnings>

Desired output

warning: ambiguous wide pointer comparison, the comparison includes metadata which may not be expected
 --> src/main.rs:8:5
  |
8 |     a == b
  |     ^^^^^^
  |
  = note: `#[warn(ambiguous_wide_pointer_comparisons)]` on by default
help: use `std::ptr::addr_eq` or untyped pointers to only compare their addresses
  |
8 |     std::ptr::addr_eq(a.as_ptr(), b.as_ptr())
  |     ++++++++++++++++++ ++++++++++  ++++++++++

Rationale and extra context

#117758 added the ambiguous_wide_pointer_comparisons lint, but the implementation doesn't consider pointer types beyond *const T and *mut T.

Other cases

I've also found that explicit PartialOrd::partial_cmp and Ord::cmp calls are not warned with ordinary pointers.

fn test_1(a: *const dyn Foo, b: *const dyn Foo) -> Option<std::cmp::Ordering> {
    a.partial_cmp(&b) // No warning.
}

fn test_2(a: *const dyn Foo, b: *const dyn Foo) -> std::cmp::Ordering {
    a.cmp(&b) // No warning.
}

Rust Version

rustc 1.78.0-nightly (6672c16af 2024-02-17)
binary: rustc
commit-hash: 6672c16afcd4db8acdf08a6984fd4107bf07632c
commit-date: 2024-02-17
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 18.1.0

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions