rust-lang / rust-lang/rust-clippy

clippy::question_mark suggestion makes closure return type ambiguous

Open
#11,275 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

When if let Err(e) = f() { return Err(e) } is used to make the return type of a closure unambiguous, clippy lints this with clippy::question_mark. Applying the suggestion with --fix makes the type ambiguous and causes a compile error.

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

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

  * a/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-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[E0282]: type annotations needed for `std::result::Result<i32, E>`
 --> a/src/main.rs:6:9
  |
6 |     let g = (|| {
  |         ^
  |
help: consider giving `g` an explicit type, where the type for type parameter `E` is specified
  |
6 |     let g: std::result::Result<i32, E> = (|| {
  |          +++++++++++++++++++++++++++++

error: aborting due to previous error

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

warning: this block may be rewritten with the `?` operator
  --> a/src/main.rs:8:9
   |
8  | /         if let Err(e) = f() {
9  | |             return Err(e);
10 | |         }
   | |_________^ help: replace it with: `f()?;`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#question_mark
   = note: `#[warn(clippy::question_mark)]` on by default

warning: `a` (bin "a") generated 1 warning (run `cargo clippy --fix --bin "a"` to apply 1 suggestion)
warning: `a` (bin "a" test) generated 1 warning (1 duplicate)
    Finished dev [unoptimized + debuginfo] target(s) in 3.58s
Lint Name

clippy::question_mark

Reproducer
fn f() -> Result<u8, u8> {
    Ok(9)
}

fn main() {
    let g = (|| {
        f()?;
        if let Err(e) = f() {
            return Err(e);
        }
        Ok(42)
    })();
    println!("{g:?}");
}
Version
rustc 1.73.0-nightly (db7ff98a7 2023-07-31)
binary: rustc
commit-hash: db7ff98a72f3e742b641f9cb16d0e8c285e87e9b
commit-date: 2023-07-31
host: x86_64-unknown-linux-gnu
release: 1.73.0-nightly
LLVM version: 16.0.5
Additional Labels

@rustbot label I-suggestion-causes-error

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 by reproducing the provided closure example and inspecting the implementation of clippy::question_mark. Verify the suggested replacement with --fix, then add or update coverage so the closure's return type remains unambiguous and the fixed example compiles successfully.

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
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.