Skip to content
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

Specifying reason in expect(clippy::needless_return) triggers false positive "this lint expectation is unfulfilled" #13366

Open
schuetzm opened this issue Sep 7, 2024 · 2 comments · May be fixed by #13393
Assignees
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

@schuetzm
Copy link

schuetzm commented Sep 7, 2024

Summary

It seems the reason option has an effect on whether the lint warns about an unfulfilled expectation.

Lint Name

clippy::needless_return

Reproducer

I tried this code:

fn a(x: Option<u8>) -> Option<u8> {
    match x {
        Some(_) => None,
        None => {
            #[expect(clippy::needless_return, reason = "Use early return for errors.")]
            return None;
        }
    }
}

fn b(x: Option<u8>) -> Option<u8> {
    match x {
        Some(_) => None,
        None => {
            #[expect(clippy::needless_return)]
            return None;
        }
    }
}

I saw this happen:
The warning about unfulfilled expectations is printed for function a, but not for b:

  |
5 |             #[expect(clippy::needless_return, reason = "Use early return for errors.")]
  |                      ^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: Use early return for errors.
  = note: `#[warn(unfulfilled_lint_expectations)]` on by default

I expected to see this happen:
No warning should be printed for either function.

Version

rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: x86_64-unknown-linux-gnu
release: 1.81.0
LLVM version: 18.1.7

Additional Labels

No response

@schuetzm schuetzm 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 Sep 7, 2024
@vHugoObject
Copy link
Contributor

vHugoObject commented Sep 10, 2024

@rustbot claim

@Jarcho
Copy link
Contributor

Jarcho commented Sep 11, 2024

This lint has a hack to make#[expect(..)] work since normally it doesn't trigger when an attribute is present on the statement. It needs to be extended to ignore the reason field.

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
3 participants