Skip to content

Commit 22624f7

Browse files
committed
Use Symbol instead of strings.
1 parent 4bbab5c commit 22624f7

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
@@ -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 & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_mir_dataflow::fmt::DebugWithContext;
1515
use rustc_mir_dataflow::{Analysis, Backward, ResultsCursor};
1616
use rustc_session::lint;
1717
use rustc_span::Span;
18+
use rustc_span::symbol::{Symbol, kw};
1819

1920
use crate::errors;
2021

@@ -161,7 +162,7 @@ fn is_capture(place: PlaceRef<'_>) -> bool {
161162
/// interpolate our dead local.
162163
fn maybe_suggest_literal_matching_name(
163164
body: &Body<'_>,
164-
name: &str,
165+
name: Symbol,
165166
) -> Vec<errors::UnusedVariableStringInterp> {
166167
struct LiteralFinder<'body, 'tcx> {
167168
body: &'body Body<'tcx>,
@@ -425,7 +426,7 @@ fn find_self_assignments<'tcx>(
425426
#[derive(Default, Debug)]
426427
struct PlaceSet<'tcx> {
427428
places: IndexVec<PlaceIndex, PlaceRef<'tcx>>,
428-
names: IndexVec<PlaceIndex, Option<(String, Span)>>,
429+
names: IndexVec<PlaceIndex, Option<(Symbol, Span)>>,
429430

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

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

@@ -497,7 +501,7 @@ impl<'tcx> PlaceSet<'tcx> {
497501
&& let Some(index) = self.locals[place.local]
498502
{
499503
self.names.get_or_insert_with(index, || {
500-
(var_debug_info.name.to_string(), var_debug_info.source_info.span)
504+
(var_debug_info.name, var_debug_info.source_info.span)
501505
});
502506
}
503507
}
@@ -789,8 +793,8 @@ impl AssignmentResult {
789793
continue;
790794
}
791795

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

@@ -817,19 +821,16 @@ impl AssignmentResult {
817821
let statements = &mut self.assignments[index];
818822
if statements.is_empty() {
819823
let sugg = if from_macro {
820-
errors::UnusedVariableSugg::NoSugg { span: def_span, name: name.clone() }
824+
errors::UnusedVariableSugg::NoSugg { span: def_span, name }
821825
} else {
822-
errors::UnusedVariableSugg::TryPrefix {
823-
spans: vec![def_span],
824-
name: name.clone(),
825-
}
826+
errors::UnusedVariableSugg::TryPrefix { spans: vec![def_span], name }
826827
};
827828
tcx.emit_node_span_lint(
828829
lint::builtin::UNUSED_VARIABLES,
829830
hir_id,
830831
def_span,
831832
errors::UnusedVariable {
832-
name: name.clone(),
833+
name,
833834
string_interp: maybe_suggest_literal_matching_name(body, name),
834835
sugg,
835836
},
@@ -867,7 +868,7 @@ impl AssignmentResult {
867868
lint::builtin::UNUSED_VARIABLES,
868869
hir_id,
869870
def_span,
870-
errors::UnusedVarAssignedOnly { name: name.clone() },
871+
errors::UnusedVarAssignedOnly { name },
871872
);
872873
continue;
873874
}
@@ -879,7 +880,7 @@ impl AssignmentResult {
879880

880881
let sugg = if any_shorthand {
881882
errors::UnusedVariableSugg::TryIgnore {
882-
name: name.clone(),
883+
name,
883884
shorthands: introductions
884885
.iter()
885886
.filter_map(
@@ -896,19 +897,19 @@ impl AssignmentResult {
896897
.collect(),
897898
}
898899
} else if from_macro {
899-
errors::UnusedVariableSugg::NoSugg { span: def_span, name: name.clone() }
900+
errors::UnusedVariableSugg::NoSugg { span: def_span, name }
900901
} else if !introductions.is_empty() {
901-
errors::UnusedVariableSugg::TryPrefix { name: name.clone(), spans: spans.clone() }
902+
errors::UnusedVariableSugg::TryPrefix { name, spans: spans.clone() }
902903
} else {
903-
errors::UnusedVariableSugg::TryPrefix { name: name.clone(), spans: vec![def_span] }
904+
errors::UnusedVariableSugg::TryPrefix { name, spans: vec![def_span] }
904905
};
905906

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

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

@@ -966,24 +967,20 @@ impl AssignmentResult {
966967
lint::builtin::UNUSED_ASSIGNMENTS,
967968
hir_id,
968969
source_info.span,
969-
errors::UnusedAssign {
970-
name: name.clone(),
971-
help: suggestion.is_none(),
972-
suggestion,
973-
},
970+
errors::UnusedAssign { name, help: suggestion.is_none(), suggestion },
974971
)
975972
}
976973
AccessKind::Param => tcx.emit_node_span_lint(
977974
lint::builtin::UNUSED_ASSIGNMENTS,
978975
hir_id,
979976
source_info.span,
980-
errors::UnusedAssignPassed { name: name.clone() },
977+
errors::UnusedAssignPassed { name },
981978
),
982979
AccessKind::Capture => tcx.emit_node_span_lint(
983980
lint::builtin::UNUSED_ASSIGNMENTS,
984981
hir_id,
985982
decl_span,
986-
errors::UnusedCaptureMaybeCaptureRef { name: name.clone() },
983+
errors::UnusedCaptureMaybeCaptureRef { name },
987984
),
988985
}
989986
}

0 commit comments

Comments
 (0)