avoid some &str to String conversions

This commit is contained in:
Takayuki Maeda 2022-07-10 03:18:56 +09:00
parent fac8fa5672
commit bda83e6543
13 changed files with 23 additions and 34 deletions

View File

@ -597,16 +597,16 @@ impl UseSpans<'_> {
} }
/// Describe the span associated with a use of a place. /// Describe the span associated with a use of a place.
pub(super) fn describe(&self) -> String { pub(super) fn describe(&self) -> &str {
match *self { match *self {
UseSpans::ClosureUse { generator_kind, .. } => { UseSpans::ClosureUse { generator_kind, .. } => {
if generator_kind.is_some() { if generator_kind.is_some() {
" in generator".to_string() " in generator"
} else { } else {
" in closure".to_string() " in closure"
} }
} }
_ => String::new(), _ => "",
} }
} }

View File

@ -1929,12 +1929,10 @@ impl BorrowKind {
} }
} }
pub fn describe_mutability(&self) -> String { pub fn describe_mutability(&self) -> &str {
match *self { match *self {
BorrowKind::Shared | BorrowKind::Shallow | BorrowKind::Unique => { BorrowKind::Shared | BorrowKind::Shallow | BorrowKind::Unique => "immutable",
"immutable".to_string() BorrowKind::Mut { .. } => "mutable",
}
BorrowKind::Mut { .. } => "mutable".to_string(),
} }
} }
} }

View File

@ -39,13 +39,11 @@ fn unsafe_derive_on_repr_packed(tcx: TyCtxt<'_>, def_id: LocalDefId) {
let message = if tcx.generics_of(def_id).own_requires_monomorphization() { let message = if tcx.generics_of(def_id).own_requires_monomorphization() {
"`#[derive]` can't be used on a `#[repr(packed)]` struct with \ "`#[derive]` can't be used on a `#[repr(packed)]` struct with \
type or const parameters (error E0133)" type or const parameters (error E0133)"
.to_string()
} else { } else {
"`#[derive]` can't be used on a `#[repr(packed)]` struct that \ "`#[derive]` can't be used on a `#[repr(packed)]` struct that \
does not derive Copy (error E0133)" does not derive Copy (error E0133)"
.to_string()
}; };
lint.build(&message).emit(); lint.build(message).emit();
}); });
} }

View File

