Skip to content

Commit 4dd07f4

Browse files
committed
Auto merge of rust-lang#125410 - fmease:adj-lint-diag-api, r=nnethercote
[perf] Delay the construction of early lint diag structs Attacks some of the perf regressions from rust-lang#124417 (comment). See individual commits for details. The first three commits are not strictly necessary. However, the 2nd one (06bc4fc, *Remove `LintDiagnostic::msg`*) makes the main change way nicer to implement. It's also pretty sweet on its own if I may say so myself.
2 parents 8f23de1 + 0c653d9 commit 4dd07f4

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

clippy_utils/src/diagnostics.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ fn docs_link(diag: &mut Diag<'_, ()>, lint: &'static Lint) {
6161
/// ```
6262
pub fn span_lint<T: LintContext>(cx: &T, lint: &'static Lint, sp: impl Into<MultiSpan>, msg: impl Into<DiagMessage>) {
6363
#[expect(clippy::disallowed_methods)]
64-
cx.span_lint(lint, sp, msg.into(), |diag| {
64+
cx.span_lint(lint, sp, |diag| {
65+
diag.primary_message(msg);
6566
docs_link(diag, lint);
6667
});
6768
}
@@ -109,7 +110,8 @@ pub fn span_lint_and_help<T: LintContext>(
109110
help: impl Into<SubdiagMessage>,
110111
) {
111112
#[expect(clippy::disallowed_methods)]
112-
cx.span_lint(lint, span, msg.into(), |diag| {
113+
cx.span_lint(lint, span, |diag| {
114+
diag.primary_message(msg);
113115
if let Some(help_span) = help_span {
114116
diag.span_help(help_span, help.into());
115117
} else {
@@ -165,7 +167,8 @@ pub fn span_lint_and_note<T: LintContext>(
165167
note: impl Into<SubdiagMessage>,
166168
) {
167169
#[expect(clippy::disallowed_methods)]
168-
cx.span_lint(lint, span, msg.into(), |diag| {
170+
cx.span_lint(lint, span, |diag| {
171+
diag.primary_message(msg);
169172
if let Some(note_span) = note_span {
170173
diag.span_note(note_span, note.into());
171174
} else {
@@ -201,7 +204,8 @@ where
201204
F: FnOnce(&mut Diag<'_, ()>),
202205
{
203206
#[expect(clippy::disallowed_methods)]
204-
cx.span_lint(lint, sp, msg, |diag| {
207+
cx.span_lint(lint, sp, |diag| {
208+
diag.primary_message(msg);
205209
f(diag);
206210
docs_link(diag, lint);
207211
});
@@ -233,7 +237,8 @@ where
233237
/// the `#[allow]` will work.
234238
pub fn span_lint_hir(cx: &LateContext<'_>, lint: &'static Lint, hir_id: HirId, sp: Span, msg: impl Into<DiagMessage>) {
235239
#[expect(clippy::disallowed_methods)]
236-
cx.tcx.node_span_lint(lint, hir_id, sp, msg.into(), |diag| {
240+
cx.tcx.node_span_lint(lint, hir_id, sp, |diag| {
241+
diag.primary_message(msg);
237242
docs_link(diag, lint);
238243
});
239244
}
@@ -271,7 +276,8 @@ pub fn span_lint_hir_and_then(
271276
f: impl FnOnce(&mut Diag<'_, ()>),
272277
) {
273278
#[expect(clippy::disallowed_methods)]
274-
cx.tcx.node_span_lint(lint, hir_id, sp, msg.into(), |diag| {
279+
cx.tcx.node_span_lint(lint, hir_id, sp, |diag| {
280+
diag.primary_message(msg);
275281
f(diag);
276282
docs_link(diag, lint);
277283
});

tests/ui-internal/disallow_span_lint.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ use rustc_lint::{Lint, LintContext};
1111
use rustc_middle::ty::TyCtxt;
1212

1313
pub fn a(cx: impl LintContext, lint: &'static Lint, span: impl Into<MultiSpan>, msg: impl Into<DiagMessage>) {
14-
cx.span_lint(lint, span, msg, |_| {});
14+
cx.span_lint(lint, span, |lint| { lint.primary_message(msg); });
1515
}
1616

1717
pub fn b(tcx: TyCtxt<'_>, lint: &'static Lint, hir_id: HirId, span: impl Into<MultiSpan>, msg: impl Into<DiagMessage>) {
18-
tcx.node_span_lint(lint, hir_id, span, msg, |_| {});
18+
tcx.node_span_lint(lint, hir_id, span, |lint| { lint.primary_message(msg); });
1919
}
2020

2121
fn main() {}

tests/ui-internal/disallow_span_lint.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: use of a disallowed method `rustc_lint::context::LintContext::span_lint`
22
--> tests/ui-internal/disallow_span_lint.rs:14:5
33
|
4-
LL | cx.span_lint(lint, span, msg, |_| {});
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4+
LL | cx.span_lint(lint, span, |lint| { lint.primary_message(msg); });
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: this function does not add a link to our documentation, please use the `clippy_utils::diagnostics::span_lint*` functions instead (from clippy.toml)
88
= note: `-D clippy::disallowed-methods` implied by `-D warnings`
@@ -11,8 +11,8 @@ LL | cx.span_lint(lint, span, msg, |_| {});
1111
error: use of a disallowed method `rustc_middle::ty::context::TyCtxt::node_span_lint`
1212
--> tests/ui-internal/disallow_span_lint.rs:18:5
1313
|
14-
LL | tcx.node_span_lint(lint, hir_id, span, msg, |_| {});
15-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
LL | tcx.node_span_lint(lint, hir_id, span, |lint| { lint.primary_message(msg); });
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1616
|
1717
= note: this function does not add a link to our documentation, please use the `clippy_utils::diagnostics::span_lint_hir*` functions instead (from clippy.toml)
1818

0 commit comments

Comments
 (0)