Skip to content

redundant_pattern_matching: no is_none() for raw pointer #13902

@matthiaskrgr

Description

@matthiaskrgr

Summary

.

Lint Name

redundant_pattern_matching

Reproducer

I tried this code:

use std::mem; // If this is `None`, the metadata becomes padding. type T = Option<&'static str>; fn main() { unsafe { let mut p: mem::MaybeUninit<T> = mem::MaybeUninit::zeroed(); // The copy when `T` is returned from `transmute` should destroy padding // (even when we use `write_unaligned`, which under the hood uses an untyped copy). p.as_mut_ptr().write_unaligned(mem::transmute((0usize, 0usize))); // Null epresents `None`. assert!(matches!(*p.as_ptr(), None)); // The second part, with the length, becomes padding. let c = &p as *const _ as *const u8; // Read a padding byte. let _val = *c.add(mem::size_of::<*const u8>()); //~^ERROR: uninitialized } }

I saw this happen:

warning: redundant pattern matching, consider using `is_none()` --> src/main.rs:13:17 | 13 | assert!(matches!(*p.as_ptr(), None)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `*p.as_ptr().is_none()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern_matching = note: `#[warn(clippy::redundant_pattern_matching)]` on by default 

This does not compile:

error[E0599]: no method named `is_none` found for raw pointer `*const std::option::Option<&str>` in the current scope --> src/main.rs:13:29 | 13 | assert!(*p.as_ptr().is_none()); | ^^^^^^^ method not found in `*const Option<&str>` | note: the method `is_none` exists on the type `&std::option::Option<&str>` --> /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/option.rs:651:5 | 651 | pub const fn is_none(&self) -> bool { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = note: you might want to use the unsafe method `<*const T>::as_ref` to get an optional reference to the value behind the pointer = note: read the documentation for `<*const T>::as_ref` and ensure you satisfy its safety preconditions before calling it to avoid undefined behavior: https://doc.rust-lang.org/std/primitive.pointer.html#method.as_ref 

Version

rustc 1.85.0-nightly (8742e0556 2024-12-28) binary: rustc commit-hash: 8742e0556dee3c64f7144de2fb2e88936418865a commit-date: 2024-12-28 host: x86_64-unknown-linux-gnu release: 1.85.0-nightly LLVM version: 19.1.6 

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 haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions