Skip to content

Commit 431cce1

Browse files
committed
Restrict From<S> for {D,Subd}iagnosticMessage.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static, str>`, which are reasonable. It also includes `&String`, which is pretty weird, and results in many places making unnecessary allocations for patterns like this: ``` self.fatal(&format!(...)) ``` This creates a string with `format!`, takes a reference, passes the reference to `fatal`, which does an `into()`, which clones the reference, doing a second allocation. Two allocations for a single string, bleh. This commit changes the `From` impls so that you can only create a `{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static, str>`. This requires changing all the places that currently create one from a `&String`. Most of these are of the `&format!(...)` form described above; each one removes an unnecessary static `&`, plus an allocation when executed. There are also a few places where the existing use of `&String` was more reasonable; these now just use `clone()` at the call site. As well as making the code nicer and more efficient, this is a step towards possibly using `Cow<'static, str>` in `{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing the `From<&'a str>` impls to `From<&'static str>`, which is doable, but I'm not yet sure if it's worthwhile.
1 parent d36bde7 commit 431cce1

File tree

5 files changed

+8
-8
lines changed

5 files changed

+8
-8
lines changed

clippy_lints/src/future_not_send.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl<'tcx> LateLintPass<'tcx> for FutureNotSend {
9696
if let PredicateKind::Clause(Clause::Trait(trait_pred)) =
9797
obligation.predicate.kind().skip_binder()
9898
{
99-
db.note(&format!(
99+
db.note(format!(
100100
"`{}` doesn't implement `{}`",
101101
trait_pred.self_ty(),
102102
trait_pred.trait_ref.print_only_trait_path(),

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ pub fn register_pre_expansion_lints(store: &mut rustc_lint::LintStore, sess: &Se
353353
pub fn read_conf(sess: &Session, path: &io::Result<(Option<PathBuf>, Vec<String>)>) -> Conf {
354354
if let Ok((_, warnings)) = path {
355355
for warning in warnings {
356-
sess.warn(warning);
356+
sess.warn(warning.clone());
357357
}
358358
}
359359
let file_name = match path {

clippy_lints/src/methods/str_splitn.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,13 @@ fn check_manual_split_once_indirect(
175175
let remove_msg = format!("remove the `{iter_ident}` usages");
176176
diag.span_suggestion(
177177
first.span,
178-
&remove_msg,
178+
remove_msg.clone(),
179179
"",
180180
app,
181181
);
182182
diag.span_suggestion(
183183
second.span,
184-
&remove_msg,
184+
remove_msg,
185185
"",
186186
app,
187187
);

clippy_lints/src/non_send_fields_in_send_ty.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,13 @@ impl<'tcx> LateLintPass<'tcx> for NonSendFieldInSendTy {
131131
for field in non_send_fields {
132132
diag.span_note(
133133
field.def.span,
134-
&format!("it is not safe to send field `{}` to another thread", field.def.ident.name),
134+
format!("it is not safe to send field `{}` to another thread", field.def.ident.name),
135135
);
136136

137137
match field.generic_params.len() {
138138
0 => diag.help("use a thread-safe type that implements `Send`"),
139-
1 if is_ty_param(field.ty) => diag.help(&format!("add `{}: Send` bound in `Send` impl", field.ty)),
140-
_ => diag.help(&format!(
139+
1 if is_ty_param(field.ty) => diag.help(format!("add `{}: Send` bound in `Send` impl", field.ty)),
140+
_ => diag.help(format!(
141141
"add bounds on type parameter{} `{}` that satisfy `{}: Send`",
142142
if field.generic_params.len() > 1 { "s" } else { "" },
143143
field.generic_params_string(),

clippy_utils/src/attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ pub fn get_unique_attr<'a>(
133133
let mut unique_attr: Option<&ast::Attribute> = None;
134134
for attr in get_attr(sess, attrs, name) {
135135
if let Some(duplicate) = unique_attr {
136-
sess.struct_span_err(attr.span, &format!("`{name}` is defined multiple times"))
136+
sess.struct_span_err(attr.span, format!("`{name}` is defined multiple times"))
137137
.span_note(duplicate.span, "first definition found here")
138138
.emit();
139139
} else {

0 commit comments

Comments
 (0)