@ -928,10 +928,7 @@ impl<'a> Resolver<'a> {
"generic parameters with a default cannot use \ "generic parameters with a default cannot use \
forward declared identifiers" forward declared identifiers"
); );
err.span_label( err.span_label(span, "defaulted generic parameters cannot be forward declared");
span,
"defaulted generic parameters cannot be forward declared".to_string(),
);
err err
} }
ResolutionError::ParamInTyOfConstParam(name) => { ResolutionError::ParamInTyOfConstParam(name) => {

View File

@ -349,10 +349,8 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
err.code(rustc_errors::error_code!(E0424)); err.code(rustc_errors::error_code!(E0424));
err.span_label(span, match source { err.span_label(span, match source {
PathSource::Pat => "`self` value is a keyword and may not be bound to variables or shadowed" PathSource::Pat => "`self` value is a keyword and may not be bound to variables or shadowed",
.to_string(), _ => "`self` value is a keyword only available in methods with a `self` parameter",
_ => "`self` value is a keyword only available in methods with a `self` parameter"
.to_string(),
}); });
if let Some((fn_kind, span)) = &self.diagnostic_metadata.current_function { if let Some((fn_kind, span)) = &self.diagnostic_metadata.current_function {
// The current function has a `self' parameter, but we were unable to resolve // The current function has a `self' parameter, but we were unable to resolve

View File

@ -629,8 +629,7 @@ pub fn debug_hygiene_data(verbose: bool) -> String {
if verbose { if verbose {
format!("{:#?}", data) format!("{:#?}", data)
} else { } else {
let mut s = String::from(""); let mut s = String::from("Expansions:");
s.push_str("Expansions:");
let mut debug_expn_data = |(id, expn_data): (&ExpnId, &ExpnData)| { let mut debug_expn_data = |(id, expn_data): (&ExpnId, &ExpnData)| {
s.push_str(&format!( s.push_str(&format!(
"\n{:?}: parent: {:?}, call_site_ctxt: {:?}, def_site_ctxt: {:?}, kind: {:?}", "\n{:?}: parent: {:?}, call_site_ctxt: {:?}, def_site_ctxt: {:?}, kind: {:?}",

View File

@ -406,7 +406,7 @@ fn report_conflicting_impls(
let mut err = err.build(&msg); let mut err = err.build(&msg);
match tcx.span_of_impl(overlap.with_impl) { match tcx.span_of_impl(overlap.with_impl) {
Ok(span) => { Ok(span) => {
err.span_label(span, "first implementation here".to_string()); err.span_label(span, "first implementation here");
err.span_label( err.span_label(
impl_span, impl_span,

View File

@ -263,7 +263,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} else if let ExprKind::Block(block, _) = &then_expr.kind } else if let ExprKind::Block(block, _) = &then_expr.kind
&& let Some(expr) = &block.expr && let Some(expr) = &block.expr
{ {
err.span_label(expr.span, "found here".to_string()); err.span_label(expr.span, "found here");
} }
err.note("`if` expressions without `else` evaluate to `()`"); err.note("`if` expressions without `else` evaluate to `()`");
err.help("consider adding an `else` block that evaluates to the expected type"); err.help("consider adding an `else` block that evaluates to the expected type");

View File

@ -317,9 +317,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.tcx .tcx
.is_diagnostic_item(sym::Result, expected_adt.did()) .is_diagnostic_item(sym::Result, expected_adt.did())
{ {
vec!["Ok(())".to_string()] vec!["Ok(())"]
} else if self.tcx.is_diagnostic_item(sym::Option, expected_adt.did()) { } else if self.tcx.is_diagnostic_item(sym::Option, expected_adt.did()) {
vec!["None".to_string(), "Some(())".to_string()] vec!["None", "Some(())"]
} else { } else {
return; return;
}; };

View File

@ -565,9 +565,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.is_ok() .is_ok()
{ {
let (variable_snippet, applicability) = if !fn_sig.inputs().is_empty() { let (variable_snippet, applicability) = if !fn_sig.inputs().is_empty() {
("( /* arguments */ )".to_string(), Applicability::HasPlaceholders) ("( /* arguments */ )", Applicability::HasPlaceholders)
} else { } else {
("()".to_string(), Applicability::MaybeIncorrect) ("()", Applicability::MaybeIncorrect)
}; };
err.span_suggestion_verbose( err.span_suggestion_verbose(

View File

@ -86,7 +86,7 @@ fn diagnostic_hir_wf_check<'tcx>(
let errors = fulfill.select_all_or_error(&infcx); let errors = fulfill.select_all_or_error(&infcx);
if !errors.is_empty() { if !errors.is_empty() {
tracing::debug!("Wf-check got errors for {:?}: {:?}", ty, errors); debug!("Wf-check got errors for {:?}: {:?}", ty, errors);
for error in errors { for error in errors {
if error.obligation.predicate == self.predicate { if error.obligation.predicate == self.predicate {
// Save the cause from the greatest depth - this corresponds // Save the cause from the greatest depth - this corresponds

View File

@ -252,7 +252,7 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
let mut diag = let mut diag =
struct_span_err!(tcx.sess, generics_param_span.unwrap_or(main_span), E0131, "{}", msg); struct_span_err!(tcx.sess, generics_param_span.unwrap_or(main_span), E0131, "{}", msg);
if let Some(generics_param_span) = generics_param_span { if let Some(generics_param_span) = generics_param_span {
let label = "`main` cannot have generic parameters".to_string(); let label = "`main` cannot have generic parameters";
diag.span_label(generics_param_span, label); diag.span_label(generics_param_span, label);
} }
diag.emit(); diag.emit();
@ -307,8 +307,7 @@ fn check_main_fn_ty(tcx: TyCtxt<'_>, main_def_id: DefId) {
let return_ty_span = main_fn_return_type_span(tcx, main_def_id).unwrap_or(main_span); let return_ty_span = main_fn_return_type_span(tcx, main_def_id).unwrap_or(main_span);
if !return_ty.bound_vars().is_empty() { if !return_ty.bound_vars().is_empty() {
let msg = "`main` function return type is not allowed to have generic \ let msg = "`main` function return type is not allowed to have generic \
parameters" parameters";
.to_owned();
struct_span_err!(tcx.sess, return_ty_span, E0131, "{}", msg).emit(); struct_span_err!(tcx.sess, return_ty_span, E0131, "{}", msg).emit();
error = true; error = true;
} }

View File

@ -126,8 +126,8 @@ impl<'a, 'tcx> WrongNumberOfGenericArgs<'a, 'tcx> {
} }
} }
fn kind(&self) -> String { fn kind(&self) -> &str {
if self.missing_lifetimes() { "lifetime".to_string() } else { "generic".to_string() } if self.missing_lifetimes() { "lifetime" } else { "generic" }
} }
fn num_provided_args(&self) -> usize { fn num_provided_args(&self) -> usize {