Skip to content

FP manual_unwrap_or_default #12531

Description

@matthiaskrgr

Summary

.

Lint Name

manual_unwrap_or_default

Reproducer

I tried this code:

unsafe fn no_deref_ptr(a: Option<i32>, b: *const Option<i32>) -> i32 {
    match a {
        // `*b` being correct depends on `a == Some(_)`
        Some(_) => match *b {
            Some(v) => v,
            _ => 0,
        },
        _ => 0,
    }
}

fn main() {}

I saw this happen:

warning: match can be simplified with `.unwrap_or_default()`
 --> ./tests/mir-opt/early_otherwise_branch_soundness.rs:5:20
  |
5 |           Some(_) => match *b {
  |  ____________________^
6 | |             Some(v) => v,
7 | |             _ => 0,
8 | |         },
  | |_________^ help: replace it with: `*b.unwrap_or_default()`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_unwrap_or_default
  = note: `#[warn(clippy::manual_unwrap_or_default)]` on by default

However the suggestion does not build

error[E0599]: no method named `unwrap_or_default` found for raw pointer `*const std::option::Option<i32>` in the current scope
 --> src/main.rs:5:23
  |
5 |         Some(_) => *b.unwrap_or_default(),
  |                       ^^^^^^^^^^^^^^^^^ method not found in `*const Option<i32>`
  |
  = note: try using `<*const T>::as_ref()` to get a reference to the type behind the pointer: https://doc.rust-lang.org/std/primitive.pointer.html#method.as_ref
  = note: using `<*const T>::as_ref()` on a pointer which is unaligned or points to invalid or uninitialized memory is undefined behavior

error: aborting due to 1 previous error

Version

No response

Additional Labels

No response

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions