Skip to content

Commit 062822c

Browse files
committed
Auto merge of #17641 - nyurik:optimize-refs, r=Veykril
Avoid ref when using format! in compiler Clean up a few minor refs in `format!` macro, as it has a performance cost. Apparently the compiler is unable to inline `format!("{}", &variable)`, and does a run-time double-reference instead (format macro already does one level referencing). Inlining format args prevents accidental `&` misuse. See rust-lang/rust-clippy#10851
2 parents c847bf7 + 3092e1c commit 062822c

File tree

7 files changed

+12
-12
lines changed

7 files changed

+12
-12
lines changed

crates/ide-assists/src/handlers/bind_unused_param.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub(crate) fn bind_unused_param(acc: &mut Assists, ctx: &AssistContext<'_>) -> O
4343

4444
acc.add(
4545
AssistId("bind_unused_param", AssistKind::QuickFix),
46-
&format!("Bind as `let _ = {};`", &ident_pat),
46+
&format!("Bind as `let _ = {ident_pat};`"),
4747
param.syntax().text_range(),
4848
|builder| {
4949
let line_index = ctx.db().line_index(ctx.file_id().into());

crates/ide-completion/src/item.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ mod tests {
664664
/// If provided vec![vec![a], vec![b, c], vec![d]], then this will assert:
665665
/// a.score < b.score == c.score < d.score
666666
fn check_relevance_score_ordered(expected_relevance_order: Vec<Vec<CompletionRelevance>>) {
667-
let expected = format!("{:#?}", &expected_relevance_order);
667+
let expected = format!("{expected_relevance_order:#?}");
668668

669669
let actual_relevance_order = expected_relevance_order
670670
.into_iter()
@@ -685,7 +685,7 @@ mod tests {
685685
)
686686
.1;
687687

688-
let actual = format!("{:#?}", &actual_relevance_order);
688+
let actual = format!("{actual_relevance_order:#?}");
689689

690690
assert_eq_text!(&expected, &actual);
691691
}

crates/ide-diagnostics/src/handlers/typed_hole.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::TypedHole) -> Option<Vec<Assist>
7575
.unique()
7676
.map(|code| Assist {
7777
id: AssistId("typed-hole", AssistKind::QuickFix),
78-
label: Label::new(format!("Replace `_` with `{}`", &code)),
78+
label: Label::new(format!("Replace `_` with `{code}`")),
7979
group: Some(GroupLabel("Replace `_` with a term".to_owned())),
8080
target: original_range.range,
8181
source_change: Some(SourceChange::from_text_edit(

crates/rust-analyzer/src/cli/analysis_stats.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ impl flags::AnalysisStats {
422422
if found_terms.is_empty() {
423423
acc.tail_expr_no_term += 1;
424424
acc.total_tail_exprs += 1;
425-
// println!("\n{}\n", &original_text);
425+
// println!("\n{original_text}\n");
426426
continue;
427427
};
428428

crates/rust-analyzer/src/lsp/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ impl GlobalState {
7979
pub(crate) fn show_and_log_error(&mut self, message: String, additional_info: Option<String>) {
8080
match additional_info {
8181
Some(additional_info) => {
82-
tracing::error!("{}:\n{}", &message, &additional_info);
82+
tracing::error!("{message}:\n{additional_info}");
8383
self.show_message(
8484
lsp_types::MessageType::ERROR,
8585
message,
8686
tracing::enabled!(tracing::Level::ERROR),
8787
);
8888
}
8989
None => {
90-
tracing::error!("{}", &message);
90+
tracing::error!("{message}");
9191
self.send_notification::<lsp_types::notification::ShowMessage>(
9292
lsp_types::ShowMessageParams { typ: lsp_types::MessageType::ERROR, message },
9393
);

crates/syntax/src/algo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ fn main() {
643643
let deletions = diff
644644
.deletions
645645
.iter()
646-
.format_with("\n", |v, f| f(&format!("Line {}: {}", line_number(v), &fmt_syntax(v))));
646+
.format_with("\n", |v, f| f(&format!("Line {}: {}", line_number(v), fmt_syntax(v))));
647647

648648
let actual = format!(
649649
"insertions:\n\n{insertions}\n\nreplacements:\n\n{replacements}\n\ndeletions:\n\n{deletions}\n"

crates/syntax/src/ast/make.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,18 @@ pub fn ty_alias(
179179
}
180180

181181
if let Some(list) = type_param_bounds {
182-
s.push_str(&format!(" : {}", &list));
182+
s.push_str(&format!(" : {list}"));
183183
}
184184

185185
if let Some(cl) = where_clause {
186-
s.push_str(&format!(" {}", &cl.to_string()));
186+
s.push_str(&format!(" {cl}"));
187187
}
188188

189189
if let Some(exp) = assignment {
190190
if let Some(cl) = exp.1 {
191-
s.push_str(&format!(" = {} {}", &exp.0.to_string(), &cl.to_string()));
191+
s.push_str(&format!(" = {} {cl}", exp.0));
192192
} else {
193-
s.push_str(&format!(" = {}", &exp.0.to_string()));
193+
s.push_str(&format!(" = {}", exp.0));
194194
}
195195
}
196196

0 commit comments

Comments
 (0)