Skip to content

Commit f5a19ee

Browse files
committed
Use Symbol instead of strings.
1 parent 72c238c commit f5a19ee

File tree

2 files changed

+32
-35
lines changed

2 files changed

+32
-35
lines changed

compiler/rustc_mir_transform/src/errors.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,20 @@ pub(crate) struct FnItemRef {
121121
#[diag(mir_transform_unused_capture_maybe_capture_ref)]
122122
#[help]
123123
pub(crate) struct UnusedCaptureMaybeCaptureRef {
124-
pub name: String,
124+
pub name: Symbol,
125125
}
126126

127127
#[derive(LintDiagnostic)]
128128
#[diag(mir_transform_unused_var_assigned_only)]
129129
#[note]
130130
pub(crate) struct UnusedVarAssignedOnly {
131-
pub name: String,
131+
pub name: Symbol,
132132
}
133133

134134
#[derive(LintDiagnostic)]
135135
#[diag(mir_transform_unused_assign)]
136136
pub(crate) struct UnusedAssign {
137-
pub name: String,
137+
pub name: Symbol,
138138
#[subdiagnostic]
139139
pub suggestion: Option<UnusedAssignSuggestion>,
140140
#[help]
@@ -159,13 +159,13 @@ pub(crate) struct UnusedAssignSuggestion {
159159
#[diag(mir_transform_unused_assign_passed)]
160160
#[help]
161161
pub(crate) struct UnusedAssignPassed {
162-
pub name: String,
162+
pub name: Symbol,
163163
}
164164

165165
#[derive(LintDiagnostic)]
166166
#[diag(mir_transform_unused_variable)]
167167
pub(crate) struct UnusedVariable {
168-
pub name: String,
168+
pub name: Symbol,
169169
#[subdiagnostic]
170170
pub string_interp: Vec<UnusedVariableStringInterp>,
171171
#[subdiagnostic]
@@ -183,7 +183,7 @@ pub(crate) enum UnusedVariableSugg {
183183
shorthands: Vec<Span>,
184184
#[suggestion_part(code = "_")]
185185
non_shorthands: Vec<Span>,
186-
name: String,
186+
name: Symbol,
187187
},
188188

189189
#[multipart_suggestion(
@@ -193,14 +193,14 @@ pub(crate) enum UnusedVariableSugg {
193193
TryPrefix {
194194
#[suggestion_part(code = "_{name}")]
195195
spans: Vec<Span>,
196-
name: String,
196+
name: Symbol,
197197
},
198198

199199
#[help(mir_transform_unused_variable_args_in_macro)]
200200
NoSugg {
201201
#[primary_span]
202202
span: Span,
203-
name: String,
203+
name: Symbol,
204204
},
205205
}
206206

compiler/rustc_mir_transform/src/liveness.rs

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_mir_dataflow::fmt::DebugWithContext;
1616
use rustc_mir_dataflow::{Analysis, Backward, ResultsCursor};
1717
use rustc_session::lint;
1818
use rustc_span::Span;
19+
use rustc_span::symbol::{Symbol, kw};
1920

2021
use crate::errors;
2122

@@ -162,7 +163,7 @@ fn is_capture(place: PlaceRef<'_>) -> bool {
162163
/// interpolate our dead local.
163164
fn maybe_suggest_literal_matching_name(
164165
body: &Body<'_>,
165-
name: &str,
166+
name: Symbol,
166167
) -> Vec<errors::UnusedVariableStringInterp> {
167168
struct LiteralFinder<'body, 'tcx> {
168169
body: &'body Body<'tcx>,
@@ -426,7 +427,7 @@ fn find_self_assignments<'tcx>(
426427
#[derive(Default, Debug)]
427428
struct PlaceSet<'tcx> {
428429
places: IndexVec<PlaceIndex, PlaceRef<'tcx>>,
429-
names: IndexVec<PlaceIndex, Option<(String, Span)>>,
430+
names: IndexVec<PlaceIndex, Option<(Symbol, Span)>>,
430431

431432
/// Places corresponding to locals, common case.
432433
locals: IndexVec<Local, Option<PlaceIndex>>,
@@ -488,7 +489,10 @@ impl<'tcx> PlaceSet<'tcx> {
488489

489490
// Record a variable name from the capture, because it is much friendlier than the
490491
// debuginfo name.
491-
self.names.insert(index, (capture.to_string(tcx), capture.get_path_span(tcx)));
492+
self.names.insert(
493+
index,
494+
(Symbol::intern(&capture.to_string(tcx)), capture.get_path_span(tcx)),
495+
);
492496
}
493497
}
494498

@@ -498,7 +502,7 @@ impl<'tcx> PlaceSet<'tcx> {
498502
&& let Some(index) = self.locals[place.local]
499503
{
500504
self.names.get_or_insert_with(index, || {
501-
(var_debug_info.name.to_string(), var_debug_info.source_info.span)
505+
(var_debug_info.name, var_debug_info.source_info.span)
502506
});
503507
}
504508
}
@@ -790,8 +794,8 @@ impl AssignmentResult {
790794
continue;
791795
}
792796

793-
let Some((ref name, def_span)) = checked_places.names[index] else { continue };
794-
if name.is_empty() || name.starts_with('_') || name == "self" {
797+
let Some((name, def_span)) = checked_places.names[index] else { continue };
798+
if name.is_empty() || name.as_str().starts_with('_') || name == kw::SelfLower {
795799
continue;
796800
}
797801

@@ -818,19 +822,16 @@ impl AssignmentResult {
818822
let statements = &mut self.assignments[index];
819823
if statements.is_empty() {
820824
let sugg = if from_macro {
821-
errors::UnusedVariableSugg::NoSugg { span: def_span, name: name.clone() }
825+
errors::UnusedVariableSugg::NoSugg { span: def_span, name }
822826
} else {
823-
errors::UnusedVariableSugg::TryPrefix {
824-
spans: vec![def_span],
825-
name: name.clone(),
826-
}
827+
errors::UnusedVariableSugg::TryPrefix { spans: vec![def_span], name }
827828
};
828829
tcx.emit_node_span_lint(
829830
lint::builtin::UNUSED_VARIABLES,
830831
hir_id,
831832
def_span,
832833
errors::UnusedVariable {
833-
name: name.clone(),
834+
name,
834835
string_interp: maybe_suggest_literal_matching_name(body, name),
835836
sugg,
836837
},
@@ -868,7 +869,7 @@ impl AssignmentResult {
868869
lint::builtin::UNUSED_VARIABLES,
869870
hir_id,
870871
def_span,
871-
errors::UnusedVarAssignedOnly { name: name.clone() },
872+
errors::UnusedVarAssignedOnly { name },
872873
);
873874
continue;
874875
}
@@ -880,7 +881,7 @@ impl AssignmentResult {
880881

881882
let sugg = if any_shorthand {
882883
errors::UnusedVariableSugg::TryIgnore {
883-
name: name.clone(),
884+
name,
884885
shorthands: introductions
885886
.iter()
886887
.filter_map(
@@ -897,19 +898,19 @@ impl AssignmentResult {
897898
.collect(),
898899
}
899900
} else if from_macro {
900-
errors::UnusedVariableSugg::NoSugg { span: def_span, name: name.clone() }
901+
errors::UnusedVariableSugg::NoSugg { span: def_span, name }
901902
} else if !introductions.is_empty() {
902-
errors::UnusedVariableSugg::TryPrefix { name: name.clone(), spans: spans.clone() }
903+
errors::UnusedVariableSugg::TryPrefix { name, spans: spans.clone() }
903904
} else {
904-
errors::UnusedVariableSugg::TryPrefix { name: name.clone(), spans: vec![def_span] }
905+
errors::UnusedVariableSugg::TryPrefix { name, spans: vec![def_span] }
905906
};
906907

907908
tcx.emit_node_span_lint(
908909
lint::builtin::UNUSED_VARIABLES,
909910
hir_id,
910911
spans,
911912
errors::UnusedVariable {
912-
name: name.clone(),
913+
name,
913914
string_interp: maybe_suggest_literal_matching_name(body, name),
914915
sugg,
915916
},
@@ -931,8 +932,8 @@ impl AssignmentResult {
931932
continue;
932933
}
933934

934-
let Some((ref name, decl_span)) = checked_places.names[index] else { continue };
935-
if name.is_empty() || name.starts_with('_') || name == "self" {
935+
let Some((name, decl_span)) = checked_places.names[index] else { continue };
936+
if name.is_empty() || name.as_str().starts_with('_') || name == kw::SelfLower {
936937
continue;
937938
}
938939

@@ -967,24 +968,20 @@ impl AssignmentResult {
967968
lint::builtin::UNUSED_ASSIGNMENTS,
968969
hir_id,
969970
source_info.span,
970-
errors::UnusedAssign {
971-
name: name.clone(),
972-
help: suggestion.is_none(),
973-
suggestion,
974-
},
971+
errors::UnusedAssign { name, help: suggestion.is_none(), suggestion },
975972
)
976973
}
977974
AccessKind::Param => tcx.emit_node_span_lint(
978975
lint::builtin::UNUSED_ASSIGNMENTS,
979976
hir_id,
980977
source_info.span,
981-
errors::UnusedAssignPassed { name: name.clone() },
978+
errors::UnusedAssignPassed { name },
982979
),
983980
AccessKind::Capture => tcx.emit_node_span_lint(
984981
lint::builtin::UNUSED_ASSIGNMENTS,
985982
hir_id,
986983
decl_span,
987-
errors::UnusedCaptureMaybeCaptureRef { name: name.clone() },
984+
errors::UnusedCaptureMaybeCaptureRef { name },
988985
),
989986
}
990987
}

0 commit comments

Comments
 (0)