Skip to content

Nightly clippy: manual_unwrap_or false positive #13018

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

Closed
ilyagr opened this issue Jun 29, 2024 · 4 comments · Fixed by #13061
Closed

Nightly clippy: manual_unwrap_or false positive #13018

ilyagr opened this issue Jun 29, 2024 · 4 comments · Fixed by #13061
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

@ilyagr
Copy link

ilyagr commented Jun 29, 2024

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

@ilyagr ilyagr 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 Jun 29, 2024
ilyagr added a commit to ilyagr/jj that referenced this issue Jun 29, 2024
ilyagr added a commit to ilyagr/jj that referenced this issue Jun 29, 2024
ilyagr added a commit to jj-vcs/jj that referenced this issue Jun 29, 2024
@Alexendoo Alexendoo added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Jun 30, 2024
@tesuji
Copy link
Contributor

tesuji commented Jul 7, 2024

Could you extract a minimal example on playground? I tried a bit but I couldn't reproduce the issue.
@rustbot label -E-needs-mcve

@rustbot rustbot added the E-needs-mcve Call for participation: This issue needs a Minimal Complete and Verifiable Example label Jul 7, 2024
@ilyagr
Copy link
Author

ilyagr commented Jul 7, 2024

It reproduces for me. I think you were using the stable version?

Link. I go to Tools -> Clippy to get this result:

image

@tesuji
Copy link
Contributor

tesuji commented Jul 7, 2024

Thanks. I thought clippy on playground always run with nightly version. I did check by switching to nightly channel before. I guess my eyes was failing me. :O

@rustbot rustbot removed the E-needs-mcve Call for participation: This issue needs a Minimal Complete and Verifiable Example label Jul 7, 2024
@ilyagr
Copy link
Author

ilyagr commented Jul 7, 2024

No problem. Thank you for making the example!

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

Successfully merging a pull request may close this issue.

4 participants