Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid cloning a collection only to iterate over it #100497

Merged
merged 1 commit into from
Aug 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,7 @@ fn find_type_parameters(
// Place bound generic params on a stack, to extract them when a type is encountered.
fn visit_poly_trait_ref(&mut self, trait_ref: &'a ast::PolyTraitRef) {
let stack_len = self.bound_generic_params_stack.len();
self.bound_generic_params_stack
.extend(trait_ref.bound_generic_params.clone().into_iter());
self.bound_generic_params_stack.extend(trait_ref.bound_generic_params.iter().cloned());

visit::walk_poly_trait_ref(self, trait_ref);

Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_parse/src/parser/attr_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl CreateTokenStream for LazyTokenStreamImpl {

if !self.replace_ranges.is_empty() {
let mut tokens: Vec<_> = tokens.collect();
let mut replace_ranges = self.replace_ranges.clone();
let mut replace_ranges = self.replace_ranges.to_vec();
replace_ranges.sort_by_key(|(range, _)| range.start);

#[cfg(debug_assertions)]
Expand Down Expand Up @@ -146,7 +146,7 @@ impl CreateTokenStream for LazyTokenStreamImpl {
// start position, we ensure that any replace range which encloses
// another replace range will capture the *replaced* tokens for the inner
// range, not the original tokens.
for (range, new_tokens) in replace_ranges.iter().rev() {
for (range, new_tokens) in replace_ranges.into_iter().rev() {
assert!(!range.is_empty(), "Cannot replace an empty range: {:?}", range);
// Replace ranges are only allowed to decrease the number of tokens.
assert!(
Expand All @@ -165,7 +165,7 @@ impl CreateTokenStream for LazyTokenStreamImpl {

tokens.splice(
(range.start as usize)..(range.end as usize),
new_tokens.clone().into_iter().chain(filler),
new_tokens.into_iter().chain(filler),
);
}
make_token_stream(tokens.into_iter(), self.break_last_token)
Expand Down Expand Up @@ -321,7 +321,7 @@ impl<'a> Parser<'a> {
self.capture_state.replace_ranges[replace_ranges_start..replace_ranges_end]
.iter()
.cloned()
.chain(inner_attr_replace_ranges.clone().into_iter())
.chain(inner_attr_replace_ranges.iter().cloned())
.map(|(range, tokens)| {
((range.start - start_calls)..(range.end - start_calls), tokens)
})
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ impl<'a> Parser<'a> {
/// This is to avoid losing unclosed delims errors `create_snapshot_for_diagnostic` clears.
pub(super) fn restore_snapshot(&mut self, snapshot: SnapshotParser<'a>) {
*self = snapshot.parser;
self.unclosed_delims.extend(snapshot.unclosed_delims.clone());
self.unclosed_delims.extend(snapshot.unclosed_delims);
}

pub fn unclosed_delims(&self) -> &[UnmatchedBrace] {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2300,7 +2300,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
err.multipart_suggestion_verbose(
message,
std::iter::once((span, intro_sugg))
.chain(spans_suggs.clone())
.chain(spans_suggs.iter().cloned())
.collect(),
Applicability::MaybeIncorrect,
);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_trait_selection/src/traits/auto_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
}
}

let obligations = impl_source.clone().nested_obligations().into_iter();
let obligations = impl_source.borrow_nested_obligations().iter().cloned();

if !self.evaluate_nested_obligations(
ty,
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_typeck/src/astconv/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
// show that order to the user as a possible order for the parameters
let mut param_types_present = defs
.params
.clone()
.into_iter()
.map(|param| (param.kind.to_ord(), param))
.iter()
.map(|param| (param.kind.to_ord(), param.clone()))
.collect::<Vec<(ParamKindOrd, GenericParamDef)>>();
param_types_present.sort_by_key(|(ord, _)| *ord);
let (mut param_types_present, ordered_params): (
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_typeck/src/check/upvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

// Combine all the reasons of why the root variable should be captured as a result of
// auto trait implementation issues
auto_trait_migration_reasons.extend(capture_trait_reasons.clone());
auto_trait_migration_reasons.extend(capture_trait_reasons.iter().copied());

diagnostics_info.push(MigrationLintNote {
captures_info,
Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/clean/auto_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,8 +525,8 @@ where
GenericBound::TraitBound(ref mut p, _) => {
// Insert regions into the for_generics hash map first, to ensure
// that we don't end up with duplicate bounds (e.g., for<'b, 'b>)
for_generics.extend(p.generic_params.clone());
p.generic_params = for_generics.into_iter().collect();
for_generics.extend(p.generic_params.drain(..));
p.generic_params.extend(for_generics);
self.is_fn_trait(&p.trait_)
}
_ => false,
Expand Down
13 changes: 9 additions & 4 deletions src/librustdoc/html/render/search_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,10 +544,15 @@ fn get_fn_inputs_and_outputs<'tcx>(
(true, _) => (Some(impl_self), &func.generics),
(_, true) => (Some(impl_self), impl_generics),
(false, false) => {
let mut params = func.generics.params.clone();
params.extend(impl_generics.params.clone());
let mut where_predicates = func.generics.where_predicates.clone();
where_predicates.extend(impl_generics.where_predicates.clone());
let params =
func.generics.params.iter().chain(&impl_generics.params).cloned().collect();
let where_predicates = func
.generics
.where_predicates
.iter()
.chain(&impl_generics.where_predicates)
.cloned()
.collect();
combined_generics = clean::Generics { params, where_predicates };
(Some(impl_self), &combined_generics)
}
Expand Down
11 changes: 6 additions & 5 deletions src/librustdoc/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ impl<'tcx> JsonRenderer<'tcx> {
// only need to synthesize items for external traits
if !id.is_local() {
let trait_item = &trait_item.trait_;
trait_item.items.clone().into_iter().for_each(|i| self.item(i).unwrap());
for item in &trait_item.items {
self.item(item.clone()).unwrap();
}
let item_id = from_item_id(id.into(), self.tcx);
Some((
item_id.clone(),
Expand Down Expand Up @@ -274,10 +276,9 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
paths: self
.cache
.paths
.clone()
.into_iter()
.chain(self.cache.external_paths.clone().into_iter())
.map(|(k, (path, kind))| {
.iter()
.chain(&self.cache.external_paths)
.map(|(&k, &(ref path, kind))| {
(
from_item_id(k.into(), self.tcx),
types::ItemSummary {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/compiletest/src/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1702,7 +1702,7 @@ impl<'test> TestCx<'test> {

fn compose_and_run_compiler(&self, mut rustc: Command, input: Option<String>) -> ProcRes {
let aux_dir = self.build_all_auxiliary(&mut rustc);
self.props.unset_rustc_env.clone().iter().fold(&mut rustc, |rustc, v| rustc.env_remove(v));
self.props.unset_rustc_env.iter().fold(&mut rustc, Command::env_remove);
rustc.envs(self.props.rustc_env.clone());
self.compose_and_run(
rustc,
Expand Down
8 changes: 2 additions & 6 deletions src/tools/unicode-table-generator/src/raw_emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,8 @@ impl RawEmitter {
for chunk in compressed_words.chunks(chunk_length) {
chunks.insert(chunk);
}
let chunk_map = chunks
.clone()
.into_iter()
.enumerate()
.map(|(idx, chunk)| (chunk, idx))
.collect::<HashMap<_, _>>();
let chunk_map =
chunks.iter().enumerate().map(|(idx, &chunk)| (chunk, idx)).collect::<HashMap<_, _>>();
let mut chunk_indices = Vec::new();
for chunk in compressed_words.chunks(chunk_length) {
chunk_indices.push(chunk_map[chunk]);
Expand Down