Rollup merge of #98668 - TaKO8Ki:avoid-many-&str-to-string-conversions, r=Dylan-DPC

Avoid some `&str` to `String` conversions with `MultiSpan::push_span_label`

This patch removes some`&str` to `String` conversions with `MultiSpan::push_span_label`.
This commit is contained in:
Matthias Krüger 2022-06-29 20:35:07 +02:00 committed by GitHub
commit d34c4ca9be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 38 additions and 66 deletions

View File

@ -136,7 +136,7 @@ fn test_harness(file_text: &str, span_labels: Vec<SpanLabel>, expected_output: &
let mut msp = MultiSpan::from_span(primary_span); let mut msp = MultiSpan::from_span(primary_span);
for span_label in span_labels { for span_label in span_labels {
let span = make_span(&file_text, &span_label.start, &span_label.end); let span = make_span(&file_text, &span_label.start, &span_label.end);
msp.push_span_label(span, span_label.label.to_string()); msp.push_span_label(span, span_label.label);
println!("span: {:?} label: {:?}", span, span_label.label); println!("span: {:?} label: {:?}", span, span_label.label);
println!("text: {:?}", source_map.span_to_snippet(span)); println!("text: {:?}", source_map.span_to_snippet(span));
} }

View File

@ -194,10 +194,7 @@ pub(super) fn try_report_static_impl_trait(&self) -> Option<ErrorGuaranteed> {
if !v.0.is_empty() { if !v.0.is_empty() {
span = v.0.clone().into(); span = v.0.clone().into();
for sp in v.0 { for sp in v.0 {
span.push_span_label( span.push_span_label(sp, "`'static` requirement introduced here");
sp,
"`'static` requirement introduced here".to_string(),
);
} }
add_label = false; add_label = false;
} }
@ -205,13 +202,10 @@ pub(super) fn try_report_static_impl_trait(&self) -> Option<ErrorGuaranteed> {
if add_label { if add_label {
span.push_span_label( span.push_span_label(
fn_decl.output.span(), fn_decl.output.span(),
"requirement introduced by this return type".to_string(), "requirement introduced by this return type",
); );
} }
span.push_span_label( span.push_span_label(cause.span, "because of this returned expression");
cause.span,
"because of this returned expression".to_string(),
);
err.span_note( err.span_note(
span, span,
"`'static` lifetime requirement introduced by the return type", "`'static` lifetime requirement introduced by the return type",
@ -523,13 +517,11 @@ fn suggest_constrain_dyn_trait_in_impl(
hir_v.visit_ty(&self_ty); hir_v.visit_ty(&self_ty);
for span in &traits { for span in &traits {
let mut multi_span: MultiSpan = vec![*span].into(); let mut multi_span: MultiSpan = vec![*span].into();
multi_span.push_span_label( multi_span
*span, .push_span_label(*span, "this has an implicit `'static` lifetime requirement");
"this has an implicit `'static` lifetime requirement".to_string(),
);
multi_span.push_span_label( multi_span.push_span_label(
ident.span, ident.span,
"calling this method introduces the `impl`'s 'static` requirement".to_string(), "calling this method introduces the `impl`'s 'static` requirement",
); );
err.span_note(multi_span, "the used `impl` has a `'static` requirement"); err.span_note(multi_span, "the used `impl` has a `'static` requirement");
err.span_suggestion_verbose( err.span_suggestion_verbose(

View File

@ -128,10 +128,8 @@ fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
} }
let mut type_param_span: MultiSpan = visitor.types.to_vec().into(); let mut type_param_span: MultiSpan = visitor.types.to_vec().into();
for &span in &visitor.types { for &span in &visitor.types {
type_param_span.push_span_label( type_param_span
span, .push_span_label(span, "consider borrowing this type parameter in the trait");
"consider borrowing this type parameter in the trait".to_string(),
);
} }
err.note(&format!("expected `{}`\n found `{}`", expected, found)); err.note(&format!("expected `{}`\n found `{}`", expected, found));

View File

@ -85,8 +85,7 @@ pub fn report_object_safety_error<'tcx>(
let has_multi_span = !multi_span.is_empty(); let has_multi_span = !multi_span.is_empty();
let mut note_span = MultiSpan::from_spans(multi_span.clone()); let mut note_span = MultiSpan::from_spans(multi_span.clone());
if let (Some(trait_span), true) = (trait_span, has_multi_span) { if let (Some(trait_span), true) = (trait_span, has_multi_span) {
note_span note_span.push_span_label(trait_span, "this trait cannot be made into an object...");
.push_span_label(trait_span, "this trait cannot be made into an object...".to_string());
} }
for (span, msg) in iter::zip(multi_span, messages) { for (span, msg) in iter::zip(multi_span, messages) {
note_span.push_span_label(span, msg); note_span.push_span_label(span, msg);

View File

@ -947,7 +947,7 @@ fn adt_defined_here<'p, 'tcx>(
span.push_span_label(def_span, String::new()); span.push_span_label(def_span, String::new());
for pat in spans { for pat in spans {
span.push_span_label(pat, "not covered".to_string()); span.push_span_label(pat, "not covered");
} }
err.span_note(span, &format!("`{}` defined here", ty)); err.span_note(span, &format!("`{}` defined here", ty));
} }

View File

@ -891,22 +891,19 @@ fn recover_missing_braces_around_closure_body(
let mut first_note = MultiSpan::from(vec![initial_semicolon]); let mut first_note = MultiSpan::from(vec![initial_semicolon]);
first_note.push_span_label( first_note.push_span_label(
initial_semicolon, initial_semicolon,
"this `;` turns the preceding closure into a statement".to_string(), "this `;` turns the preceding closure into a statement",
); );
first_note.push_span_label( first_note.push_span_label(
closure_spans.body, closure_spans.body,
"this expression is a statement because of the trailing semicolon".to_string(), "this expression is a statement because of the trailing semicolon",
); );
expect_err.span_note(first_note, "statement found outside of a block"); expect_err.span_note(first_note, "statement found outside of a block");
let mut second_note = MultiSpan::from(vec![closure_spans.whole_closure]); let mut second_note = MultiSpan::from(vec![closure_spans.whole_closure]);
second_note.push_span_label( second_note.push_span_label(closure_spans.whole_closure, "this is the parsed closure...");
closure_spans.whole_closure,
"this is the parsed closure...".to_string(),
);
second_note.push_span_label( second_note.push_span_label(
following_token_span, following_token_span,
"...but likely you meant the closure to end here".to_string(), "...but likely you meant the closure to end here",
); );
expect_err.span_note(second_note, "the closure body may be incorrectly delimited"); expect_err.span_note(second_note, "the closure body may be incorrectly delimited");

View File

@ -857,11 +857,8 @@ fn check_doc_inline(
if let Some((prev_inline, prev_span)) = *specified_inline { if let Some((prev_inline, prev_span)) = *specified_inline {
if do_inline != prev_inline { if do_inline != prev_inline {
let mut spans = MultiSpan::from_spans(vec![prev_span, meta.span()]); let mut spans = MultiSpan::from_spans(vec![prev_span, meta.span()]);
spans.push_span_label(prev_span, String::from("this attribute...")); spans.push_span_label(prev_span, "this attribute...");
spans.push_span_label( spans.push_span_label(meta.span(), "...conflicts with this attribute");
meta.span(),
String::from("...conflicts with this attribute"),
);
self.tcx self.tcx
.sess .sess
.struct_span_err(spans, "conflicting doc inlining attributes") .struct_span_err(spans, "conflicting doc inlining attributes")

View File

@ -2561,7 +2561,7 @@ fn show_candidates(
let span = source_span[local_def_id]; let span = source_span[local_def_id];
let span = session.source_map().guess_head_span(span); let span = session.source_map().guess_head_span(span);
let mut multi_span = MultiSpan::from_span(span); let mut multi_span = MultiSpan::from_span(span);
multi_span.push_span_label(span, "not accessible".to_string()); multi_span.push_span_label(span, "not accessible");
err.span_note(multi_span, &msg); err.span_note(multi_span, &msg);
} else { } else {
err.note(&msg); err.note(&msg);

View File

@ -601,10 +601,8 @@ struct BaseError<'a> {
}; };
multi_span.push_span_label(sp, msg); multi_span.push_span_label(sp, msg);
} }
multi_span.push_span_label( multi_span
base_error.span, .push_span_label(base_error.span, "expected this type to be a trait...");
"expected this type to be a trait...".to_string(),
);
err.span_help( err.span_help(
multi_span, multi_span,
"`+` is used to constrain a \"trait object\" type with lifetimes or \ "`+` is used to constrain a \"trait object\" type with lifetimes or \
@ -1227,17 +1225,14 @@ fn smart_resolve_context_dependent_help(
let mut m: MultiSpan = non_visible_spans.clone().into(); let mut m: MultiSpan = non_visible_spans.clone().into();
non_visible_spans non_visible_spans
.into_iter() .into_iter()
.for_each(|s| m.push_span_label(s, "private field".to_string())); .for_each(|s| m.push_span_label(s, "private field"));
err.span_note(m, "constructor is not visible here due to private fields"); err.span_note(m, "constructor is not visible here due to private fields");
} }
return true; return true;
} }
err.span_label( err.span_label(span, "constructor is not visible here due to private fields");
span,
"constructor is not visible here due to private fields".to_string(),
);
} }
( (
Res::Def( Res::Def(

View File

@ -2204,8 +2204,7 @@ fn note_obligation_cause_code<T>(
_ => true, _ => true,
}; };
if !ident.span.overlaps(span) && !same_line { if !ident.span.overlaps(span) && !same_line {
multispan multispan.push_span_label(ident.span, "required by a bound in this");
.push_span_label(ident.span, "required by a bound in this".to_string());
} }
} }
let descr = format!("required by a bound in `{}`", item_name); let descr = format!("required by a bound in `{}`", item_name);

View File

@ -645,7 +645,7 @@ pub(crate) fn prohibit_explicit_late_bound_lifetimes(
err.emit(); err.emit();
} else { } else {
let mut multispan = MultiSpan::from_span(span); let mut multispan = MultiSpan::from_span(span);
multispan.push_span_label(span_late, note.to_string()); multispan.push_span_label(span_late, note);
tcx.struct_span_lint_hir( tcx.struct_span_lint_hir(
LATE_BOUND_LIFETIME_ARGUMENTS, LATE_BOUND_LIFETIME_ARGUMENTS,
args.args[0].id(), args.args[0].id(),

View File

@ -154,18 +154,14 @@ pub fn check_match(
ret_span.push_span_label( ret_span.push_span_label(
expr.span, expr.span,
"this could be implicitly returned but it is a statement, not a \ "this could be implicitly returned but it is a statement, not a \
tail expression" tail expression",
.to_owned(),
);
ret_span.push_span_label(
ret,
"the `match` arms can conform to this return type".to_owned(),
); );
ret_span
.push_span_label(ret, "the `match` arms can conform to this return type");
ret_span.push_span_label( ret_span.push_span_label(
semi_span, semi_span,
"the `match` is a statement because of this semicolon, consider \ "the `match` is a statement because of this semicolon, consider \
removing it" removing it",
.to_owned(),
); );
err.span_note( err.span_note(
ret_span, ret_span,

View File

@ -1580,8 +1580,7 @@ fn opaque_type_cycle_error(tcx: TyCtxt<'_>, def_id: LocalDefId, span: Span) -> E
} else { } else {
let mut multispan: MultiSpan = spans.clone().into(); let mut multispan: MultiSpan = spans.clone().into();
for span in spans { for span in spans {
multispan multispan.push_span_label(span, "this returned value is of `!` type");
.push_span_label(span, "this returned value is of `!` type".to_string());
} }
err.span_note(multispan, "these returned values have a concrete \"never\" type"); err.span_note(multispan, "these returned values have a concrete \"never\" type");
} }

View File

@ -1034,7 +1034,7 @@ pub(in super::super) fn note_internal_mutation_in_method(
); );
sp.push_span_label( sp.push_span_label(
rcvr.span, rcvr.span,
"you probably want to use this value after calling the method...".to_string(), "you probably want to use this value after calling the method...",
); );
err.span_note( err.span_note(
sp, sp,

View File

@ -1813,7 +1813,7 @@ fn label_fn_like<'tcx>(
.flat_map(|id| tcx.hir().body(id).params); .flat_map(|id| tcx.hir().body(id).params);
for param in params { for param in params {
spans.push_span_label(param.span, String::new()); spans.push_span_label(param.span, "");
} }
let def_kind = tcx.def_kind(def_id); let def_kind = tcx.def_kind(def_id);

View File

@ -638,7 +638,7 @@ pub fn report_method_error(
let parent_trait_ref = data.parent_trait_pred; let parent_trait_ref = data.parent_trait_pred;
let path = parent_trait_ref.print_modifiers_and_trait_path(); let path = parent_trait_ref.print_modifiers_and_trait_path();
let tr_self_ty = parent_trait_ref.skip_binder().self_ty(); let tr_self_ty = parent_trait_ref.skip_binder().self_ty();
let unsatisfied_msg = "unsatisfied trait bound introduced here".to_string(); let unsatisfied_msg = "unsatisfied trait bound introduced here";
let derive_msg = let derive_msg =
"unsatisfied trait bound introduced in this `derive` macro"; "unsatisfied trait bound introduced in this `derive` macro";
match self.tcx.hir().get_if_local(impl_def_id) { match self.tcx.hir().get_if_local(impl_def_id) {
@ -655,7 +655,7 @@ pub fn report_method_error(
{ {
let span = ident.span.ctxt().outer_expn_data().call_site; let span = ident.span.ctxt().outer_expn_data().call_site;
let mut spans: MultiSpan = span.into(); let mut spans: MultiSpan = span.into();
spans.push_span_label(span, derive_msg.to_string()); spans.push_span_label(span, derive_msg);
let entry = spanned_predicates.entry(spans); let entry = spanned_predicates.entry(spans);
entry.or_insert_with(|| (path, tr_self_ty, Vec::new())).2.push(p); entry.or_insert_with(|| (path, tr_self_ty, Vec::new())).2.push(p);
} }
@ -678,7 +678,7 @@ pub fn report_method_error(
{ {
let span = self_ty.span.ctxt().outer_expn_data().call_site; let span = self_ty.span.ctxt().outer_expn_data().call_site;
let mut spans: MultiSpan = span.into(); let mut spans: MultiSpan = span.into();
spans.push_span_label(span, derive_msg.to_string()); spans.push_span_label(span, derive_msg);
let entry = spanned_predicates.entry(spans); let entry = spanned_predicates.entry(spans);
entry.or_insert_with(|| (path, tr_self_ty, Vec::new())).2.push(p); entry.or_insert_with(|| (path, tr_self_ty, Vec::new())).2.push(p);
} }
@ -706,7 +706,7 @@ pub fn report_method_error(
} else { } else {
ident.span.into() ident.span.into()
}; };
spans.push_span_label(ident.span, "in this trait".to_string()); spans.push_span_label(ident.span, "in this trait");
let entry = spanned_predicates.entry(spans); let entry = spanned_predicates.entry(spans);
entry.or_insert_with(|| (path, tr_self_ty, Vec::new())).2.push(p); entry.or_insert_with(|| (path, tr_self_ty, Vec::new())).2.push(p);
} }
@ -747,9 +747,9 @@ pub fn report_method_error(
spans.into() spans.into()
}; };
if let Some(trait_ref) = of_trait { if let Some(trait_ref) = of_trait {
spans.push_span_label(trait_ref.path.span, String::new()); spans.push_span_label(trait_ref.path.span, "");
} }
spans.push_span_label(self_ty.span, String::new()); spans.push_span_label(self_ty.span, "");
let entry = spanned_predicates.entry(spans); let entry = spanned_predicates.entry(spans);
entry.or_insert_with(|| (path, tr_self_ty, Vec::new())).2.push(p); entry.or_insert_with(|| (path, tr_self_ty, Vec::new())).2.push(p);

View File

@ -836,7 +836,7 @@ fn show_definition(&self, err: &mut Diagnostic) {
.take(bound) .take(bound)
.map(|param| { .map(|param| {
let span = self.tcx.def_span(param.def_id); let span = self.tcx.def_span(param.def_id);
spans.push_span_label(span, String::new()); spans.push_span_label(span, "");
param param
}) })
.map(|param| format!("`{}`", param.name)) .map(|param| format!("`{}`", param.name))