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

manual_unwrap_or_default suggested fix fails to compile due to missing type annotation #12670

Open
Diomendius opened this issue Apr 12, 2024 · 0 comments · May be fixed by #12688
Open

manual_unwrap_or_default suggested fix fails to compile due to missing type annotation #12670

Diomendius opened this issue Apr 12, 2024 · 0 comments · May be fixed by #12688
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied L-suspicious Lint: Belongs in the suspicious lint group

Comments

@Diomendius
Copy link

Summary

It's possible to bind an if let expression to a variable where the only indication of the variable's type comes from a call to SomeType::default(). If this code triggers the manual_unwrap_or_default lint, the suggested fix avoids the call to SomeType::default() but does not insert a type annotation, causing a compile error due to the now-ambiguous type.

This is a really contrived case which I doubt has much chance of showing up in a real project, but cargo clippy --fix still shouldn't break your code.

Reproducer

Run cargo clippy --fix on a new binary crate with this main.rs:

fn main() {
    let _ = if let Some(x) = None {
        x
    } else {
        i32::default()
    };
}

Clippy should attempt to change this to:

fn main() {
    let _ = None.unwrap_or_default();
}

rustc asks itself, "What type is a piece of None?":

The following errors were reported:
error[E0282]: type annotations needed
 --> src/main.rs:2:9
  |
2 |     let _ = None.unwrap_or_default();
  |         ^   ---- type must be known at this point
  |
help: consider giving this pattern a type
  |
2 |     let _: /* Type */ = None.unwrap_or_default();
  |          ++++++++++++

error: aborting due to 1 previous error

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

warning: if let can be simplified with `.unwrap_or_default()`
 --> src/main.rs:2:13
  |
2 |       let _ = if let Some(x) = None {
  |  _____________^
3 | |         x
4 | |     } else {
5 | |         i32::default()
6 | |     };
  | |_____^ help: replace it with: `None.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

warning: `foo` (bin "foo") generated 1 warning (run `cargo clippy --fix --bin "foo"` to apply 1 suggestion)
warning: failed to automatically apply fixes suggested by rustc to crate `foo`

Version

rustc 1.79.0-nightly (a07f3eb43 2024-04-11)
binary: rustc
commit-hash: a07f3eb43acc5df851e15176c7081a900a30a4d7
commit-date: 2024-04-11
host: x86_64-unknown-linux-gnu
release: 1.79.0-nightly
LLVM version: 18.1.3

Additional Labels

@rustbot label +I-suggestion-causes-error

@Diomendius Diomendius added the C-bug Category: Clippy is not doing the correct thing label Apr 12, 2024
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Apr 12, 2024
@J-ZhengLi J-ZhengLi added the L-suspicious Lint: Belongs in the suspicious lint group label Apr 15, 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-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied L-suspicious Lint: Belongs in the suspicious lint group
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants