Skip to content

Commit a22934b

Browse files
committed
avoid Symbol to &str conversions
1 parent 95e7764 commit a22934b

File tree

12 files changed

+28
-23
lines changed

12 files changed

+28
-23
lines changed

compiler/rustc_ast_lowering/src/asm.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
145145
InlineAsmRegOrRegClass::Reg(s) => {
146146
asm::InlineAsmRegOrRegClass::Reg(if let Some(asm_arch) = asm_arch {
147147
asm::InlineAsmReg::parse(asm_arch, s).unwrap_or_else(|e| {
148-
let msg = format!("invalid register `{}`: {}", s.as_str(), e);
148+
let msg = format!("invalid register `{}`: {}", s, e);
149149
sess.struct_span_err(*op_sp, &msg).emit();
150150
asm::InlineAsmReg::Err
151151
})
@@ -156,7 +156,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
156156
InlineAsmRegOrRegClass::RegClass(s) => {
157157
asm::InlineAsmRegOrRegClass::RegClass(if let Some(asm_arch) = asm_arch {
158158
asm::InlineAsmRegClass::parse(asm_arch, s).unwrap_or_else(|e| {
159-
let msg = format!("invalid register class `{}`: {}", s.as_str(), e);
159+
let msg = format!("invalid register class `{}`: {}", s, e);
160160
sess.struct_span_err(*op_sp, &msg).emit();
161161
asm::InlineAsmRegClass::Err
162162
})

compiler/rustc_ast_passes/src/feature_gate.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ impl<'a> PostExpansionVisitor<'a> {
6262
let ast::StrLit { symbol_unescaped, span, .. } = abi;
6363

6464
if let ast::Const::Yes(_) = constness {
65-
match symbol_unescaped.as_str() {
65+
match symbol_unescaped {
6666
// Stable
67-
"Rust" | "C" => {}
67+
sym::Rust | sym::C => {}
6868
abi => gate_feature_post!(
6969
&self,
7070
const_extern_fn,

compiler/rustc_codegen_llvm/src/debuginfo/gdb.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub fn get_or_insert_gdb_debug_scripts_section_global<'ll>(cx: &CodegenCx<'ll, '
5555
// The initial byte `4` instructs GDB that the following pretty printer
5656
// is defined inline as opposed to in a standalone file.
5757
section_contents.extend_from_slice(b"\x04");
58-
let vis_name = format!("pretty-printer-{}-{}\n", crate_name.as_str(), index);
58+
let vis_name = format!("pretty-printer-{}-{}\n", crate_name, index);
5959
section_contents.extend_from_slice(vis_name.as_bytes());
6060
section_contents.extend_from_slice(&visualizer.src);
6161

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/different_lifetimes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ pub fn suggest_adding_lifetime_params<'tcx>(
228228
if is_impl {
229229
sugg.push_str(" and update trait if needed");
230230
}
231-
err.multipart_suggestion(sugg.as_str(), suggestions, Applicability::MaybeIncorrect);
231+
err.multipart_suggestion(sugg, suggestions, Applicability::MaybeIncorrect);
232232

233233
true
234234
}

compiler/rustc_passes/src/entry.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ fn err_if_attr_found(ctxt: &EntryContext<'_>, attrs: &[Attribute], sym: Symbol)
7777
.sess
7878
.struct_span_err(
7979
attr.span,
80-
&format!("`{}` attribute can only be used on functions", sym.as_str()),
80+
&format!("`{}` attribute can only be used on functions", sym),
8181
)
8282
.emit();
8383
}

compiler/rustc_resolve/src/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1630,7 +1630,7 @@ impl<'a> Resolver<'a> {
16301630
"{}{} `{}` defined here",
16311631
prefix,
16321632
suggestion.res.descr(),
1633-
suggestion.candidate.as_str(),
1633+
suggestion.candidate,
16341634
),
16351635
);
16361636
}

compiler/rustc_resolve/src/late/diagnostics.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl ForLifetimeSpanType {
8080
}
8181
}
8282

83-
pub(crate) fn suggestion(&self, sugg: &str) -> String {
83+
pub(crate) fn suggestion(&self, sugg: impl std::fmt::Display) -> String {
8484
match self {
8585
Self::BoundEmpty | Self::TypeEmpty => format!("for<{}> ", sugg),
8686
Self::BoundTail | Self::TypeTail => format!(", {}", sugg),
@@ -2311,8 +2311,8 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
23112311

23122312
let suggest_existing =
23132313
|err: &mut Diagnostic,
2314-
name: &str,
2315-
formatters: Vec<Option<Box<dyn Fn(&str) -> String>>>| {
2314+
name: Symbol,
2315+
formatters: Vec<Option<Box<dyn Fn(Symbol) -> String>>>| {
23162316
if let Some(MissingLifetimeSpot::HigherRanked { span: for_span, span_type }) =
23172317
self.missing_named_lifetime_spots.iter().rev().next()
23182318
{
@@ -2332,7 +2332,8 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
23322332
// If all single char lifetime names are present, we wrap around and double the chars.
23332333
let lt_name = (1..)
23342334
.flat_map(a_to_z_repeat_n)
2335-
.find(|lt| !lifetime_names.contains(&Symbol::intern(&lt)))
2335+
.map(|lt| Symbol::intern(&lt))
2336+
.find(|lt| !lifetime_names.contains(lt))
23362337
.unwrap();
23372338
let msg = format!(
23382339
"consider making the {} lifetime-generic with a new `{}` lifetime",
@@ -2359,7 +2360,7 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
23592360
introduce_suggestion.push((*for_span, for_sugg));
23602361
for ((span, _), formatter) in spans_with_counts.iter().zip(formatters.iter()) {
23612362
if let Some(formatter) = formatter {
2362-
introduce_suggestion.push((*span, formatter(&lt_name)));
2363+
introduce_suggestion.push((*span, formatter(lt_name)));
23632364
}
23642365
}
23652366
err.multipart_suggestion_verbose(
@@ -2582,15 +2583,19 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
25822583
let lifetime_names: Vec<_> = lifetime_names.iter().collect();
25832584
match &lifetime_names[..] {
25842585
[name] => {
2585-
let mut suggs: Vec<Option<Box<dyn Fn(&str) -> String>>> = Vec::new();
2586+
let mut suggs: Vec<Option<Box<dyn Fn(Symbol) -> String>>> = Vec::new();
25862587
for (snippet, (_, count)) in snippets.iter().zip(spans_with_counts.iter().copied())
25872588
{
25882589
suggs.push(match snippet.as_deref() {
25892590
Some("&") => Some(Box::new(|name| format!("&{} ", name))),
25902591
Some("'_") => Some(Box::new(|n| n.to_string())),
25912592
Some("") => Some(Box::new(move |n| format!("{}, ", n).repeat(count))),
25922593
Some("<") => Some(Box::new(move |n| {
2593-
std::iter::repeat(n).take(count).collect::<Vec<_>>().join(", ")
2594+
std::iter::repeat(n)
2595+
.take(count)
2596+
.map(|n| n.to_string())
2597+
.collect::<Vec<_>>()
2598+
.join(", ")
25942599
})),
25952600
Some(snippet) if !snippet.ends_with('>') => Some(Box::new(move |name| {
25962601
format!(
@@ -2605,7 +2610,7 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
26052610
_ => None,
26062611
});
26072612
}
2608-
suggest_existing(err, name.as_str(), suggs);
2613+
suggest_existing(err, **name, suggs);
26092614
}
26102615
[] => {
26112616
let mut suggs = Vec::new();

compiler/rustc_resolve/src/macros.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ impl<'a> ResolverExpand for Resolver<'a> {
325325
UNUSED_MACROS,
326326
node_id,
327327
ident.span,
328-
&format!("unused macro definition: `{}`", ident.as_str()),
328+
&format!("unused macro definition: `{}`", ident.name),
329329
);
330330
}
331331
for (&(def_id, arm_i), &(ident, rule_span)) in self.unused_macro_rules.iter() {
@@ -341,7 +341,7 @@ impl<'a> ResolverExpand for Resolver<'a> {
341341
&format!(
342342
"{} rule of macro `{}` is never used",
343343
crate::diagnostics::ordinalize(arm_i + 1),
344-
ident.as_str()
344+
ident.name
345345
),
346346
);
347347
}

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ symbols! {
257257
Result,
258258
Return,
259259
Right,
260+
Rust,
260261
RustcDecodable,
261262
RustcEncodable,
262263
Send,

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
474474
if let Some(ref s) = label {
475475
// If it has a custom `#[rustc_on_unimplemented]`
476476
// error message, let's display it as the label!
477-
err.span_label(span, s.as_str());
477+
err.span_label(span, s);
478478
if !matches!(trait_ref.skip_binder().self_ty().kind(), ty::Param(_)) {
479479
// When the self type is a type param We don't need to "the trait
480480
// `std::marker::Sized` is not implemented for `T`" as we will point
@@ -531,7 +531,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
531531
let enclosing_scope_span =
532532
tcx.hir().span_with_body(tcx.hir().local_def_id_to_hir_id(body));
533533

534-
err.span_label(enclosing_scope_span, s.as_str());
534+
err.span_label(enclosing_scope_span, s);
535535
}
536536

537537
self.suggest_floating_point_literal(&obligation, &mut err, &trait_ref);

compiler/rustc_typeck/src/check/_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
259259
&cause,
260260
&mut |err| {
261261
if let Some((span, msg)) = &ret_reason {
262-
err.span_label(*span, msg.as_str());
262+
err.span_label(*span, msg);
263263
} else if let ExprKind::Block(block, _) = &then_expr.kind
264264
&& let Some(expr) = &block.expr
265265
{

compiler/rustc_typeck/src/check/op.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
395395
false,
396396
),
397397
};
398-
let mut err =
399-
struct_span_err!(self.tcx.sess, op.span, E0369, "{}", message.as_str());
398+
let mut err = struct_span_err!(self.tcx.sess, op.span, E0369, "{message}");
400399
if !lhs_expr.span.eq(&rhs_expr.span) {
401400
self.add_type_neq_err_label(
402401
&mut err,

0 commit comments

Comments
 (0)