Skip to content

false positive for clippy::question_mark #9347

@conradludgate

Description

@conradludgate

Summary

Clippy wrongly suggests I can use the question mark operator when returning an error which matches a deep pattern.

The idea is that I'm manually matching an error and then returning, but my match is more specific than what ? does, so it's a false positive

Lint Name

question_mark

Reproducer

I tried this code:

pub struct Error {
    flag: bool,
}

fn foo() -> Result<(), Error> {
    let res = Ok(());
    
    if let Err(err @ Error { flag: true }) = res {
        return Err(err);
    }
    
    res
}

I saw this happen:

warning: this block may be rewritten with the `?` operator
  --> src/lib.rs:10:5
   |
10 | /     if let Err(err @ Error { flag: true }) = res {
11 | |         return Err(err);
12 | |     }
   | |_____^ help: replace it with: `res?;`
   |
   = note: `#[warn(clippy::question_mark)]` on by default

I expected to see this happen:

No warning

Version

rustc 1.65.0-nightly (29e4a9ee0 2022-08-10)
binary: rustc
commit-hash: 29e4a9ee0253cd39e552a77f51f11f9a5f1c41e6
commit-date: 2022-08-10
host: aarch64-apple-darwin
release: 1.65.0-nightly
LLVM version: 14.0.6

Additional Labels

No response

Activity

added
C-bugCategory: Clippy is not doing the correct thing
I-false-positiveIssue: The lint was triggered on code it shouldn't have
on Aug 18, 2022
added a commit that references this issue on Aug 18, 2022
e87a5a1
added 2 commits that reference this issue on Aug 18, 2022
81296cf
eeaaba3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      Participants

      @conradludgate

      Issue actions

        false positive for clippy::question_mark · Issue #9347 · rust-lang/rust-clippy