Skip to content

Commit 2e8b61d

Browse files
authored
Rollup merge of rust-lang#55048 - ljedrz:begone_vecc, r=estebank
Don't collect to vectors where unnecessary This removes 3 vector allocations and a call to `cloned()`.
2 parents dc87247 + 12b5c7b commit 2e8b61d

File tree

3 files changed

+8
-14
lines changed

3 files changed

+8
-14
lines changed

src/librustc/hir/lowering.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ use util::nodemap::{DefIdMap, NodeMap};
6060

6161
use std::collections::BTreeMap;
6262
use std::fmt::Debug;
63-
use std::iter;
6463
use std::mem;
6564
use smallvec::SmallVec;
6665
use syntax::attr;
@@ -3888,9 +3887,7 @@ impl<'a> LoweringContext<'a> {
38883887
.collect::<P<[hir::Field]>>();
38893888

38903889
let is_unit = fields.is_empty();
3891-
let struct_path = iter::once("ops")
3892-
.chain(iter::once(path))
3893-
.collect::<Vec<_>>();
3890+
let struct_path = ["ops", path];
38943891
let struct_path = self.std_path(e.span, &struct_path, None, is_unit);
38953892
let struct_path = hir::QPath::Resolved(None, P(struct_path));
38963893

src/librustc_traits/lowering.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,7 @@ fn program_clauses_for_trait<'a, 'tcx>(
306306
let wf_conditions = iter::once(ty::Binder::dummy(trait_pred.lower()))
307307
.chain(
308308
where_clauses
309-
.iter()
310-
.cloned()
309+
.into_iter()
311310
.map(|wc| wc.map_bound(|goal| goal.into_well_formed_goal()))
312311
);
313312

@@ -350,15 +349,13 @@ fn program_clauses_for_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId
350349
// `WC`
351350
let where_clauses = tcx.predicates_of(def_id).predicates
352351
.into_iter()
353-
.map(|(wc, _)| wc.lower())
354-
.collect::<Vec<_>>();
352+
.map(|(wc, _)| wc.lower());
355353

356354
// `Implemented(A0: Trait<A1..An>) :- WC`
357355
let clause = ProgramClause {
358356
goal: trait_pred,
359357
hypotheses: tcx.mk_goals(
360358
where_clauses
361-
.into_iter()
362359
.map(|wc| tcx.mk_goal(GoalKind::from_poly_domain_goal(wc, tcx))),
363360
),
364361
};

src/librustc_typeck/check/demand.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
115115
// field is of the found type, suggest such variants. See Issue
116116
// #42764.
117117
if let ty::Adt(expected_adt, substs) = expected.sty {
118-
let compatible_variants = expected_adt.variants
118+
let mut compatible_variants = expected_adt.variants
119119
.iter()
120120
.filter(|variant| variant.fields.len() == 1)
121121
.filter_map(|variant| {
@@ -127,12 +127,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
127127
} else {
128128
None
129129
}
130-
}).collect::<Vec<_>>();
130+
}).peekable();
131131

132-
if !compatible_variants.is_empty() {
132+
if compatible_variants.peek().is_some() {
133133
let expr_text = print::to_string(print::NO_ANN, |s| s.print_expr(expr));
134-
let suggestions = compatible_variants.iter()
135-
.map(|v| format!("{}({})", v, expr_text)).collect::<Vec<_>>();
134+
let suggestions = compatible_variants.map(|v|
135+
format!("{}({})", v, expr_text)).collect::<Vec<_>>();
136136
err.span_suggestions_with_applicability(
137137
expr.span,
138138
"try using a variant of the expected type",

0 commit comments

Comments
 (0)