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

allow_attributes and allow_attributes_without_reason triggering on item with "allow" in name #13463

Closed
ethanoroshiba opened this issue Sep 26, 2024 · 2 comments
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

@ethanoroshiba
Copy link

Summary

Clippy gives warnings for allow_attributes and allow_attributes_without_reason when some objects have "allow" in the name. This only seems to happen, however, when the object in question is followed by "?".

Lint Name

allow_attributes, allow_attributes_without_reason

Reproducer

When running cargo clippy -- --warn clippy::allow_attributes --warn clippy::allow_attributes_without_reason (on Mac) with this code:

fn main() {
    let _ = this_produces_a_clippy_warning();
    let _ = this_does_not_produce_a_clippy_warning();
}

fn this_produces_a_clippy_warning() -> anyhow::Result<Vec<String>> {
    let allowed_things: Result<Vec<String>, anyhow::Error> = Ok(vec!["foo".to_string(), "bar".to_string()]);
    let allowed_things = allowed_things?;
    Ok(allowed_things)
}

#[expect(clippy::unnecessary_literal_unwrap, reason = "for demonstration")]
fn this_does_not_produce_a_clippy_warning() -> anyhow::Result<Vec<String>> {
    let allowed_things: Result<Vec<String>, anyhow::Error> = Ok(vec!["foo".to_string(), "bar".to_string()]);
    let allowed_things = allowed_things.unwrap();
    Ok(allowed_things)
}

It results in these incorrect warnings:

Checking clippy_error v0.1.0 (/Users/eoroshiba/Documents/random/clippy_error)
warning: #[allow] attribute found
 --> src/main.rs:8:26
  |
8 |     let allowed_things = allowed_things?;
  |                          ^^^^^^^^^^^^^^^ help: replace it with: `expect`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#allow_attributes
  = note: requested on the command line with `-W clippy::allow-attributes`

warning: `allow` attribute without specifying a reason
 --> src/main.rs:8:26
  |
8 |     let allowed_things = allowed_things?;
  |                          ^^^^^^^^^^^^^^^
  |
  = help: try adding a reason at the end with `, reason = ".."`
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#allow_attributes_without_reason
  = note: requested on the command line with `-W clippy::allow-attributes-without-reason`

warning: `clippy_error` (bin "clippy_error") generated 2 warnings (run `cargo clippy --fix --bin "clippy_error"` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.05s

Note that the only difference is propagation with unwrap() versus ? that changes the warnings.

Version

rustc 1.81.0 (eeb90cda1 2024-09-04)
binary: rustc
commit-hash: eeb90cda1969383f56a2637cbd3037bdf598841c
commit-date: 2024-09-04
host: aarch64-apple-darwin
release: 1.81.0
LLVM version: 18.1.7

Additional Labels

No response

@ethanoroshiba ethanoroshiba 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 26, 2024
@smoelius
Copy link
Contributor

Duplicate? #13380

@y21
Copy link
Member

y21 commented Sep 26, 2024

This no longer reproduces on newer nightlies, so closing as fixed (the PR that closed the issue that @smoelius linked to also added a regression test for this)

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

No branches or pull requests

3 participants