Skip to content

Commit f8126ac

Browse files
committed
Use Symbol instead of strings.
1 parent 8c26e87 commit f8126ac

File tree

2 files changed

+32
-36
lines changed

2 files changed

+32
-36
lines changed

compiler/rustc_mir_transform/src/errors.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,20 +129,20 @@ pub(crate) struct MCDCExceedsTestVectorLimit {
129129
#[diag(mir_transform_unused_capture_maybe_capture_ref)]
130130
#[help]
131131
pub(crate) struct UnusedCaptureMaybeCaptureRef {
132-
pub name: String,
132+
pub name: Symbol,
133133
}
134134

135135
#[derive(LintDiagnostic)]
136136
#[diag(mir_transform_unused_var_assigned_only)]
137137
#[note]
138138
pub(crate) struct UnusedVarAssignedOnly {
139-
pub name: String,
139+
pub name: Symbol,
140140
}
141141

142142
#[derive(LintDiagnostic)]
143143
#[diag(mir_transform_unused_assign)]
144144
pub(crate) struct UnusedAssign {
145-
pub name: String,
145+
pub name: Symbol,
146146
#[subdiagnostic]
147147
pub suggestion: Option<UnusedAssignSuggestion>,
148148
#[help]
@@ -167,13 +167,13 @@ pub(crate) struct UnusedAssignSuggestion {
167167
#[diag(mir_transform_unused_assign_passed)]
168168
#[help]
169169
pub(crate) struct UnusedAssignPassed {
170-
pub name: String,
170+
pub name: Symbol,
171171
}
172172

173173
#[derive(LintDiagnostic)]
174174
#[diag(mir_transform_unused_variable)]
175175
pub(crate) struct UnusedVariable {
176-
pub name: String,
176+
pub name: Symbol,
177177
#[subdiagnostic]
178178
pub string_interp: Vec<UnusedVariableStringInterp>,
179179
#[subdiagnostic]
@@ -191,7 +191,7 @@ pub(crate) enum UnusedVariableSugg {
191191
shorthands: Vec<Span>,
192192
#[suggestion_part(code = "_")]
193193
non_shorthands: Vec<Span>,
194-
name: String,
194+
name: Symbol,
195195
},
196196

197197
#[multipart_suggestion(
@@ -201,14 +201,14 @@ pub(crate) enum UnusedVariableSugg {
201201
TryPrefix {
202202
#[suggestion_part(code = "_{name}")]
203203
spans: Vec<Span>,
204-
name: String,
204+
name: Symbol,
205205
},
206206

207207
#[help(mir_transform_unused_variable_args_in_macro)]
208208
NoSugg {
209209
#[primary_span]
210210
span: Span,
211-
name: String,
211+
name: Symbol,
212212
},
213213
}
214214

compiler/rustc_mir_transform/src/liveness.rs

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_mir_dataflow::fmt::DebugWithContext;
1414
use rustc_mir_dataflow::{Analysis, Backward, ResultsCursor};
1515
use rustc_session::lint;
1616
use rustc_span::Span;
17-
use rustc_span::symbol::sym;
17+
use rustc_span::symbol::{Symbol, kw, sym};
1818

1919
use crate::errors;
2020

@@ -161,7 +161,7 @@ fn is_capture(place: PlaceRef<'_>) -> bool {
161161
/// interpolate our dead local.
162162
fn maybe_suggest_literal_matching_name(
163163
body: &Body<'_>,
164-
name: &str,
164+
name: Symbol,
165165
) -> Vec<errors::UnusedVariableStringInterp> {
166166
struct LiteralFinder<'body, 'tcx> {
167167
body: &'body Body<'tcx>,
@@ -425,7 +425,7 @@ fn find_self_assignments<'tcx>(
425425
#[derive(Default, Debug)]
426426
struct PlaceSet<'tcx> {
427427
places: IndexVec<PlaceIndex, PlaceRef<'tcx>>,
428-
names: IndexVec<PlaceIndex, Option<(String, Span)>>,
428+
names: IndexVec<PlaceIndex, Option<(Symbol, Span)>>,
429429

430430
/// Places corresponding to locals, common case.
431431
locals: IndexVec<Local, Option<PlaceIndex>>,
@@ -487,7 +487,10 @@ impl<'tcx> PlaceSet<'tcx> {
487487

488488
// Record a variable name from the capture, because it is much friendlier than the
489489
// debuginfo name.
490-
self.names.insert(index, (capture.to_string(tcx), capture.get_path_span(tcx)));
490+
self.names.insert(
491+
index,
492+
(Symbol::intern(&capture.to_string(tcx)), capture.get_path_span(tcx)),
493+
);
491494
}
492495
}
493496

@@ -497,7 +500,7 @@ impl<'tcx> PlaceSet<'tcx> {
497500
&& let Some(index) = self.locals[place.local]
498501
{
499502
self.names.get_or_insert_with(index, || {
500-
(var_debug_info.name.to_string(), var_debug_info.source_info.span)
503+
(var_debug_info.name, var_debug_info.source_info.span)
501504
});
502505
}
503506
}
@@ -789,8 +792,8 @@ impl AssignmentResult {
789792
continue;
790793
}
791794

792-
let Some((ref name, def_span)) = checked_places.names[index] else { continue };
793-
if name.is_empty() || name.starts_with('_') || name == "self" {
795+
let Some((name, def_span)) = checked_places.names[index] else { continue };
796+
if name.is_empty() || name.as_str().starts_with('_') || name == kw::SelfLower {
794797
continue;
795798
}
796799

@@ -817,19 +820,16 @@ impl AssignmentResult {
817820
let statements = &mut self.assignments[index];
818821
if statements.is_empty() {
819822
let sugg = if from_macro {
820-
errors::UnusedVariableSugg::NoSugg { span: def_span, name: name.clone() }
823+
errors::UnusedVariableSugg::NoSugg { span: def_span, name }
821824
} else {
822-
errors::UnusedVariableSugg::TryPrefix {
823-
spans: vec![def_span],
824-
name: name.clone(),
825-
}
825+
errors::UnusedVariableSugg::TryPrefix { spans: vec![def_span], name }
826826
};
827827
tcx.emit_node_span_lint(
828828
lint::builtin::UNUSED_VARIABLES,
829829
hir_id,
830830
def_span,
831831
errors::UnusedVariable {
832-
name: name.clone(),
832+
name,
833833
string_interp: maybe_suggest_literal_matching_name(body, name),
834834
sugg,
835835
},
@@ -867,7 +867,7 @@ impl AssignmentResult {
867867
lint::builtin::UNUSED_VARIABLES,
868868
hir_id,
869869
def_span,
870-
errors::UnusedVarAssignedOnly { name: name.clone() },
870+
errors::UnusedVarAssignedOnly { name },
871871
);
872872
continue;
873873
}
@@ -879,7 +879,7 @@ impl AssignmentResult {
879879

880880
let sugg = if any_shorthand {
881881
errors::UnusedVariableSugg::TryIgnore {
882-
name: name.clone(),
882+
name,
883883
shorthands: introductions
884884
.iter()
885885
.filter_map(
@@ -896,19 +896,19 @@ impl AssignmentResult {
896896
.collect(),
897897
}
898898
} else if from_macro {
899-
errors::UnusedVariableSugg::NoSugg { span: def_span, name: name.clone() }
899+
errors::UnusedVariableSugg::NoSugg { span: def_span, name }
900900
} else if !introductions.is_empty() {
901-
errors::UnusedVariableSugg::TryPrefix { name: name.clone(), spans: spans.clone() }
901+
errors::UnusedVariableSugg::TryPrefix { name, spans: spans.clone() }
902902
} else {
903-
errors::UnusedVariableSugg::TryPrefix { name: name.clone(), spans: vec![def_span] }
903+
errors::UnusedVariableSugg::TryPrefix { name, spans: vec![def_span] }
904904
};
905905

906906
tcx.emit_node_span_lint(
907907
lint::builtin::UNUSED_VARIABLES,
908908
hir_id,
909909
spans,
910910
errors::UnusedVariable {
911-
name: name.clone(),
911+
name,
912912
string_interp: maybe_suggest_literal_matching_name(body, name),
913913
sugg,
914914
},
@@ -930,8 +930,8 @@ impl AssignmentResult {
930930
continue;
931931
}
932932

933-
let Some((ref name, decl_span)) = checked_places.names[index] else { continue };
934-
if name.is_empty() || name.starts_with('_') || name == "self" {
933+
let Some((name, decl_span)) = checked_places.names[index] else { continue };
934+
if name.is_empty() || name.as_str().starts_with('_') || name == kw::SelfLower {
935935
continue;
936936
}
937937

@@ -966,24 +966,20 @@ impl AssignmentResult {
966966
lint::builtin::UNUSED_ASSIGNMENTS,
967967
hir_id,
968968
source_info.span,
969-
errors::UnusedAssign {
970-
name: name.clone(),
971-
help: suggestion.is_none(),
972-
suggestion,
973-
},
969+
errors::UnusedAssign { name, help: suggestion.is_none(), suggestion },
974970
)
975971
}
976972
AccessKind::Param => tcx.emit_node_span_lint(
977973
lint::builtin::UNUSED_ASSIGNMENTS,
978974
hir_id,
979975
source_info.span,
980-
errors::UnusedAssignPassed { name: name.clone() },
976+
errors::UnusedAssignPassed { name },
981977
),
982978
AccessKind::Capture => tcx.emit_node_span_lint(
983979
lint::builtin::UNUSED_ASSIGNMENTS,
984980
hir_id,
985981
decl_span,
986-
errors::UnusedCaptureMaybeCaptureRef { name: name.clone() },
982+
errors::UnusedCaptureMaybeCaptureRef { name },
987983
),
988984
}
989985
}

0 commit comments

Comments
 (0)