Skip to content

#[expect(clippy::non_std_lazy_statics)] does not correctly suppress lint on types #14729

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
pufferfish101007 opened this issue May 3, 2025 · 1 comment · Fixed by #14740
Closed
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@pufferfish101007
Copy link

pufferfish101007 commented May 3, 2025

Summary

When using a Lazy type from another crate (e.g. lazy_regex), the non_std_lazy_statics lint is triggered, but fails to be recognised when it is expected. expecting the lint seems to work fine when using e.g. lazy_static::lazy_static!, but when the offending item is in the type position it doesn't like it.

Clippy version: clippy 0.1.88 (4824c2bb74 2025-05-02)

Reproducer

Take the following code:

#![warn(clippy::non_std_lazy_statics)]

use lazy_regex::{lazy_regex, Lazy};
use regex::Regex;

static REGEX: Lazy<Regex> = lazy_regex!(r#"ab*"#);

pub fn matches() -> bool {
    (*REGEX).is_match("ab")
}

Running cargo clippy gives the following output, which makes sense:

warning: this type has been superseded by `LazyLock` in the standard library
 --> src/lib.rs:6:15
  |
6 | static REGEX: Lazy<Regex> = lazy_regex!(r#"ab*"#);
  |               ^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#non_std_lazy_statics
note: the lint level is defined here
 --> src/lib.rs:1:9
  |
1 | #![warn(clippy::non_std_lazy_statics)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: use `std::sync::LazyLock` instead
  |
6 - static REGEX: Lazy<Regex> = lazy_regex!(r#"ab*"#);
6 + static REGEX: std::sync::LazyLock<Regex> = std::sync::LazyLock::new;
  |

Adding #[expect(clippy::non_std_lazy_statics)]:

New code
#![warn(clippy::non_std_lazy_statics)]

use lazy_regex::{lazy_regex, Lazy};
use regex::Regex;

#[expect(clippy::non_std_lazy_statics)]
static REGEX: Lazy<Regex> = lazy_regex!(r#"ab*"#);

pub fn matches() -> bool {
    (*REGEX).is_match("ab")
}

And cargo clippy now says:

warning: this type has been superseded by `LazyLock` in the standard library
 --> src/lib.rs:7:16
  |
7 | static REGEX:  Lazy<Regex> = lazy_regex!(r#"ab*"#);
  |                ^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#non_std_lazy_statics
note: the lint level is defined here
 --> src/lib.rs:1:9
  |
1 | #![warn(clippy::non_std_lazy_statics)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: use `std::sync::LazyLock` instead
  |
7 - static REGEX:  Lazy<Regex> = lazy_regex!(r#"ab*"#);
7 + static REGEX:  std::sync::LazyLock<Regex> = std::sync::LazyLock::new;
  |

warning: this lint expectation is unfulfilled
 --> src/lib.rs:6:10
  |
6 | #[expect(clippy::non_std_lazy_statics)]
  |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unfulfilled_lint_expectations)]` on by default

So, the #[expect(...)] is not suppressing the lint correctly.

Version

rustc 1.88.0-nightly (4824c2bb7 2025-05-02)
binary: rustc
commit-hash: 4824c2bb7445cb2478aab0190c268c939d77a0f6
commit-date: 2025-05-02
host: x86_64-unknown-linux-gnu
release: 1.88.0-nightly
LLVM version: 20.1.2

Additional Labels

No response

@pufferfish101007 pufferfish101007 added the C-bug Category: Clippy is not doing the correct thing label May 3, 2025
@samueltardieu
Copy link
Contributor

@rustbot claim

github-merge-queue bot pushed a commit that referenced this issue May 14, 2025
When a `non_std_lazy_statics` warning is generated about an item type
which can be replaced by a standard library one, ensure that the lint
happens on the item HIR node so that it can be expected.

changelog: [`non_std_lazy_statics`]: generate the warning onto the right
node

Fixes #14729

Note that this doesn't change anything on lints generated for the
`lazy_static::lazy_static` macro because the `expect` attribute cannot
be applied to a macro.
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants