Skip to content

Commit 373df52

Browse files
authored
Post non_std_lazy_statics type warnings onto the right node (#14740)
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.
2 parents 17f2a87 + cc5d1a5 commit 373df52

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

clippy_lints/src/non_std_lazy_statics.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_config::Conf;
2-
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
2+
use clippy_utils::diagnostics::{span_lint, span_lint_hir_and_then};
33
use clippy_utils::msrvs::{self, Msrv};
44
use clippy_utils::paths::{self, PathNS, find_crates, lookup_path_str};
55
use clippy_utils::visitors::for_each_expr;
@@ -8,7 +8,7 @@ use rustc_data_structures::fx::FxIndexMap;
88
use rustc_errors::Applicability;
99
use rustc_hir::def::{DefKind, Res};
1010
use rustc_hir::def_id::{CrateNum, DefId};
11-
use rustc_hir::{self as hir, BodyId, Expr, ExprKind, Item, ItemKind};
11+
use rustc_hir::{self as hir, BodyId, Expr, ExprKind, HirId, Item, ItemKind};
1212
use rustc_lint::{LateContext, LateLintPass, LintContext};
1313
use rustc_session::impl_lint_pass;
1414
use rustc_span::Span;
@@ -173,6 +173,8 @@ struct LazyInfo {
173173
/// // ^^^^
174174
/// ```
175175
ty_span_no_args: Span,
176+
/// Item on which the lint must be generated.
177+
item_hir_id: HirId,
176178
/// `Span` and `DefId` of calls on `Lazy` type.
177179
/// i.e.:
178180
/// ```ignore
@@ -206,6 +208,7 @@ impl LazyInfo {
206208

207209
Some(LazyInfo {
208210
ty_span_no_args,
211+
item_hir_id: item.hir_id(),
209212
calls_span_and_id: new_fn_calls,
210213
})
211214
} else {
@@ -229,9 +232,10 @@ impl LazyInfo {
229232
}
230233
}
231234

232-
span_lint_and_then(
235+
span_lint_hir_and_then(
233236
cx,
234237
NON_STD_LAZY_STATICS,
238+
self.item_hir_id,
235239
self.ty_span_no_args,
236240
"this type has been superseded by `LazyLock` in the standard library",
237241
|diag| {

tests/ui/non_std_lazy_static/non_std_lazy_static_fixable.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,10 @@ mod external_macros {
7070
once_cell::external!();
7171
lazy_static::external!();
7272
}
73+
74+
mod issue14729 {
75+
use once_cell::sync::Lazy;
76+
77+
#[expect(clippy::non_std_lazy_statics)]
78+
static LAZY_FOO: Lazy<String> = Lazy::new(|| "foo".to_uppercase());
79+
}

tests/ui/non_std_lazy_static/non_std_lazy_static_fixable.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,10 @@ mod external_macros {
7070
once_cell::external!();
7171
lazy_static::external!();
7272
}
73+
74+
mod issue14729 {
75+
use once_cell::sync::Lazy;
76+
77+
#[expect(clippy::non_std_lazy_statics)]
78+
static LAZY_FOO: Lazy<String> = Lazy::new(|| "foo".to_uppercase());
79+
}

0 commit comments

Comments
 (0)