Skip to content

Commit 8b80390

Browse files
committed
Auto merge of #53927 - ljedrz:save_analysis_cleanups, r=oli-obk
A few cleanups and minor improvements to save_analysis - calculate the capacity of some `Vec`s - change`to_owned()` to `clone()` for the purposes of `lower_attributes` - remove a superfluous `clone()` - prefer `to_owned()` to `to_string()` - a few other minor improvements
2 parents c7fc1a5 + 9883dd7 commit 8b80390

File tree

4 files changed

+16
-23
lines changed

4 files changed

+16
-23
lines changed

src/librustc_save_analysis/dump_visitor.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
147147
let crate_root = source_file.map(|source_file| {
148148
let source_file = Path::new(source_file);
149149
match source_file.file_name() {
150-
Some(_) => source_file.parent().unwrap().display().to_string(),
151-
None => source_file.display().to_string(),
152-
}
150+
Some(_) => source_file.parent().unwrap().display(),
151+
None => source_file.display(),
152+
}.to_string()
153153
});
154154

155155
let data = CratePreludeData {
@@ -176,8 +176,8 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
176176
let segments = &path.segments[if path.is_global() { 1 } else { 0 }..];
177177

178178
let mut result = Vec::with_capacity(segments.len());
179+
let mut segs = Vec::with_capacity(segments.len());
179180

180-
let mut segs = vec![];
181181
for (i, seg) in segments.iter().enumerate() {
182182
segs.push(seg.clone());
183183
let sub_path = ast::Path {
@@ -591,9 +591,7 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
591591

592592
for variant in &enum_definition.variants {
593593
let name = variant.node.ident.name.to_string();
594-
let mut qualname = enum_data.qualname.clone();
595-
qualname.push_str("::");
596-
qualname.push_str(&name);
594+
let qualname = format!("{}::{}", enum_data.qualname, name);
597595

598596
match variant.node.data {
599597
ast::VariantData::Struct(ref fields, _) => {
@@ -973,9 +971,9 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
973971
match self.save_ctxt.get_path_def(id) {
974972
HirDef::Local(id) => {
975973
let mut value = if immut == ast::Mutability::Immutable {
976-
self.span.snippet(ident.span).to_string()
974+
self.span.snippet(ident.span)
977975
} else {
978-
"<mutable>".to_string()
976+
"<mutable>".to_owned()
979977
};
980978
let hir_id = self.tcx.hir.node_to_hir_id(id);
981979
let typ = self.save_ctxt
@@ -1103,10 +1101,9 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
11031101
/// mac_uses and mac_defs sets to prevent multiples.
11041102
fn process_macro_use(&mut self, span: Span) {
11051103
let source_span = span.source_callsite();
1106-
if self.macro_calls.contains(&source_span) {
1104+
if !self.macro_calls.insert(source_span) {
11071105
return;
11081106
}
1109-
self.macro_calls.insert(source_span);
11101107

11111108
let data = match self.save_ctxt.get_macro_use_data(span) {
11121109
None => return,
@@ -1608,8 +1605,7 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tc
16081605
}
16091606
}
16101607
ast::ExprKind::Closure(_, _, _, ref decl, ref body, _fn_decl_span) => {
1611-
let mut id = String::from("$");
1612-
id.push_str(&ex.id.to_string());
1608+
let id = format!("${}", ex.id);
16131609

16141610
// walk arg and return types
16151611
for arg in &decl.inputs {

src/librustc_save_analysis/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
101101
let end = cm.lookup_char_pos(span.hi());
102102

103103
SpanData {
104-
file_name: start.file.name.clone().to_string().into(),
104+
file_name: start.file.name.to_string().into(),
105105
byte_start: span.lo().0,
106106
byte_end: span.hi().0,
107107
line_start: Row::new_one_indexed(start.line as u32),
@@ -113,7 +113,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
113113

114114
// List external crates used by the current crate.
115115
pub fn get_external_crates(&self) -> Vec<ExternalCrateData> {
116-
let mut result = Vec::new();
116+
let mut result = Vec::with_capacity(self.tcx.crates().len());
117117

118118
for &n in self.tcx.crates().iter() {
119119
let span = match *self.tcx.extern_crate(n.as_def_id()) {
@@ -321,7 +321,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
321321
decl_id: None,
322322
docs: self.docs_for_attrs(&item.attrs),
323323
sig: sig::item_signature(item, self),
324-
attributes: lower_attributes(item.attrs.to_owned(), self),
324+
attributes: lower_attributes(item.attrs.clone(), self),
325325
}))
326326
}
327327
ast::ItemKind::Impl(.., ref trait_ref, ref typ, ref impls) => {

src/librustc_save_analysis/sig.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ impl Sig for ast::Item {
435435
},
436436
];
437437
text.push_str(&name);
438-
// Could be either `mod foo;` or `mod foo { ... }`, but we'll just puck one.
438+
// Could be either `mod foo;` or `mod foo { ... }`, but we'll just pick one.
439439
text.push(';');
440440

441441
Ok(Signature {
@@ -630,7 +630,7 @@ impl Sig for ast::Generics {
630630

631631
let mut text = "<".to_owned();
632632

633-
let mut defs = vec![];
633+
let mut defs = Vec::with_capacity(self.params.len());
634634
for param in &self.params {
635635
let mut param_text = param.ident.to_string();
636636
defs.push(SigElement {

src/librustc_save_analysis/span_utils.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,8 @@ impl<'a> SpanUtils<'a> {
263263
/// such as references to macro internal variables.
264264
pub fn filter_generated(&self, sub_span: Option<Span>, parent: Span) -> bool {
265265
if !generated_code(parent) {
266-
if sub_span.is_none() {
267-
// Edge case - this occurs on generated code with incorrect expansion info.
268-
return true;
269-
}
270-
return false;
266+
// Edge case - this occurs on generated code with incorrect expansion info.
267+
return sub_span.is_none()
271268
}
272269
// If sub_span is none, filter out generated code.
273270
let sub_span = match sub_span {

0 commit comments

Comments
 (0)