Skip to content

Commit acd38f6

Browse files
committed
Improve a few vectors - calculate capacity or build from iterators
1 parent 1398572 commit acd38f6

File tree

6 files changed

+14
-21
lines changed

6 files changed

+14
-21
lines changed

src/librustc/hir/map/hir_id_validator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl<'a, 'hir: 'a> HirIdValidator<'a, 'hir> {
105105
.collect();
106106

107107
// Try to map those to something more useful
108-
let mut missing_items = vec![];
108+
let mut missing_items = Vec::with_capacity(missing.len());
109109

110110
for local_id in missing {
111111
let hir_id = HirId {

src/librustc/infer/outlives/obligations.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -505,11 +505,7 @@ where
505505
}
506506

507507
fn recursive_type_bound(&self, ty: Ty<'tcx>) -> VerifyBound<'tcx> {
508-
let mut bounds = vec![];
509-
510-
for subty in ty.walk_shallow() {
511-
bounds.push(self.type_bound(subty));
512-
}
508+
let mut bounds = ty.walk_shallow().map(|subty| self.type_bound(subty)).collect::<Vec<_>>();
513509

514510
let mut regions = ty.regions();
515511
regions.retain(|r| !r.is_late_bound()); // ignore late-bound regions

src/librustc/lint/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ impl LintStore {
219219
}
220220
}
221221

222-
let mut future_incompatible = vec![];
222+
let mut future_incompatible = Vec::with_capacity(lints.len());
223223
for lint in lints {
224224
future_incompatible.push(lint.id);
225225
self.future_incompatible.insert(lint.id, lint);

src/librustc/traits/object_safety.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
9898
pub fn astconv_object_safety_violations(self, trait_def_id: DefId)
9999
-> Vec<ObjectSafetyViolation>
100100
{
101-
let mut violations = vec![];
102-
103-
for def_id in traits::supertrait_def_ids(self, trait_def_id) {
104-
if self.predicates_reference_self(def_id, true) {
105-
violations.push(ObjectSafetyViolation::SupertraitSelf);
106-
}
107-
}
101+
let violations = traits::supertrait_def_ids(self, trait_def_id)
102+
.filter(|&def_id| self.predicates_reference_self(def_id, true))
103+
.map(|_| ObjectSafetyViolation::SupertraitSelf)
104+
.collect();
108105

109106
debug!("astconv_object_safety_violations(trait_def_id={:?}) = {:?}",
110107
trait_def_id,

src/librustc_codegen_llvm/back/link.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use std::env;
4040
use std::fmt;
4141
use std::fs;
4242
use std::io;
43+
use std::iter;
4344
use std::path::{Path, PathBuf};
4445
use std::process::{Output, Stdio};
4546
use std::str;
@@ -885,9 +886,9 @@ fn exec_linker(sess: &Session, cmd: &mut Command, out_filename: &Path, tmpdir: &
885886
}
886887
let file = tmpdir.join("linker-arguments");
887888
let bytes = if sess.target.target.options.is_like_msvc {
888-
let mut out = vec![];
889+
let mut out = Vec::with_capacity((1 + args.len()) * 2);
889890
// start the stream with a UTF-16 BOM
890-
for c in vec![0xFEFF].into_iter().chain(args.encode_utf16()) {
891+
for c in iter::once(0xFEFF).chain(args.encode_utf16()) {
891892
// encode in little endian
892893
out.push(c as u8);
893894
out.push((c >> 8) as u8);

src/libsyntax_pos/lib.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -634,15 +634,14 @@ impl MultiSpan {
634634
/// `SpanLabel` instances with empty labels.
635635
pub fn span_labels(&self) -> Vec<SpanLabel> {
636636
let is_primary = |span| self.primary_spans.contains(&span);
637-
let mut span_labels = vec![];
638637

639-
for &(span, ref label) in &self.span_labels {
640-
span_labels.push(SpanLabel {
638+
let mut span_labels = self.span_labels.iter().map(|&(span, ref label)|
639+
SpanLabel {
641640
span,
642641
is_primary: is_primary(span),
643642
label: Some(label.clone())
644-
});
645-
}
643+
}
644+
).collect::<Vec<_>>();
646645

647646
for &span in &self.primary_spans {
648647
if !span_labels.iter().any(|sl| sl.span == span) {

0 commit comments

Comments
 (0)