Skip to content

Nightly manual_unwrap_or_default attempts to discard side effects #12621

Closed
@cbiffle

Description

@cbiffle

Summary

Clippy on current nightly fires manual_unwrap_or_default and proposes code that would remove side effects on at least the None branch.

Lint Name

manual_unwrap_or_default

Reproducer

Given the code:

        let addr = match addr {
            Some(a) => a,
            None => {
                self.sector_erase(
                    0,
                    HfProtectMode::AllowModificationsToSector0,
                )?;
                0
            }
        };

...clippy fires the manual_unwrap_or_default lint with this advice:

warning: match can be simplified with `.unwrap_or_default()`
   --> drv/gimlet-hf-server/src/main.rs:425:20
    |
425 |           let addr = match addr {
    |  ____________________^
426 | |             Some(a) => a,
427 | |             None => {
428 | |                 self.sector_erase(
...   |
433 | |             }
434 | |         };
    | |_________^ help: replace it with: `addr.unwrap_or_default()`
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_unwrap_or_default
    = note: `-W clippy::manual-unwrap-or-default` implied by `-W clippy::all`
    = help: to override `-W clippy::all` add `#[allow(clippy::manual_unwrap_or_default)]`

While it is correct that 0 is the Default value for the type in question, if I were to follow its suggestion, it would remove the sector_erase call on the None case... which is kind of important.

Version

rustc 1.79.0-nightly (805813650 2024-03-31)
binary: rustc
commit-hash: 805813650248c1a2f6f271460d728d1bb852d2a7
commit-date: 2024-03-31
host: x86_64-unknown-linux-gnu
release: 1.79.0-nightly
LLVM version: 18.1.2

Additional Labels

@rustbot label +I-suggestion-causes-error

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