Skip to content

Commit

Permalink
Auto merge of rust-lang#17641 - nyurik:optimize-refs, r=Veykril
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Jul 20, 2024
2 parents 91b9739 + afa72d5 commit aa52c5d
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub(crate) fn bind_unused_param(acc: &mut Assists, ctx: &AssistContext<'_>) -> O

acc.add(
AssistId("bind_unused_param", AssistKind::QuickFix),
&format!("Bind as `let _ = {};`", &ident_pat),
&format!("Bind as `let _ = {ident_pat};`"),
param.syntax().text_range(),
|builder| {
let line_index = ctx.db().line_index(ctx.file_id().into());
Expand Down
4 changes: 2 additions & 2 deletions src/tools/rust-analyzer/crates/ide-completion/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ mod tests {
/// If provided vec![vec![a], vec![b, c], vec![d]], then this will assert:
/// a.score < b.score == c.score < d.score
fn check_relevance_score_ordered(expected_relevance_order: Vec<Vec<CompletionRelevance>>) {
let expected = format!("{:#?}", &expected_relevance_order);
let expected = format!("{expected_relevance_order:#?}");

let actual_relevance_order = expected_relevance_order
.into_iter()
Expand All @@ -685,7 +685,7 @@ mod tests {
)
.1;

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

assert_eq_text!(&expected, &actual);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::TypedHole) -> Option<Vec<Assist>
.unique()
.map(|code| Assist {
id: AssistId("typed-hole", AssistKind::QuickFix),
label: Label::new(format!("Replace `_` with `{}`", &code)),
label: Label::new(format!("Replace `_` with `{code}`")),
group: Some(GroupLabel("Replace `_` with a term".to_owned())),
target: original_range.range,
source_change: Some(SourceChange::from_text_edit(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ impl flags::AnalysisStats {
if found_terms.is_empty() {
acc.tail_expr_no_term += 1;
acc.total_tail_exprs += 1;
// println!("\n{}\n", &original_text);
// println!("\n{original_text}\n");
continue;
};

Expand Down
4 changes: 2 additions & 2 deletions src/tools/rust-analyzer/crates/rust-analyzer/src/lsp/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ impl GlobalState {
pub(crate) fn show_and_log_error(&mut self, message: String, additional_info: Option<String>) {
match additional_info {
Some(additional_info) => {
tracing::error!("{}:\n{}", &message, &additional_info);
tracing::error!("{message}:\n{additional_info}");
self.show_message(
lsp_types::MessageType::ERROR,
message,
tracing::enabled!(tracing::Level::ERROR),
);
}
None => {
tracing::error!("{}", &message);
tracing::error!("{message}");
self.send_notification::<lsp_types::notification::ShowMessage>(
lsp_types::ShowMessageParams { typ: lsp_types::MessageType::ERROR, message },
);
Expand Down
2 changes: 1 addition & 1 deletion src/tools/rust-analyzer/crates/syntax/src/algo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ fn main() {
let deletions = diff
.deletions
.iter()
.format_with("\n", |v, f| f(&format!("Line {}: {}", line_number(v), &fmt_syntax(v))));
.format_with("\n", |v, f| f(&format!("Line {}: {}", line_number(v), fmt_syntax(v))));

let actual = format!(
"insertions:\n\n{insertions}\n\nreplacements:\n\n{replacements}\n\ndeletions:\n\n{deletions}\n"
Expand Down
8 changes: 4 additions & 4 deletions src/tools/rust-analyzer/crates/syntax/src/ast/make.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,18 +179,18 @@ pub fn ty_alias(
}

if let Some(list) = type_param_bounds {
s.push_str(&format!(" : {}", &list));
s.push_str(&format!(" : {list}"));
}

if let Some(cl) = where_clause {
s.push_str(&format!(" {}", &cl.to_string()));
s.push_str(&format!(" {cl}"));
}

if let Some(exp) = assignment {
if let Some(cl) = exp.1 {
s.push_str(&format!(" = {} {}", &exp.0.to_string(), &cl.to_string()));
s.push_str(&format!(" = {} {cl}", exp.0));
} else {
s.push_str(&format!(" = {}", &exp.0.to_string()));
s.push_str(&format!(" = {}", exp.0));
}
}

Expand Down

0 comments on commit aa52c5d

Please sign in to comment.