Skip to content

Nightly clippy: manual_unwrap_or false positive  #13018

@ilyagr

Description

@ilyagr

Summary

This is the same as (now fixed) #12564, but now the lint is manual_unwrap_or instead of manual_unwrap_or_default.

There is a false positive; cargo clippy --fix fails

Lint Name

manual_unwrap_or

Reproducer

I tried this code:

    #[allow(unknown_lints)] // XXX FIXME (aseipp): nightly bogons; re-test this occasionally
    #[allow(clippy::manual_unwrap_or_default)]
    pub fn get(&self, id: &CommitId) -> &[Rc<RefName>] {
        if let Some(names) = self.index.get(id) {
            names
        } else {
            &[]
        }
    }

from

https://github.com/ilyagr/jj/blob/4b45dde8c6b2f038cb68c8250d3f7313c4f9cd8c/cli/src/commit_templater.rs#L738-L746

Running cargo clippy --fix fails with:

warning: this pattern reimplements `Option::unwrap_or`
    --> cli/src/commit_templater.rs:1033:9
     |
1033 | /         if let Some(names) = self.index.get(id) {
1034 | |             names
1035 | |         } else {
1036 | |             &[]
1037 | |         }
     | |_________^ help: replace with: `self.index.get(id).unwrap_or(&[])`
     |
     = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_unwrap_or
     = note: `#[warn(clippy::manual_unwrap_or)]` on by default

warning: `jj-cli` (lib) generated 1 warning (run `cargo clippy --fix --lib -p jj-cli` to apply 1 suggestion)
warning: failed to automatically apply fixes suggested by rustc to crate `jj_cli`

after fixes were automatically applied the compiler reported errors within these files:

  * cli/src/commit_templater.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0308]: mismatched types
    --> cli/src/commit_templater.rs:1033:38
     |
1033 |         self.index.get(id).unwrap_or(&[])
     |                            --------- ^^^ expected `&Vec<Rc<...>>`, found `&[_; 0]`
     |                            |
     |                            arguments to this method are incorrect
     |
     = note: expected reference `&std::vec::Vec<std::rc::Rc<commit_templater::RefName>>`
                found reference `&[_; 0]`
help: the return type of this call is `&[_; 0]` due to the type of the argument passed
    --> cli/src/commit_templater.rs:1033:9
     |
1033 |         self.index.get(id).unwrap_or(&[])
     |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---^
     |                                      |
     |                                      this argument influences the return type of `unwrap_or`
note: method defined here
    --> /Users/ilyagr/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/option.rs:983:12
     |
983  |     pub fn unwrap_or(self, default: T) -> T {
     |            ^^^^^^^^^

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
Original diagnostics will follow.

Version

rustc 1.81.0-nightly (e9e6e2e44 2024-06-28)
binary: rustc
commit-hash: e9e6e2e444c30c23a9c878a88fbc3978c2acad95
commit-date: 2024-06-28
host: aarch64-apple-darwin
release: 1.81.0-nightly
LLVM version: 18.1.7

This issue was not present in the nightly from a week ago (2024-06-21).

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