Skip to content

Commit 712e4d9

Browse files
committed
Use Symbol instead of strings.
1 parent 2c51f5a commit 712e4d9

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
@@ -15,7 +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::sym;
18+
use rustc_span::symbol::{Symbol, kw, sym};
1919

2020
use crate::errors;
2121

@@ -162,7 +162,7 @@ fn is_capture(place: PlaceRef<'_>) -> bool {
162162
/// interpolate our dead local.
163163
fn maybe_suggest_literal_matching_name(
164164
body: &Body<'_>,
165-
name: &str,
165+
name: Symbol,
166166
) -> Vec<errors::UnusedVariableStringInterp> {
167167
struct LiteralFinder<'body, 'tcx> {
168168
body: &'body Body<'tcx>,
@@ -426,7 +426,7 @@ fn find_self_assignments<'tcx>(
426426
#[derive(Default, Debug)]
427427
struct PlaceSet<'tcx> {
428428
places: IndexVec<PlaceIndex, PlaceRef<'tcx>>,
429-
names: IndexVec<PlaceIndex, Option<(String, Span)>>,
429+
names: IndexVec<PlaceIndex, Option<(Symbol, Span)>>,
430430

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

489489
// Record a variable name from the capture, because it is much friendlier than the
490490
// debuginfo name.
491-
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+
);
492495
}
493496
}
494497

@@ -498,7 +501,7 @@ impl<'tcx> PlaceSet<'tcx> {
498501
&& let Some(index) = self.locals[place.local]
499502
{
500503
self.names.get_or_insert_with(index, || {
501-
(var_debug_info.name.to_string(), var_debug_info.source_info.span)
504+
(var_debug_info.name, var_debug_info.source_info.span)
502505
});
503506
}
504507
}
@@ -790,8 +793,8 @@ impl AssignmentResult {
790793
continue;
791794
}
792795

793-
let Some((ref name, def_span)) = checked_places.names[index] else { continue };
794-
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 {
795798
continue;
796799
}
797800

@@ -818,19 +821,16 @@ impl AssignmentResult {
818821
let statements = &mut self.assignments[index];
819822
if statements.is_empty() {
820823
let sugg = if from_macro {
821-
errors::UnusedVariableSugg::NoSugg { span: def_span, name: name.clone() }
824+
errors::UnusedVariableSugg::NoSugg { span: def_span, name }
822825
} else {
823-
errors::UnusedVariableSugg::TryPrefix {
824-
spans: vec![def_span],
825-
name: name.clone(),
826-
}
826+
errors::UnusedVariableSugg::TryPrefix { spans: vec![def_span], name }
827827
};
828828
tcx.emit_node_span_lint(
829829
lint::builtin::UNUSED_VARIABLES,
830830
hir_id,
831831
def_span,
832832
errors::UnusedVariable {
833-
name: name.clone(),
833+
name,
834834
string_interp: maybe_suggest_literal_matching_name(body, name),
835835
sugg,
836836
},
@@ -868,7 +868,7 @@ impl AssignmentResult {
868868
lint::builtin::UNUSED_VARIABLES,
869869
hir_id,
870870
def_span,
871-
errors::UnusedVarAssignedOnly { name: name.clone() },
871+
errors::UnusedVarAssignedOnly { name },
872872
);
873873
continue;
874874
}
@@ -880,7 +880,7 @@ impl AssignmentResult {
880880

881881
let sugg = if any_shorthand {
882882
errors::UnusedVariableSugg::TryIgnore {
883-
name: name.clone(),
883+
name,
884884
shorthands: introductions
885885
.iter()
886886
.filter_map(
@@ -897,19 +897,19 @@ impl AssignmentResult {
897897
.collect(),
898898
}
899899
} else if from_macro {
900-
errors::UnusedVariableSugg::NoSugg { span: def_span, name: name.clone() }
900+
errors::UnusedVariableSugg::NoSugg { span: def_span, name }
901901
} else if !introductions.is_empty() {
902-
errors::UnusedVariableSugg::TryPrefix { name: name.clone(), spans: spans.clone() }
902+
errors::UnusedVariableSugg::TryPrefix { name, spans: spans.clone() }
903903
} else {
904-
errors::UnusedVariableSugg::TryPrefix { name: name.clone(), spans: vec![def_span] }
904+
errors::UnusedVariableSugg::TryPrefix { name, spans: vec![def_span] }
905905
};
906906

907907
tcx.emit_node_span_lint(
908908
lint::builtin::UNUSED_VARIABLES,
909909
hir_id,
910910
spans,
911911
errors::UnusedVariable {
912-
name: name.clone(),
912+
name,
913913
string_interp: maybe_suggest_literal_matching_name(body, name),
914914
sugg,
915915
},
@@ -931,8 +931,8 @@ impl AssignmentResult {
931931
continue;
932932
}
933933

934-
let Some((ref name, decl_span)) = checked_places.names[index] else { continue };
935-
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 {
936936
continue;
937937
}
938938

@@ -967,24 +967,20 @@ impl AssignmentResult {
967967
lint::builtin::UNUSED_ASSIGNMENTS,
968968
hir_id,
969969
source_info.span,
970-
errors::UnusedAssign {
971-
name: name.clone(),
972-
help: suggestion.is_none(),
973-
suggestion,
974-
},
970+
errors::UnusedAssign { name, help: suggestion.is_none(), suggestion },
975971
)
976972
}
977973
AccessKind::Param => tcx.emit_node_span_lint(
978974
lint::builtin::UNUSED_ASSIGNMENTS,
979975
hir_id,
980976
source_info.span,
981-
errors::UnusedAssignPassed { name: name.clone() },
977+
errors::UnusedAssignPassed { name },
982978
),
983979
AccessKind::Capture => tcx.emit_node_span_lint(
984980
lint::builtin::UNUSED_ASSIGNMENTS,
985981
hir_id,
986982
decl_span,
987-
errors::UnusedCaptureMaybeCaptureRef { name: name.clone() },
983+
errors::UnusedCaptureMaybeCaptureRef { name },
988984
),
989985
}
990986
}

0 commit comments

Comments
 (0)