Skip to content

question_mark suggests using ? in a method which doesn't return result #8019

Closed
@ChrisJefferson

Description

@ChrisJefferson

I tried this code on the latest nightly (reduced from large program):

pub enum SolutionFound {
    None,
    First,
    AfterFirst,
}


fn obj(_: i32) -> Result<(), SolutionFound> {
    Err(SolutionFound::First)
}

fn f() -> SolutionFound {
    if obj(2).is_err() {
        return SolutionFound::None;
    }
    SolutionFound::First
}

fn main() {
    let _ = f();
    
}

I expected to see this happen: No suggestion

Instead, this happened:

warning: this block may be rewritten with the `?` operator
  --> src/main.rs:13:5
   |
13 | /     if obj(2).is_err() {
14 | |         return SolutionFound::None;
15 | |     }
   | |_____^ help: replace it with: `obj(2)?;`
   |
   = note: `#[warn(clippy::question_mark)]` on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#question_mark

Meta

Rust version (rustc -Vv):

I run through cargo + rustup. cargo +nightly version is the following (I rustup updated just now)

cargo 1.58.0-nightly (ad50d0d26 2021-11-17)

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thing

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions