mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-27 23:20:39 +00:00
Auto merge of #13130 - nyurik:ref-lints, r=Centri3
Avoid ref when using format! 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 also https://github.com/rust-lang/rust/issues/112156 changelog: none
This commit is contained in:
commit
479491ebce
6 changed files with 8 additions and 8 deletions
|
@ -97,7 +97,7 @@ impl ApproxConstant {
|
||||||
cx,
|
cx,
|
||||||
APPROX_CONSTANT,
|
APPROX_CONSTANT,
|
||||||
e.span,
|
e.span,
|
||||||
format!("approximate value of `{module}::consts::{}` found", &name),
|
format!("approximate value of `{module}::consts::{name}` found"),
|
||||||
None,
|
None,
|
||||||
"consider using the constant directly",
|
"consider using the constant directly",
|
||||||
);
|
);
|
||||||
|
|
|
@ -221,7 +221,7 @@ impl<'tcx> LateLintPass<'tcx> for Default {
|
||||||
.map(ToString::to_string)
|
.map(ToString::to_string)
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join(", ");
|
.join(", ");
|
||||||
format!("{adt_def_ty_name}::<{}>", &tys_str)
|
format!("{adt_def_ty_name}::<{tys_str}>")
|
||||||
} else {
|
} else {
|
||||||
binding_type.to_string()
|
binding_type.to_string()
|
||||||
};
|
};
|
||||||
|
|
|
@ -151,7 +151,7 @@ fn check_open_options(cx: &LateContext<'_>, settings: &[(OpenOption, Argument, S
|
||||||
cx,
|
cx,
|
||||||
NONSENSICAL_OPEN_OPTIONS,
|
NONSENSICAL_OPEN_OPTIONS,
|
||||||
prev_span,
|
prev_span,
|
||||||
format!("the method `{}` is called more than once", &option),
|
format!("the method `{option}` is called more than once"),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -127,7 +127,7 @@ pub(super) fn check<'tcx>(
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join(" and ");
|
.join(" and ");
|
||||||
|
|
||||||
format!("methods with the following characteristics: ({})", &s)
|
format!("methods with the following characteristics: ({s})")
|
||||||
} else {
|
} else {
|
||||||
format!("methods called {}", &conventions[0])
|
format!("methods called {}", &conventions[0])
|
||||||
}
|
}
|
||||||
|
|
|
@ -273,7 +273,7 @@ fn check_unnecessary_operation(cx: &LateContext<'_>, stmt: &Stmt<'_>) {
|
||||||
}
|
}
|
||||||
let snippet =
|
let snippet =
|
||||||
if let (Some(arr), Some(func)) = (snippet_opt(cx, reduced[0].span), snippet_opt(cx, reduced[1].span)) {
|
if let (Some(arr), Some(func)) = (snippet_opt(cx, reduced[0].span), snippet_opt(cx, reduced[1].span)) {
|
||||||
format!("assert!({}.len() > {});", &arr, &func)
|
format!("assert!({arr}.len() > {func});")
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
|
@ -48,15 +48,15 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, lt: &Lifetime, m
|
||||||
let inner_snippet = snippet(cx, inner.span, "..");
|
let inner_snippet = snippet(cx, inner.span, "..");
|
||||||
let suggestion = match &inner.kind {
|
let suggestion = match &inner.kind {
|
||||||
TyKind::TraitObject(bounds, lt_bound, _) if bounds.len() > 1 || !lt_bound.is_elided() => {
|
TyKind::TraitObject(bounds, lt_bound, _) if bounds.len() > 1 || !lt_bound.is_elided() => {
|
||||||
format!("&{ltopt}({})", &inner_snippet)
|
format!("&{ltopt}({inner_snippet})")
|
||||||
},
|
},
|
||||||
TyKind::Path(qpath)
|
TyKind::Path(qpath)
|
||||||
if get_bounds_if_impl_trait(cx, qpath, inner.hir_id)
|
if get_bounds_if_impl_trait(cx, qpath, inner.hir_id)
|
||||||
.map_or(false, |bounds| bounds.len() > 1) =>
|
.map_or(false, |bounds| bounds.len() > 1) =>
|
||||||
{
|
{
|
||||||
format!("&{ltopt}({})", &inner_snippet)
|
format!("&{ltopt}({inner_snippet})")
|
||||||
},
|
},
|
||||||
_ => format!("&{ltopt}{}", &inner_snippet),
|
_ => format!("&{ltopt}{inner_snippet}"),
|
||||||
};
|
};
|
||||||
span_lint_and_sugg(
|
span_lint_and_sugg(
|
||||||
cx,
|
cx,
|
||||||
|
|
Loading…
Reference in a new issue