Skip to content

Commit 53f1e6b

Browse files
committed
Use Cow in {D,Subd}iagnosticMessage.
Each of `{D,Subd}iagnosticMessage::{Str,Eager}` has a comment: ``` // FIXME(davidtwco): can a `Cow<'static, str>` be used here? ``` This commit answers that question in the affirmative. It's not the most compelling change ever, but it might be worth merging. This requires changing the `impl<'a> From<&'a str>` impls to `impl From<&'static str>`, which involves a bunch of knock-on changes that require/result in call sites being a little more precise about exactly what kind of string they use to create errors, and not just `&str`. This will result in fewer unnecessary allocations, though this will not have any notable perf effects given that these are error paths. Note that I was lazy within Clippy, using `to_string` in a few places to preserve the existing string imprecision. I could have used `impl Into<{D,Subd}iagnosticMessage>` in various places as is done in the compiler, but that would have required changes to *many* call sites (mostly changing `&format("...")` to `format!("...")`) which didn't seem worthwhile.
1 parent a37852e commit 53f1e6b

File tree

5 files changed

+21
-22
lines changed

5 files changed

+21
-22
lines changed

clippy_lints/src/missing_const_for_fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
154154

155155
if let Err((span, err)) = is_min_const_fn(cx.tcx, mir, &self.msrv) {
156156
if cx.tcx.is_const_fn_raw(def_id.to_def_id()) {
157-
cx.tcx.sess.span_err(span, err.as_ref());
157+
cx.tcx.sess.span_err(span, err);
158158
}
159159
} else {
160160
span_lint(cx, MISSING_CONST_FOR_FN, span, "this could be a `const fn`");

clippy_lints/src/needless_pass_by_value.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use rustc_span::{sym, Span};
2626
use rustc_target::spec::abi::Abi;
2727
use rustc_trait_selection::traits;
2828
use rustc_trait_selection::traits::misc::type_allowed_to_implement_copy;
29-
use std::borrow::Cow;
3029

3130
declare_clippy_lint! {
3231
/// ### What it does
@@ -240,9 +239,8 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
240239
snippet_opt(cx, span)
241240
.map_or(
242241
"change the call to".into(),
243-
|x| Cow::from(format!("change `{x}` to")),
244-
)
245-
.as_ref(),
242+
|x| format!("change `{x}` to"),
243+
),
246244
suggestion,
247245
Applicability::Unspecified,
248246
);
@@ -270,9 +268,8 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
270268
snippet_opt(cx, span)
271269
.map_or(
272270
"change the call to".into(),
273-
|x| Cow::from(format!("change `{x}` to"))
274-
)
275-
.as_ref(),
271+
|x| format!("change `{x}` to")
272+
),
276273
suggestion,
277274
Applicability::Unspecified,
278275
);

clippy_lints/src/unnecessary_wraps.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessaryWraps {
163163
span_lint_and_then(cx, UNNECESSARY_WRAPS, span, lint_msg.as_str(), |diag| {
164164
diag.span_suggestion(
165165
fn_decl.output.span(),
166-
return_type_sugg_msg.as_str(),
166+
return_type_sugg_msg,
167167
return_type_sugg,
168168
Applicability::MaybeIncorrect,
169169
);

clippy_utils/src/diagnostics.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fn docs_link(diag: &mut Diagnostic, lint: &'static Lint) {
4646
/// | ^^^^^^^^^^^^^^^^^^^^^^^
4747
/// ```
4848
pub fn span_lint<T: LintContext>(cx: &T, lint: &'static Lint, sp: impl Into<MultiSpan>, msg: &str) {
49-
cx.struct_span_lint(lint, sp, msg, |diag| {
49+
cx.struct_span_lint(lint, sp, msg.to_string(), |diag| {
5050
docs_link(diag, lint);
5151
diag
5252
});
@@ -80,11 +80,12 @@ pub fn span_lint_and_help<T: LintContext>(
8080
help_span: Option<Span>,
8181
help: &str,
8282
) {
83-
cx.struct_span_lint(lint, span, msg, |diag| {
83+
cx.struct_span_lint(lint, span, msg.to_string(), |diag| {
84+
let help = help.to_string();
8485
if let Some(help_span) = help_span {
85-
diag.span_help(help_span, help);
86+
diag.span_help(help_span, help.to_string());
8687
} else {
87-
diag.help(help);
88+
diag.help(help.to_string());
8889
}
8990
docs_link(diag, lint);
9091
diag
@@ -122,7 +123,8 @@ pub fn span_lint_and_note<T: LintContext>(
122123
note_span: Option<Span>,
123124
note: &str,
124125
) {
125-
cx.struct_span_lint(lint, span, msg, |diag| {
126+
cx.struct_span_lint(lint, span, msg.to_string(), |diag| {
127+
let note = note.to_string();
126128
if let Some(note_span) = note_span {
127129
diag.span_note(note_span, note);
128130
} else {
@@ -143,15 +145,15 @@ where
143145
S: Into<MultiSpan>,
144146
F: FnOnce(&mut Diagnostic),
145147
{
146-
cx.struct_span_lint(lint, sp, msg, |diag| {
148+
cx.struct_span_lint(lint, sp, msg.to_string(), |diag| {
147149
f(diag);
148150
docs_link(diag, lint);
149151
diag
150152
});
151153
}
152154

153155
pub fn span_lint_hir(cx: &LateContext<'_>, lint: &'static Lint, hir_id: HirId, sp: Span, msg: &str) {
154-
cx.tcx.struct_span_lint_hir(lint, hir_id, sp, msg, |diag| {
156+
cx.tcx.struct_span_lint_hir(lint, hir_id, sp, msg.to_string(), |diag| {
155157
docs_link(diag, lint);
156158
diag
157159
});
@@ -165,7 +167,7 @@ pub fn span_lint_hir_and_then(
165167
msg: &str,
166168
f: impl FnOnce(&mut Diagnostic),
167169
) {
168-
cx.tcx.struct_span_lint_hir(lint, hir_id, sp, msg, |diag| {
170+
cx.tcx.struct_span_lint_hir(lint, hir_id, sp, msg.to_string(), |diag| {
169171
f(diag);
170172
docs_link(diag, lint);
171173
diag
@@ -202,7 +204,7 @@ pub fn span_lint_and_sugg<T: LintContext>(
202204
applicability: Applicability,
203205
) {
204206
span_lint_and_then(cx, lint, sp, msg, |diag| {
205-
diag.span_suggestion(sp, help, sugg, applicability);
207+
diag.span_suggestion(sp, help.to_string(), sugg, applicability);
206208
});
207209
}
208210

@@ -232,5 +234,5 @@ pub fn multispan_sugg_with_applicability<I>(
232234
) where
233235
I: IntoIterator<Item = (Span, String)>,
234236
{
235-
diag.multipart_suggestion(help_msg, sugg.into_iter().collect(), applicability);
237+
diag.multipart_suggestion(help_msg.to_string(), sugg.into_iter().collect(), applicability);
236238
}

clippy_utils/src/sugg.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ impl<T: LintContext> DiagnosticExt<T> for rustc_errors::Diagnostic {
741741
if let Some(indent) = indentation(cx, item) {
742742
let span = item.with_hi(item.lo());
743743

744-
self.span_suggestion(span, msg, format!("{attr}\n{indent}"), applicability);
744+
self.span_suggestion(span, msg.to_string(), format!("{attr}\n{indent}"), applicability);
745745
}
746746
}
747747

@@ -762,7 +762,7 @@ impl<T: LintContext> DiagnosticExt<T> for rustc_errors::Diagnostic {
762762
})
763763
.collect::<String>();
764764

765-
self.span_suggestion(span, msg, format!("{new_item}\n{indent}"), applicability);
765+
self.span_suggestion(span, msg.to_string(), format!("{new_item}\n{indent}"), applicability);
766766
}
767767
}
768768

@@ -779,7 +779,7 @@ impl<T: LintContext> DiagnosticExt<T> for rustc_errors::Diagnostic {
779779
}
780780
}
781781

782-
self.span_suggestion(remove_span, msg, "", applicability);
782+
self.span_suggestion(remove_span, msg.to_string(), "", applicability);
783783
}
784784
}
785785

0 commit comments

Comments
 (0)