Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FP manual_unwrap_or_default #12531

Closed
matthiaskrgr opened this issue Mar 22, 2024 · 1 comment · Fixed by #12532
Closed

FP manual_unwrap_or_default #12531

matthiaskrgr opened this issue Mar 22, 2024 · 1 comment · Fixed by #12532
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@matthiaskrgr
Copy link
Member

matthiaskrgr commented Mar 22, 2024

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

@matthiaskrgr matthiaskrgr added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Mar 22, 2024
@samueltardieu
Copy link
Contributor

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants