Skip to content

Commit

Permalink
Rollup merge of rust-lang#62249 - czipperz:use-mem-take-instead-of-re…
Browse files Browse the repository at this point in the history
…place-default, r=dtolnay,Centril

Use mem::take instead of mem::replace with default
  • Loading branch information
Centril committed Jul 3, 2019
2 parents 8db468a + eddfad3 commit 04d811e
Show file tree
Hide file tree
Showing 43 changed files with 57 additions and 49 deletions.
4 changes: 2 additions & 2 deletions src/liballoc/collections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,8 +770,8 @@ impl<K: Ord, V> BTreeMap<K, V> {
}

// First, we merge `self` and `other` into a sorted sequence in linear time.
let self_iter = mem::replace(self, BTreeMap::new()).into_iter();
let other_iter = mem::replace(other, BTreeMap::new()).into_iter();
let self_iter = mem::take(self).into_iter();
let other_iter = mem::take(other).into_iter();
let iter = MergeIter {
left: self_iter.peekable(),
right: other_iter.peekable(),
Expand Down
2 changes: 1 addition & 1 deletion src/liballoc/collections/linked_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ impl<T> LinkedList<T> {
let len = self.len();
assert!(at <= len, "Cannot split off at a nonexistent index");
if at == 0 {
return mem::replace(self, Self::new());
return mem::take(self);
} else if at == len {
return Self::new();
}
Expand Down
1 change: 1 addition & 0 deletions src/liballoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
#![feature(maybe_uninit_extra, maybe_uninit_slice, maybe_uninit_array)]
#![feature(alloc_layout_extra)]
#![feature(try_trait)]
#![feature(mem_take)]

// Allow testing this library

Expand Down
2 changes: 1 addition & 1 deletion src/liballoc/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impl ToOwned for str {
}

fn clone_into(&self, target: &mut String) {
let mut b = mem::replace(target, String::new()).into_bytes();
let mut b = mem::take(target).into_bytes();
self.as_bytes().clone_into(&mut b);
*target = unsafe { String::from_utf8_unchecked(b) }
}
Expand Down
1 change: 1 addition & 0 deletions src/libcore/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
#![feature(adx_target_feature)]
#![feature(maybe_uninit_slice, maybe_uninit_array)]
#![feature(external_doc)]
#![feature(mem_take)]

#[prelude_import]
#[allow(unused)]
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ impl<T> Option<T> {
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub fn take(&mut self) -> Option<T> {
mem::replace(self, None)
mem::take(self)
}

/// Replaces the actual value in the option by the value given in parameter,
Expand Down
2 changes: 1 addition & 1 deletion src/libproc_macro/bridge/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl<T: Copy> Buffer<T> {
}

pub(super) fn take(&mut self) -> Self {
mem::replace(self, Self::default())
mem::take(self)
}

pub(super) fn extend_from_slice(&mut self, xs: &[T]) {
Expand Down
1 change: 1 addition & 0 deletions src/libproc_macro/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#![feature(extern_types)]
#![feature(in_band_lifetimes)]
#![feature(optin_builtin_traits)]
#![feature(mem_take)]
#![feature(non_exhaustive)]
#![feature(specialization)]

Expand Down
4 changes: 2 additions & 2 deletions src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1280,8 +1280,8 @@ impl<'a> LoweringContext<'a> {
let was_in_loop_condition = self.is_in_loop_condition;
self.is_in_loop_condition = false;

let catch_scopes = mem::replace(&mut self.catch_scopes, Vec::new());
let loop_scopes = mem::replace(&mut self.loop_scopes, Vec::new());
let catch_scopes = mem::take(&mut self.catch_scopes);
let loop_scopes = mem::take(&mut self.loop_scopes);
let ret = f(self);
self.catch_scopes = catch_scopes;
self.loop_scopes = loop_scopes;
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/infer/nll_relate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ where
// been fully instantiated and hence the set of scopes we have
// doesn't matter -- just to be sure, put an empty vector
// in there.
let old_a_scopes = ::std::mem::replace(pair.vid_scopes(self), vec![]);
let old_a_scopes = ::std::mem::take(pair.vid_scopes(self));

// Relate the generalized kind to the original one.
let result = pair.relate_generalized_ty(self, generalized_ty);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/infer/outlives/obligations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {

/// Trait queries just want to pass back type obligations "as is"
pub fn take_registered_region_obligations(&self) -> Vec<(hir::HirId, RegionObligation<'tcx>)> {
::std::mem::replace(&mut *self.region_obligations.borrow_mut(), vec![])
::std::mem::take(&mut *self.region_obligations.borrow_mut())
}

/// Process the region obligations that must be proven (during
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/infer/region_constraints/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ impl<'tcx> RegionConstraintCollector<'tcx> {
*any_unifications = false;
}

mem::replace(data, RegionConstraintData::default())
mem::take(data)
}

pub fn data(&self) -> &RegionConstraintData<'tcx> {
Expand Down
1 change: 1 addition & 0 deletions src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
#![feature(crate_visibility_modifier)]
#![feature(proc_macro_hygiene)]
#![feature(log_syntax)]
#![feature(mem_take)]

#![recursion_limit="512"]

Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1375,7 +1375,7 @@ impl<'tcx> Visitor<'tcx> for RegionResolutionVisitor<'tcx> {

let outer_ec = mem::replace(&mut self.expr_and_pat_count, 0);
let outer_cx = self.cx;
let outer_ts = mem::replace(&mut self.terminating_scopes, FxHashSet::default());
let outer_ts = mem::take(&mut self.terminating_scopes);
self.terminating_scopes.insert(body.value.hir_id.local_id);

if let Some(root_id) = self.cx.root_id {
Expand Down
9 changes: 4 additions & 5 deletions src/librustc/middle/resolve_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use errors::{Applicability, DiagnosticBuilder};
use rustc_macros::HashStable;
use std::borrow::Cow;
use std::cell::Cell;
use std::mem::replace;
use std::mem::{replace, take};
use syntax::ast;
use syntax::attr;
use syntax::symbol::{kw, sym};
Expand Down Expand Up @@ -441,7 +441,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {

fn visit_nested_body(&mut self, body: hir::BodyId) {
// Each body has their own set of labels, save labels.
let saved = replace(&mut self.labels_in_fn, vec![]);
let saved = take(&mut self.labels_in_fn);
let body = self.tcx.hir().body(body);
extract_labels(self, body);
self.with(
Expand Down Expand Up @@ -1405,9 +1405,8 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
lifetime_uses,
..
} = self;
let labels_in_fn = replace(&mut self.labels_in_fn, vec![]);
let xcrate_object_lifetime_defaults =
replace(&mut self.xcrate_object_lifetime_defaults, DefIdMap::default());
let labels_in_fn = take(&mut self.labels_in_fn);
let xcrate_object_lifetime_defaults = take(&mut self.xcrate_object_lifetime_defaults);
let mut this = LifetimeContext {
tcx: *tcx,
map: map,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_codegen_llvm/back/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ impl<'a> LlvmArchiveBuilder<'a> {
}

fn build_with_llvm(&mut self, kind: ArchiveKind) -> io::Result<()> {
let removals = mem::replace(&mut self.removals, Vec::new());
let mut additions = mem::replace(&mut self.additions, Vec::new());
let removals = mem::take(&mut self.removals);
let mut additions = mem::take(&mut self.additions);
let mut strings = Vec::new();
let mut members = Vec::new();

Expand Down
1 change: 1 addition & 0 deletions src/librustc_codegen_llvm/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#![feature(link_args)]
#![feature(static_nobundle)]
#![feature(trusted_len)]
#![feature(mem_take)]
#![deny(rust_2018_idioms)]
#![deny(internal)]
#![deny(unused_lifetimes)]
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_ssa/back/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl Command {
}

pub fn take_args(&mut self) -> Vec<OsString> {
mem::replace(&mut self.args, Vec::new())
mem::take(&mut self.args)
}

/// Returns a `true` if we're pretty sure that this'll blow OS spawn limits,
Expand Down
9 changes: 3 additions & 6 deletions src/librustc_codegen_ssa/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1345,12 +1345,9 @@ fn start_executing_work<B: ExtraBackendMethods>(
assert!(!started_lto);
started_lto = true;

let needs_fat_lto =
mem::replace(&mut needs_fat_lto, Vec::new());
let needs_thin_lto =
mem::replace(&mut needs_thin_lto, Vec::new());
let import_only_modules =
mem::replace(&mut lto_import_only_modules, Vec::new());
let needs_fat_lto = mem::take(&mut needs_fat_lto);
let needs_thin_lto = mem::take(&mut needs_thin_lto);
let import_only_modules = mem::take(&mut lto_import_only_modules);

for (work, cost) in generate_lto_work(&cgcx, needs_fat_lto,
needs_thin_lto, import_only_modules) {
Expand Down
1 change: 1 addition & 0 deletions src/librustc_codegen_ssa/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#![feature(in_band_lifetimes)]
#![feature(nll)]
#![feature(trusted_len)]
#![feature(mem_take)]
#![allow(unused_attributes)]
#![allow(dead_code)]
#![deny(rust_2018_idioms)]
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/borrow_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ fn do_mir_borrowck<'a, 'tcx>(
mbcx.analyze_results(&mut state); // entry point for DataflowResultsConsumer

// Convert any reservation warnings into lints.
let reservation_warnings = mem::replace(&mut mbcx.reservation_warnings, Default::default());
let reservation_warnings = mem::take(&mut mbcx.reservation_warnings);
for (_, (place, span, location, bk, borrow)) in reservation_warnings {
let mut initial_diag =
mbcx.report_conflicting_borrow(location, (&place, span), bk, &borrow);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/build/matches/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
candidate: &mut Candidate<'pat, 'tcx>) {
// repeatedly simplify match pairs until fixed point is reached
loop {
let match_pairs = mem::replace(&mut candidate.match_pairs, vec![]);
let match_pairs = mem::take(&mut candidate.match_pairs);
let mut changed = false;
for match_pair in match_pairs {
match self.simplify_match_pair(match_pair, candidate) {
Expand Down
1 change: 1 addition & 0 deletions src/librustc_mir/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Rust MIR: a lowered representation of Rust. Also: an experiment!
#![feature(slice_concat_ext)]
#![feature(trusted_len)]
#![feature(try_blocks)]
#![feature(mem_take)]

#![recursion_limit="256"]

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/util/def_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl DefUseAnalysis {
self.clear();

let mut finder = DefUseFinder {
info: mem::replace(&mut self.info, IndexVec::new()),
info: mem::take(&mut self.info),
};
finder.visit_body(body);
self.info = finder.info
Expand Down
1 change: 1 addition & 0 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#![feature(crate_visibility_modifier)]
#![feature(label_break_value)]
#![feature(mem_take)]
#![feature(nll)]
#![feature(rustc_diagnostic_macros)]
#![cfg_attr(bootstrap, feature(type_alias_enum_variants))]
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_resolve/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ impl<'a> Resolver<'a> {
};

let macro_resolutions =
mem::replace(&mut *module.multi_segment_macro_resolutions.borrow_mut(), Vec::new());
mem::take(&mut *module.multi_segment_macro_resolutions.borrow_mut());
for (mut path, path_span, kind, parent_scope, initial_res) in macro_resolutions {
// FIXME: Path resolution will ICE if segment IDs present.
for seg in &mut path { seg.id = None; }
Expand All @@ -973,7 +973,7 @@ impl<'a> Resolver<'a> {
}

let macro_resolutions =
mem::replace(&mut *module.single_segment_macro_resolutions.borrow_mut(), Vec::new());
mem::take(&mut *module.single_segment_macro_resolutions.borrow_mut());
for (ident, kind, parent_scope, initial_binding) in macro_resolutions {
match self.early_resolve_ident_in_lexical_scope(ident, ScopeSet::Macro(kind),
&parent_scope, true, true, ident.span) {
Expand All @@ -998,7 +998,7 @@ impl<'a> Resolver<'a> {
}
}

let builtin_attrs = mem::replace(&mut *module.builtin_attrs.borrow_mut(), Vec::new());
let builtin_attrs = mem::take(&mut *module.builtin_attrs.borrow_mut());
for (ident, parent_scope) in builtin_attrs {
let _ = self.early_resolve_ident_in_lexical_scope(
ident, ScopeSet::Macro(MacroKind::Attr), &parent_scope, true, true, ident.span
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_resolve/resolve_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
let mut prev_num_indeterminates = self.indeterminate_imports.len() + 1;
while self.indeterminate_imports.len() < prev_num_indeterminates {
prev_num_indeterminates = self.indeterminate_imports.len();
for import in mem::replace(&mut self.indeterminate_imports, Vec::new()) {
for import in mem::take(&mut self.indeterminate_imports) {
match self.resolve_import(&import) {
true => self.determined_imports.push(import),
false => self.indeterminate_imports.push(import),
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_typeck/check/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -970,9 +970,9 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {

debug!("pick: actual search failed, assemble diagnotics");

let static_candidates = mem::replace(&mut self.static_candidates, vec![]);
let static_candidates = mem::take(&mut self.static_candidates);
let private_candidate = self.private_candidate.take();
let unsatisfied_predicates = mem::replace(&mut self.unsatisfied_predicates, vec![]);
let unsatisfied_predicates = mem::take(&mut self.unsatisfied_predicates);

// things failed, so lets look at all traits, for diagnostic purposes now:
self.reset();
Expand Down
1 change: 1 addition & 0 deletions src/librustc_typeck/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ This API is completely unstable and subject to change.
#![feature(slice_patterns)]
#![feature(never_type)]
#![feature(inner_deref)]
#![feature(mem_take)]

#![recursion_limit="256"]

Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4408,7 +4408,7 @@ pub fn enter_impl_trait<F, R>(cx: &DocContext<'_>, f: F) -> R
where
F: FnOnce() -> R,
{
let old_bounds = mem::replace(&mut *cx.impl_trait_bounds.borrow_mut(), Default::default());
let old_bounds = mem::take(&mut *cx.impl_trait_bounds.borrow_mut());
let r = f();
assert!(cx.impl_trait_bounds.borrow().is_empty());
*cx.impl_trait_bounds.borrow_mut() = old_bounds;
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub fn ty_params(mut params: Vec<clean::GenericParamDef>) -> Vec<clean::GenericP
for param in &mut params {
match param.kind {
clean::GenericParamDefKind::Type { ref mut bounds, .. } => {
*bounds = ty_bounds(mem::replace(bounds, Vec::new()));
*bounds = ty_bounds(mem::take(bounds));
}
_ => panic!("expected only type parameters"),
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ pub fn run(mut krate: clean::Crate,
deref_trait_did,
deref_mut_trait_did,
owned_box_did,
masked_crates: mem::replace(&mut krate.masked_crates, Default::default()),
masked_crates: mem::take(&mut krate.masked_crates),
param_names: external_param_names,
aliases: Default::default(),
};
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#![feature(drain_filter)]
#![feature(inner_deref)]
#![feature(never_type)]
#![feature(mem_take)]

#![recursion_limit="256"]

Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/passes/collapse_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::fold;
use crate::fold::{DocFolder};
use crate::passes::Pass;

use std::mem::replace;
use std::mem::take;

pub const COLLAPSE_DOCS: Pass = Pass {
name: "collapse-docs",
Expand Down Expand Up @@ -46,7 +46,7 @@ fn collapse(doc_strings: &mut Vec<DocFragment>) {
let mut docs = vec![];
let mut last_frag: Option<DocFragment> = None;

for frag in replace(doc_strings, vec![]) {
for frag in take(doc_strings) {
if let Some(mut curr_frag) = last_frag.take() {
let curr_kind = curr_frag.kind();
let new_kind = frag.kind();
Expand Down
1 change: 1 addition & 0 deletions src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@
#![feature(libc)]
#![feature(link_args)]
#![feature(linkage)]
#![feature(mem_take)]
#![feature(needs_panic_runtime)]
#![feature(never_type)]
#![feature(nll)]
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/panicking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ fn continue_panic_fmt(info: &PanicInfo<'_>) -> ! {

unsafe impl<'a> BoxMeUp for PanicPayload<'a> {
fn box_me_up(&mut self) -> *mut (dyn Any + Send) {
let contents = mem::replace(self.fill(), String::new());
let contents = mem::take(self.fill());
Box::into_raw(Box::new(contents))
}

Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sync/mpsc/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ impl<T> Packet<T> {
// needs to be careful to destroy the data *outside* of the lock to
// prevent deadlock.
let _data = if guard.cap != 0 {
mem::replace(&mut guard.buf.buf, Vec::new())
mem::take(&mut guard.buf.buf)
} else {
Vec::new()
};
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/sys/windows/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ impl<'a> Drop for AsyncPipe<'a> {
// If anything here fails, there's not really much we can do, so we leak
// the buffer/OVERLAPPED pointers to ensure we're at least memory safe.
if self.pipe.cancel_io().is_err() || self.result().is_err() {
let buf = mem::replace(self.dst, Vec::new());
let buf = mem::take(self.dst);
let overlapped = Box::new(unsafe { mem::zeroed() });
let overlapped = mem::replace(&mut self.overlapped, overlapped);
mem::forget((buf, overlapped));
Expand Down
Loading

0 comments on commit 04d811e

Please sign in to comment.