rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
11.25k stars 1.52k forks source link

needless_question_mark not ignored properly when implicitly returned #8392

Open trinity-1686a opened 2 years ago

trinity-1686a commented 2 years ago

Summary

#[allow(clippy::needless_question_mark)] does not seems to prevent the lint from firing when it's applied on the last statement of a block, and is implicitly returned. It's properly taken into account when using #![allow(clippy::needless_question_mark)] on the whole block, or when assigning let _ = Some(a?);.

Lint Name

needless_question_mark

Reproducer

I tried this code:

#![deny(clippy::all)]

fn some_function() -> Option<()> {
    let a = Some(());
    #[allow(clippy::needless_question_mark)]
    Some(a?)
}

I saw this happen:

error: question mark operator is useless here
 --> src/lib.rs:6:5
  |
6 |     Some(a?)
  |     ^^^^^^^^ help: try removing question mark and `Some()`: `a`
  |
note: the lint level is defined here
 --> src/lib.rs:1:9
  |
1 | #![deny(clippy::all)]
  |         ^^^^^^^^^^^
  = note: `#[deny(clippy::needless_question_mark)]` implied by `#[deny(clippy::all)]`
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_question_mark

I expected to see this happen: No error since this lint is disabled.

Version

rustc 1.60.0-nightly (27f5d830e 2022-02-02)
binary: rustc
commit-hash: 27f5d830eb11cd7bdc834d6f0d78120976f75443
commit-date: 2022-02-02
host: x86_64-unknown-linux-gnu
release: 1.60.0-nightly
LLVM version: 13.0.0

Additional Labels

No response

giraffate commented 2 years ago

In this case body seems to be checked when triggered, so it looks like that it will be difficult to fix this. It would be put #[allow(clippy::needless_question_mark)] on the block.