rust-lang / rust-lang/rust-clippy

FP equatable_if_let: no longer able to infer type

Open
#9,888 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-positive I-suggestion-causes-error L-nursery
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

.

Lint Name

equatable_if_let

Reproducer

I tried this code:

#![warn(clippy::redundant_else)]
#![allow(clippy::needless_return, clippy::if_same_then_else, clippy::needless_late_init)]

fn main() {
   
    // inside if let
    let _ = if let Some(1) = foo() {
        let _ = 1;
        if foo() {
            return;
        } else {
            1
        }
    } else {
        1
    };

}

fn foo<T>() -> T {
    unimplemented!("I'm not Santa Claus")
}

I saw this happen:
cargo clippy --fix -- -Wclippy::equatable_if_let

warning: failed to automatically apply fixes suggested by rustc to crate `a`

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

  * src/main.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/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:
warning: redundant else block
  --> src/main.rs:11:16
   |
11 |           } else {
   |  ________________^
12 | |             1
13 | |         }
   | |_________^
   |
   = help: remove the `else` block and move the contents out
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_else
note: the lint level is defined here
  --> src/main.rs:1:9
   |
1  | #![warn(clippy::redundant_else)]
   |         ^^^^^^^^^^^^^^^^^^^^^^

error[E0282]: type annotations needed
 --> src/main.rs:7:16
  |
7 |     let _ = if foo() == Some(1) {
  |                ^^^ cannot infer type of the type parameter `T` declared on the function `foo`
  |
help: consider specifying the generic argument
  |
7 |     let _ = if foo::<T>() == Some(1) {
  |                   +++++

error: aborting due to previous error; 1 warning emitted

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

warning: redundant else block
  --> src/main.rs:11:16
   |
11 |           } else {
   |  ________________^
12 | |             1
13 | |         }
   | |_________^
   |
   = help: remove the `else` block and move the contents out
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_else
note: the lint level is defined here
  --> src/main.rs:1:9
   |
1  | #![warn(clippy::redundant_else)]
   |         ^^^^^^^^^^^^^^^^^^^^^^

warning: this pattern matching can be expressed using equality
 --> src/main.rs:7:16
  |
7 |     let _ = if let Some(1) = foo() {
  |                ^^^^^^^^^^^^^^^^^^^ help: try: `foo() == Some(1)`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#equatable_if_let
  = note: requested on the command line with `-W clippy::equatable-if-let`

warning: `a` (bin "a") generated 2 warnings (run `cargo fix --bin "a"` to apply 1 suggestion)
Version
rustc 1.67.0-nightly (b833ad56f 2022-11-18)
binary: rustc
commit-hash: b833ad56f46a0bbe0e8729512812a161e7dae28a
commit-date: 2022-11-18
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the equatable_if_let lint and the supplied generic foo() reproducer, then run cargo clippy --fix with the shown lint settings. Trace the suggested transformation and add or update a regression test so the fix preserves type inference and the reproducer compiles without the reported E0282 error.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.