Skip to content

Commit 6bedaae

Browse files
committed
accept Into<{Sub}DiagMessage> in span_lint functions
1 parent 4a8c949 commit 6bedaae

File tree

2 files changed

+34
-28
lines changed

2 files changed

+34
-28
lines changed

clippy_utils/src/diagnostics.rs

+27-28
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//! Thank you!
99
//! ~The `INTERNAL_METADATA_COLLECTOR` lint
1010
11-
use rustc_errors::{Applicability, Diag, MultiSpan};
11+
use rustc_errors::{Applicability, Diag, DiagMessage, MultiSpan, SubdiagMessage};
1212
use rustc_hir::HirId;
1313
use rustc_lint::{LateContext, Lint, LintContext};
1414
use rustc_span::Span;
@@ -59,9 +59,9 @@ fn docs_link(diag: &mut Diag<'_, ()>, lint: &'static Lint) {
5959
/// 17 | std::mem::forget(seven);
6060
/// | ^^^^^^^^^^^^^^^^^^^^^^^
6161
/// ```
62-
pub fn span_lint<T: LintContext>(cx: &T, lint: &'static Lint, sp: impl Into<MultiSpan>, msg: &str) {
62+
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.to_string(), |diag| {
64+
cx.span_lint(lint, sp, msg.into(), |diag| {
6565
docs_link(diag, lint);
6666
});
6767
}
@@ -104,17 +104,16 @@ pub fn span_lint_and_help<T: LintContext>(
104104
cx: &T,
105105
lint: &'static Lint,
106106
span: impl Into<MultiSpan>,
107-
msg: &str,
107+
msg: impl Into<DiagMessage>,
108108
help_span: Option<Span>,
109-
help: &str,
109+
help: impl Into<SubdiagMessage>,
110110
) {
111111
#[expect(clippy::disallowed_methods)]
112-
cx.span_lint(lint, span, msg.to_string(), |diag| {
113-
let help = help.to_string();
112+
cx.span_lint(lint, span, msg.into(), |diag| {
114113
if let Some(help_span) = help_span {
115-
diag.span_help(help_span, help);
114+
diag.span_help(help_span, help.into());
116115
} else {
117-
diag.help(help);
116+
diag.help(help.into());
118117
}
119118
docs_link(diag, lint);
120119
});
@@ -161,17 +160,16 @@ pub fn span_lint_and_note<T: LintContext>(
161160
cx: &T,
162161
lint: &'static Lint,
163162
span: impl Into<MultiSpan>,
164-
msg: &str,
163+
msg: impl Into<DiagMessage>,
165164
note_span: Option<Span>,
166-
note: &str,
165+
note: impl Into<SubdiagMessage>,
167166
) {
168167
#[expect(clippy::disallowed_methods)]
169-
cx.span_lint(lint, span, msg.to_string(), |diag| {
170-
let note = note.to_string();
168+
cx.span_lint(lint, span, msg.into(), |diag| {
171169
if let Some(note_span) = note_span {
172-
diag.span_note(note_span, note);
170+
diag.span_note(note_span, note.into());
173171
} else {
174-
diag.note(note);
172+
diag.note(note.into());
175173
}
176174
docs_link(diag, lint);
177175
});
@@ -195,14 +193,15 @@ pub fn span_lint_and_note<T: LintContext>(
195193
/// If you're unsure which function you should use, you can test if the `#[allow]` attribute works
196194
/// where you would expect it to.
197195
/// If it doesn't, you likely need to use [`span_lint_hir_and_then`] instead.
198-
pub fn span_lint_and_then<C, S, F>(cx: &C, lint: &'static Lint, sp: S, msg: &str, f: F)
196+
pub fn span_lint_and_then<C, S, M, F>(cx: &C, lint: &'static Lint, sp: S, msg: M, f: F)
199197
where
200198
C: LintContext,
201199
S: Into<MultiSpan>,
200+
M: Into<DiagMessage>,
202201
F: FnOnce(&mut Diag<'_, ()>),
203202
{
204203
#[expect(clippy::disallowed_methods)]
205-
cx.span_lint(lint, sp, msg.to_string(), |diag| {
204+
cx.span_lint(lint, sp, msg, |diag| {
206205
f(diag);
207206
docs_link(diag, lint);
208207
});
@@ -232,9 +231,9 @@ where
232231
/// Instead, use this function and also pass the `HirId` of `<expr_1>`, which will let
233232
/// the compiler check lint level attributes at the place of the expression and
234233
/// the `#[allow]` will work.
235-
pub fn span_lint_hir(cx: &LateContext<'_>, lint: &'static Lint, hir_id: HirId, sp: Span, msg: &str) {
234+
pub fn span_lint_hir(cx: &LateContext<'_>, lint: &'static Lint, hir_id: HirId, sp: Span, msg: impl Into<DiagMessage>) {
236235
#[expect(clippy::disallowed_methods)]
237-
cx.tcx.node_span_lint(lint, hir_id, sp, msg.to_string(), |diag| {
236+
cx.tcx.node_span_lint(lint, hir_id, sp, msg.into(), |diag| {
238237
docs_link(diag, lint);
239238
});
240239
}
@@ -268,11 +267,11 @@ pub fn span_lint_hir_and_then(
268267
lint: &'static Lint,
269268
hir_id: HirId,
270269
sp: impl Into<MultiSpan>,
271-
msg: &str,
270+
msg: impl Into<DiagMessage>,
272271
f: impl FnOnce(&mut Diag<'_, ()>),
273272
) {
274273
#[expect(clippy::disallowed_methods)]
275-
cx.tcx.node_span_lint(lint, hir_id, sp, msg.to_string(), |diag| {
274+
cx.tcx.node_span_lint(lint, hir_id, sp, msg.into(), |diag| {
276275
f(diag);
277276
docs_link(diag, lint);
278277
});
@@ -316,13 +315,13 @@ pub fn span_lint_and_sugg<T: LintContext>(
316315
cx: &T,
317316
lint: &'static Lint,
318317
sp: Span,
319-
msg: &str,
320-
help: &str,
318+
msg: impl Into<DiagMessage>,
319+
help: impl Into<SubdiagMessage>,
321320
sugg: String,
322321
applicability: Applicability,
323322
) {
324-
span_lint_and_then(cx, lint, sp, msg, |diag| {
325-
diag.span_suggestion(sp, help.to_string(), sugg, applicability);
323+
span_lint_and_then(cx, lint, sp, msg.into(), |diag| {
324+
diag.span_suggestion(sp, help.into(), sugg, applicability);
326325
});
327326
}
328327

@@ -332,7 +331,7 @@ pub fn span_lint_and_sugg<T: LintContext>(
332331
/// appear once per
333332
/// replacement. In human-readable format though, it only appears once before
334333
/// the whole suggestion.
335-
pub fn multispan_sugg<I>(diag: &mut Diag<'_, ()>, help_msg: &str, sugg: I)
334+
pub fn multispan_sugg<I>(diag: &mut Diag<'_, ()>, help_msg: impl Into<SubdiagMessage>, sugg: I)
336335
where
337336
I: IntoIterator<Item = (Span, String)>,
338337
{
@@ -346,11 +345,11 @@ where
346345
/// Suggestions with multiple spans will be silently ignored.
347346
pub fn multispan_sugg_with_applicability<I>(
348347
diag: &mut Diag<'_, ()>,
349-
help_msg: &str,
348+
help_msg: impl Into<SubdiagMessage>,
350349
applicability: Applicability,
351350
sugg: I,
352351
) where
353352
I: IntoIterator<Item = (Span, String)>,
354353
{
355-
diag.multipart_suggestion(help_msg.to_string(), sugg.into_iter().collect(), applicability);
354+
diag.multipart_suggestion(help_msg.into(), sugg.into_iter().collect(), applicability);
356355
}

clippy_utils/src/sugg.rs

+7
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,13 @@ impl<'a> Sugg<'a> {
358358
},
359359
}
360360
}
361+
362+
pub fn into_string(self) -> String {
363+
match self {
364+
Sugg::NonParen(p) | Sugg::MaybeParen(p) => p.into_owned(),
365+
Sugg::BinOp(b, l, r) => binop_to_string(b, &l, &r),
366+
}
367+
}
361368
}
362369

363370
/// Generates a string from the operator and both sides.

0 commit comments

Comments
 (0)