Skip to content

needless_question_mark not ignored properly when implicitly returned #8392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
trinity-1686a opened this issue Feb 3, 2022 · 1 comment · Fixed by #14682
Closed

needless_question_mark not ignored properly when implicitly returned #8392

trinity-1686a opened this issue Feb 3, 2022 · 1 comment · Fixed by #14682
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@trinity-1686a
Copy link

trinity-1686a commented Feb 3, 2022

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

@trinity-1686a trinity-1686a added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Feb 3, 2022
@giraffate
Copy link
Contributor

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.

github-merge-queue bot pushed a commit that referenced this issue Apr 23, 2025
Fixes #14675 by making it
clearer that the constructor needs to be removed as well
Also fixes #8392

changelog: none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants