Skip to content

Commit

Permalink
Replace push loops with collect() and extend() where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
ljedrz committed Jul 29, 2018
1 parent a5c2d0f commit 59c8a27
Show file tree
Hide file tree
Showing 28 changed files with 101 additions and 150 deletions.
5 changes: 1 addition & 4 deletions src/bootstrap/util.rs
Expand Up @@ -92,10 +92,7 @@ pub fn push_exe_path(mut buf: PathBuf, components: &[&str]) -> PathBuf {
file.push_str(".exe");
}

for c in components {
buf.push(c);
}

buf.extend(components);
buf.push(file);

buf
Expand Down
10 changes: 5 additions & 5 deletions src/librustc/cfg/construct.rs
Expand Up @@ -567,12 +567,12 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
fn add_returning_edge(&mut self,
_from_expr: &hir::Expr,
from_index: CFGIndex) {
let mut data = CFGEdgeData {
exiting_scopes: vec![],
let data = CFGEdgeData {
exiting_scopes: self.loop_scopes.iter()
.rev()
.map(|&LoopScope { loop_id: id, .. }| id)
.collect()
};
for &LoopScope { loop_id: id, .. } in self.loop_scopes.iter().rev() {
data.exiting_scopes.push(id);
}
self.graph.add_edge(from_index, self.fn_exit, data);
}

Expand Down
11 changes: 5 additions & 6 deletions src/librustc/infer/outlives/obligations.rs
Expand Up @@ -151,13 +151,12 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
debug!("process_registered_region_obligations()");

// pull out the region obligations with the given `body_id` (leaving the rest)
let mut my_region_obligations = Vec::with_capacity(self.region_obligations.borrow().len());
{
let my_region_obligations = {
let mut r_o = self.region_obligations.borrow_mut();
for (_, obligation) in r_o.drain_filter(|(ro_body_id, _)| *ro_body_id == body_id) {
my_region_obligations.push(obligation);
}
}
let my_r_o = r_o.drain_filter(|(ro_body_id, _)| *ro_body_id == body_id)
.map(|(_, obligation)| obligation).collect::<Vec<_>>();
my_r_o
};

let outlives = &mut TypeOutlives::new(
self,
Expand Down
8 changes: 2 additions & 6 deletions src/librustc/middle/reachable.rs
Expand Up @@ -367,9 +367,7 @@ impl<'a, 'tcx: 'a> ItemLikeVisitor<'tcx> for CollectPrivateImplItemsVisitor<'a,
// We need only trait impls here, not inherent impls, and only non-exported ones
if let hir::ItemKind::Impl(.., Some(ref trait_ref), _, ref impl_item_refs) = item.node {
if !self.access_levels.is_reachable(item.id) {
for impl_item_ref in impl_item_refs {
self.worklist.push(impl_item_ref.id.node_id);
}
self.worklist.extend(impl_item_refs.iter().map(|r| r.id.node_id));

let trait_def_id = match trait_ref.path.def {
Def::Trait(def_id) => def_id,
Expand Down Expand Up @@ -426,9 +424,7 @@ fn reachable_set<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateNum) ->
// If other crates link to us, they're going to expect to be able to
// use the lang items, so we need to be sure to mark them as
// exported.
for (id, _) in &access_levels.map {
reachable_context.worklist.push(*id);
}
reachable_context.worklist.extend(access_levels.map.iter().map(|(id, _)| *id));
for item in tcx.lang_items().items().iter() {
if let Some(did) = *item {
if let Some(node_id) = tcx.hir.as_local_node_id(did) {
Expand Down
4 changes: 1 addition & 3 deletions src/librustc/mir/traversal.rs
Expand Up @@ -64,9 +64,7 @@ impl<'a, 'tcx> Iterator for Preorder<'a, 'tcx> {
let data = &self.mir[idx];

if let Some(ref term) = data.terminator {
for &succ in term.successors() {
self.worklist.push(succ);
}
self.worklist.extend(term.successors());
}

return Some((idx, data));
Expand Down
4 changes: 1 addition & 3 deletions src/librustc/session/config.rs
Expand Up @@ -899,9 +899,7 @@ macro_rules! options {
-> bool {
match v {
Some(s) => {
for s in s.split_whitespace() {
slot.push(s.to_string());
}
slot.extend(s.split_whitespace().map(|s| s.to_string()));
true
},
None => false,
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/traits/specialize/mod.rs
Expand Up @@ -438,9 +438,9 @@ fn to_pretty_impl_header(tcx: TyCtxt, impl_def_id: DefId) -> Option<String> {
}
pretty_predicates.push(p.to_string());
}
for ty in types_without_default_bounds {
pretty_predicates.push(format!("{}: ?Sized", ty));
}
pretty_predicates.extend(
types_without_default_bounds.iter().map(|ty| format!("{}: ?Sized", ty))
);
if !pretty_predicates.is_empty() {
write!(w, "\n where {}", pretty_predicates.join(", ")).unwrap();
}
Expand Down
4 changes: 1 addition & 3 deletions src/librustc_codegen_llvm/back/rpath.rs
Expand Up @@ -152,9 +152,7 @@ fn path_relative_from(path: &Path, base: &Path) -> Option<PathBuf> {
(Some(_), Some(b)) if b == Component::ParentDir => return None,
(Some(a), Some(_)) => {
comps.push(Component::ParentDir);
for _ in itb {
comps.push(Component::ParentDir);
}
comps.extend(itb.map(|_| Component::ParentDir));
comps.push(a);
comps.extend(ita.by_ref());
break;
Expand Down
23 changes: 11 additions & 12 deletions src/librustc_codegen_llvm/debuginfo/metadata.rs
Expand Up @@ -39,6 +39,7 @@ use rustc::util::common::path2cstr;
use libc::{c_uint, c_longlong};
use std::ffi::CString;
use std::fmt::Write;
use std::iter;
use std::ptr;
use std::path::{Path, PathBuf};
use syntax::ast;
Expand Down Expand Up @@ -364,18 +365,16 @@ fn subroutine_type_metadata<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
&signature,
);

let mut signature_metadata: Vec<DIType> = Vec::with_capacity(signature.inputs().len() + 1);

// return type
signature_metadata.push(match signature.output().sty {
ty::TyTuple(ref tys) if tys.is_empty() => ptr::null_mut(),
_ => type_metadata(cx, signature.output(), span)
});

// regular arguments
for &argument_type in signature.inputs() {
signature_metadata.push(type_metadata(cx, argument_type, span));
}
let signature_metadata: Vec<DIType> = iter::once(
// return type
match signature.output().sty {
ty::TyTuple(ref tys) if tys.is_empty() => ptr::null_mut(),
_ => type_metadata(cx, signature.output(), span)
}
).chain(
// regular arguments
signature.inputs().iter().map(|argument_type| type_metadata(cx, argument_type, span))
).collect();

return_if_metadata_created_in_meantime!(cx, unique_type_id);

Expand Down
7 changes: 4 additions & 3 deletions src/librustc_codegen_llvm/debuginfo/mod.rs
Expand Up @@ -352,9 +352,10 @@ pub fn create_function_debug_context<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,

if sig.abi == Abi::RustCall && !sig.inputs().is_empty() {
if let ty::TyTuple(args) = sig.inputs()[sig.inputs().len() - 1].sty {
for &argument_type in args {
signature.push(type_metadata(cx, argument_type, syntax_pos::DUMMY_SP));
}
signature.extend(
args.iter().map(|argument_type|
type_metadata(cx, argument_type, syntax_pos::DUMMY_SP))
);
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/librustc_driver/lib.rs
Expand Up @@ -1588,10 +1588,7 @@ pub fn in_rustc_thread<F, R>(f: F) -> Result<R, Box<dyn Any + Send>>
/// debugging, since some ICEs only happens with non-default compiler flags
/// (and the users don't always report them).
fn extra_compiler_flags() -> Option<(Vec<String>, bool)> {
let mut args = Vec::new();
for arg in env::args_os() {
args.push(arg.to_string_lossy().to_string());
}
let args = env::args_os().map(|arg| arg.to_string_lossy().to_string()).collect::<Vec<_>>();

// Avoid printing help because of empty args. This can suggest the compiler
// itself is not the program root (consider RLS).
Expand Down
7 changes: 3 additions & 4 deletions src/librustc_driver/profile/trace.rs
Expand Up @@ -204,10 +204,9 @@ pub fn write_counts(count_file: &mut File, counts: &mut HashMap<String,QueryMetr
use rustc::util::common::duration_to_secs_str;
use std::cmp::Reverse;

let mut data = vec![];
for (ref cons, ref qm) in counts.iter() {
data.push((cons.clone(), qm.count.clone(), qm.dur_total.clone(), qm.dur_self.clone()));
};
let mut data = counts.iter().map(|(ref cons, ref qm)|
(cons.clone(), qm.count.clone(), qm.dur_total.clone(), qm.dur_self.clone())
).collect::<Vec<_>>();
data.sort_by_key(|k| Reverse(k.3));
for (cons, count, dur_total, dur_self) in data {
write!(count_file, "{}, {}, {}, {}\n",
Expand Down
6 changes: 1 addition & 5 deletions src/librustc_llvm/lib.rs
Expand Up @@ -275,12 +275,8 @@ pub fn get_param(llfn: ValueRef, index: c_uint) -> ValueRef {
fn get_params(llfn: ValueRef) -> Vec<ValueRef> {
unsafe {
let num_params = LLVMCountParams(llfn);
let mut params = Vec::with_capacity(num_params as usize);
for idx in 0..num_params {
params.push(LLVMGetParam(llfn, idx));
}

params
(0..num_params).map(|idx| LLVMGetParam(llfn, idx)).collect()
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/librustc_mir/transform/inline.rs
Expand Up @@ -406,10 +406,9 @@ impl<'a, 'tcx> Inliner<'a, 'tcx> {
local_map.push(idx);
}

for p in callee_mir.promoted.iter().cloned() {
let idx = caller_mir.promoted.push(p);
promoted_map.push(idx);
}
promoted_map.extend(
callee_mir.promoted.iter().cloned().map(|p| caller_mir.promoted.push(p))
);

// If the call is something like `a[*i] = f(i)`, where
// `i : &mut usize`, then just duplicating the `a[*i]`
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_resolve/lib.rs
Expand Up @@ -3831,9 +3831,9 @@ impl<'a> Resolver<'a> {
}
// Add primitive types to the mix
if filter_fn(Def::PrimTy(TyBool)) {
for (name, _) in &self.primitive_type_table.primitive_types {
names.push(*name);
}
names.extend(
self.primitive_type_table.primitive_types.iter().map(|(name, _)| name)
)
}
} else {
// Search in module.
Expand Down
11 changes: 5 additions & 6 deletions src/librustc_save_analysis/dump_visitor.rs
Expand Up @@ -1318,14 +1318,13 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
};

// Make a comma-separated list of names of imported modules.
let mut names = vec![];
let glob_map = &self.save_ctxt.analysis.glob_map;
let glob_map = glob_map.as_ref().unwrap();
if glob_map.contains_key(&id) {
for n in glob_map.get(&id).unwrap() {
names.push(n.to_string());
}
}
let names = if glob_map.contains_key(&id) {
glob_map.get(&id).unwrap().iter().map(|n| n.to_string()).collect()
} else {
Vec::new()
};

let sub_span = self.span.sub_span_of_token(use_tree.span,
token::BinOp(token::Star));
Expand Down
33 changes: 16 additions & 17 deletions src/librustc_typeck/collect.rs
Expand Up @@ -962,19 +962,21 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
&["<closure_kind>", "<closure_signature>"][..]
};

for (i, &arg) in dummy_args.iter().enumerate() {
params.push(ty::GenericParamDef {
index: type_start + i as u32,
name: Symbol::intern(arg).as_interned_str(),
def_id,
pure_wrt_drop: false,
kind: ty::GenericParamDefKind::Type {
has_default: false,
object_lifetime_default: rl::Set1::Empty,
synthetic: None,
},
});
}
params.extend(
dummy_args.iter().enumerate().map(|(i, &arg)|
ty::GenericParamDef {
index: type_start + i as u32,
name: Symbol::intern(arg).as_interned_str(),
def_id,
pure_wrt_drop: false,
kind: ty::GenericParamDefKind::Type {
has_default: false,
object_lifetime_default: rl::Set1::Empty,
synthetic: None,
},
}
)
);

tcx.with_freevars(node_id, |fv| {
params.extend(fv.iter().zip((dummy_args.len() as u32)..).map(|(_, i)| {
Expand Down Expand Up @@ -1651,10 +1653,7 @@ fn explicit_predicates_of<'a, 'tcx>(
&mut projections);

predicates.push(trait_ref.to_predicate());

for projection in &projections {
predicates.push(projection.to_predicate());
}
predicates.extend(projections.iter().map(|p| p.to_predicate()));
}

&hir::GenericBound::Outlives(ref lifetime) => {
Expand Down
4 changes: 1 addition & 3 deletions src/librustdoc/core.rs
Expand Up @@ -203,9 +203,7 @@ pub fn run_core(search_paths: SearchPaths,
intra_link_resolution_failure_name.to_owned(),
missing_docs.to_owned()];

for (lint, _) in &cmd_lints {
whitelisted_lints.push(lint.clone());
}
whitelisted_lints.extend(cmd_lints.iter().map(|(lint, _)| lint).cloned());

let lints = lint::builtin::HardwiredLints.get_lints()
.into_iter()
Expand Down
4 changes: 1 addition & 3 deletions src/librustdoc/lib.rs
Expand Up @@ -722,9 +722,7 @@ where R: 'static + Send,
},
_ => continue,
};
for p in value.as_str().split_whitespace() {
sink.push(p.to_string());
}
sink.extend(value.as_str().split_whitespace().map(|p| p.to_string()));
}

if attr.is_word() && name == Some("document_private_items") {
Expand Down
12 changes: 3 additions & 9 deletions src/libserialize/json.rs
Expand Up @@ -1364,9 +1364,7 @@ impl Stack {
// Used by Parser to insert StackElement::Key elements at the top of the stack.
fn push_key(&mut self, key: string::String) {
self.stack.push(InternalKey(self.str_buffer.len() as u16, key.len() as u16));
for c in key.as_bytes() {
self.str_buffer.push(*c);
}
self.str_buffer.extend(key.as_bytes());
}

// Used by Parser to insert StackElement::Index elements at the top of the stack.
Expand Down Expand Up @@ -2212,9 +2210,7 @@ impl ::Decoder for Decoder {
};
match o.remove(&"fields".to_string()) {
Some(Json::Array(l)) => {
for field in l.into_iter().rev() {
self.stack.push(field);
}
self.stack.extend(l.into_iter().rev());
},
Some(val) => {
return Err(ExpectedError("Array".to_owned(), format!("{}", val)))
Expand Down Expand Up @@ -2346,9 +2342,7 @@ impl ::Decoder for Decoder {
{
let array = expect!(self.pop(), Array)?;
let len = array.len();
for v in array.into_iter().rev() {
self.stack.push(v);
}
self.stack.extend(array.into_iter().rev());
f(self, len)
}

Expand Down
11 changes: 6 additions & 5 deletions src/libstd/sys/redox/process.rs
Expand Up @@ -13,6 +13,7 @@ use ffi::OsStr;
use os::unix::ffi::OsStrExt;
use fmt;
use io::{self, Error, ErrorKind};
use iter;
use libc::{EXIT_SUCCESS, EXIT_FAILURE};
use path::{Path, PathBuf};
use sys::fd::FileDesc;
Expand Down Expand Up @@ -296,11 +297,11 @@ impl Command {
t!(callback());
}

let mut args: Vec<[usize; 2]> = Vec::new();
args.push([self.program.as_ptr() as usize, self.program.len()]);
for arg in self.args.iter() {
args.push([arg.as_ptr() as usize, arg.len()]);
}
let args: Vec<[usize; 2]> = iter::once(
[self.program.as_ptr() as usize, self.program.len()]
).chain(
self.args.iter().map(|arg| [arg.as_ptr() as usize, arg.len()])
).collect();

self.env.apply();

Expand Down

0 comments on commit 59c8a27

Please sign in to comment.