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

Nightly manual_unwrap_or_default attempts to discard side effects #12621

Closed
cbiffle opened this issue Apr 2, 2024 · 3 comments
Closed

Nightly manual_unwrap_or_default attempts to discard side effects #12621

cbiffle opened this issue Apr 2, 2024 · 3 comments
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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@cbiffle
Copy link

cbiffle commented Apr 2, 2024

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

@cbiffle cbiffle 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 Apr 2, 2024
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Apr 2, 2024
@cbiffle
Copy link
Author

cbiffle commented Apr 2, 2024

In case it's helpful, here's another one:

                    if let Some(b) = txbuf.read() {
                        b
                    } else {
                        // We've hit the end of the lease. Stop checking.
                        tx = None;
                        0
                    }

(Same Clippy behavior.)

@y21
Copy link
Member

y21 commented Apr 2, 2024

This looks like a duplicate of #12569 , which was fixed by #12579

@cbiffle
Copy link
Author

cbiffle commented Apr 2, 2024

Ah, great, thank you -- I managed to search for that without finding it.

@cbiffle cbiffle closed this as completed Apr 2, 2024
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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

No branches or pull requests

3 participants