Skip to content

clippy::question_mark suggests code that triggers "type annotations needed" #16184

@ChayimFriedman2

Description

@ChayimFriedman2

Summary

Sometimes a manual impl of the ? operator is needed, because using it will cause "type annotations needed" due to the From::from() function it calls on the error type.

Lint Name

question_mark

Reproducer

I tried this code:

fn main() {
    _ = (|| {
        if let Err(err) = Err::<(), _>(()) {
            return Err(err);
        }
        Ok(())
    })();
}

I saw this happen:

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

I expected to see this happen:

No warning. Replacing with Clippy's suggestion leads to a "type annotations needed" error.

Version


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 have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions