Skip to content

Commit 2f586b9

Browse files
committed
Opt for .cloned() over .map(|x| x.clone()) etc.
1 parent 5705d48 commit 2f586b9

File tree

39 files changed

+68
-82
lines changed

39 files changed

+68
-82
lines changed

src/compiletest/compiletest.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#![feature(test)]
2222
#![feature(unicode)]
2323
#![feature(env)]
24+
#![feature(core)]
2425

2526
#![deny(warnings)]
2627

src/compiletest/runtest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,7 @@ fn compile_test_(config: &Config, props: &TestProps,
11331133
// FIXME (#9639): This needs to handle non-utf8 paths
11341134
let mut link_args = vec!("-L".to_string(),
11351135
aux_dir.as_str().unwrap().to_string());
1136-
link_args.extend(extra_args.iter().map(|s| s.clone()));
1136+
link_args.extend(extra_args.iter().cloned());
11371137
let args = make_compile_args(config,
11381138
props,
11391139
link_args,

src/libcollections/bit.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2306,7 +2306,7 @@ mod tests {
23062306
#[test]
23072307
fn test_from_bools() {
23082308
let bools = vec![true, false, true, true];
2309-
let bitv: Bitv = bools.iter().map(|n| *n).collect();
2309+
let bitv: Bitv = bools.iter().cloned().collect();
23102310
assert_eq!(format!("{:?}", bitv), "1011");
23112311
}
23122312

@@ -2319,12 +2319,12 @@ mod tests {
23192319
#[test]
23202320
fn test_bitv_iterator() {
23212321
let bools = vec![true, false, true, true];
2322-
let bitv: Bitv = bools.iter().map(|n| *n).collect();
2322+
let bitv: Bitv = bools.iter().cloned().collect();
23232323

23242324
assert_eq!(bitv.iter().collect::<Vec<bool>>(), bools);
23252325

23262326
let long: Vec<_> = (0i32..10000).map(|i| i % 2 == 0).collect();
2327-
let bitv: Bitv = long.iter().map(|n| *n).collect();
2327+
let bitv: Bitv = long.iter().cloned().collect();
23282328
assert_eq!(bitv.iter().collect::<Vec<bool>>(), long)
23292329
}
23302330

src/libcollections/dlist.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -938,7 +938,7 @@ impl<A: Ord> Ord for DList<A> {
938938
#[stable(feature = "rust1", since = "1.0.0")]
939939
impl<A: Clone> Clone for DList<A> {
940940
fn clone(&self) -> DList<A> {
941-
self.iter().map(|x| x.clone()).collect()
941+
self.iter().cloned().collect()
942942
}
943943
}
944944

@@ -1056,7 +1056,7 @@ mod tests {
10561056

10571057
#[cfg(test)]
10581058
fn list_from<T: Clone>(v: &[T]) -> DList<T> {
1059-
v.iter().map(|x| (*x).clone()).collect()
1059+
v.iter().cloned().collect()
10601060
}
10611061

10621062
#[test]

src/libcore/iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ pub trait IteratorExt: Iterator + Sized {
350350
///
351351
/// ```
352352
/// let xs = [100, 200, 300];
353-
/// let mut it = xs.iter().map(|x| *x).peekable();
353+
/// let mut it = xs.iter().cloned().peekable();
354354
/// assert_eq!(*it.peek().unwrap(), 100);
355355
/// assert_eq!(it.next().unwrap(), 100);
356356
/// assert_eq!(it.next().unwrap(), 200);

src/libcoretest/iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ fn test_random_access_inspect() {
713713
fn test_random_access_map() {
714714
let xs = [1, 2, 3, 4, 5];
715715

716-
let mut it = xs.iter().map(|x| *x);
716+
let mut it = xs.iter().cloned();
717717
assert_eq!(xs.len(), it.indexable());
718718
for (i, elt) in xs.iter().enumerate() {
719719
assert_eq!(Some(*elt), it.idx(i));

src/librustc/metadata/cstore.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ impl CStore {
139139
pub fn get_used_crate_source(&self, cnum: ast::CrateNum)
140140
-> Option<CrateSource> {
141141
self.used_crate_sources.borrow_mut()
142-
.iter().find(|source| source.cnum == cnum)
143-
.map(|source| source.clone())
142+
.iter().find(|source| source.cnum == cnum).cloned()
144143
}
145144

146145
pub fn reset(&self) {
@@ -218,7 +217,7 @@ impl CStore {
218217

219218
pub fn find_extern_mod_stmt_cnum(&self, emod_id: ast::NodeId)
220219
-> Option<ast::CrateNum> {
221-
self.extern_mod_crate_map.borrow().get(&emod_id).map(|x| *x)
220+
self.extern_mod_crate_map.borrow().get(&emod_id).cloned()
222221
}
223222
}
224223

src/librustc/middle/check_match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl<'a> fmt::Debug for Matrix<'a> {
7676
pretty_printed_matrix.iter().map(|row| row[col].len()).max().unwrap_or(0)
7777
}).collect();
7878

79-
let total_width = column_widths.iter().map(|n| *n).sum() + column_count * 3 + 1;
79+
let total_width = column_widths.iter().cloned().sum() + column_count * 3 + 1;
8080
let br = repeat('+').take(total_width).collect::<String>();
8181
try!(write!(f, "{}\n", br));
8282
for row in pretty_printed_matrix {

src/librustc/middle/const_eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ fn lit_to_const(lit: &ast::Lit, ty_hint: Option<Ty>) -> const_val {
501501
match lit.node {
502502
ast::LitStr(ref s, _) => const_str((*s).clone()),
503503
ast::LitBinary(ref data) => {
504-
const_binary(Rc::new(data.iter().map(|x| *x).collect()))
504+
const_binary(data.clone())
505505
}
506506
ast::LitByte(n) => const_uint(n as u64),
507507
ast::LitChar(n) => const_uint(n as u64),

src/librustc/middle/dependency_format.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ fn calculate_type(sess: &session::Session,
158158

159159
// Collect what we've got so far in the return vector.
160160
let mut ret = (1..sess.cstore.next_crate_num()).map(|i| {
161-
match formats.get(&i).map(|v| *v) {
161+
match formats.get(&i).cloned() {
162162
v @ Some(cstore::RequireDynamic) => v,
163163
_ => None,
164164
}

src/librustc/middle/infer/error_reporting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ impl<'a, 'tcx> Rebuilder<'a, 'tcx> {
924924

925925
fn rebuild(&self)
926926
-> (ast::FnDecl, Option<ast::ExplicitSelf_>, ast::Generics) {
927-
let mut expl_self_opt = self.expl_self_opt.map(|x| x.clone());
927+
let mut expl_self_opt = self.expl_self_opt.cloned();
928928
let mut inputs = self.fn_decl.inputs.clone();
929929
let mut output = self.fn_decl.output.clone();
930930
let mut ty_params = self.generics.ty_params.clone();

src/librustc/middle/lang_items.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,12 @@ struct LanguageItemCollector<'a> {
147147

148148
impl<'a, 'v> Visitor<'v> for LanguageItemCollector<'a> {
149149
fn visit_item(&mut self, item: &ast::Item) {
150-
match extract(&item.attrs) {
151-
Some(value) => {
152-
let item_index = self.item_refs.get(&value[]).map(|x| *x);
153-
154-
match item_index {
155-
Some(item_index) => {
156-
self.collect_item(item_index, local_def(item.id), item.span)
157-
}
158-
None => {}
159-
}
150+
if let Some(value) = extract(&item.attrs) {
151+
let item_index = self.item_refs.get(&value[]).cloned();
152+
153+
if let Some(item_index) = item_index {
154+
self.collect_item(item_index, local_def(item.id), item.span)
160155
}
161-
None => {}
162156
}
163157

164158
visit::walk_item(self, item);

src/librustc/middle/region.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ impl RegionMaps {
407407

408408
pub fn opt_encl_scope(&self, id: CodeExtent) -> Option<CodeExtent> {
409409
//! Returns the narrowest scope that encloses `id`, if any.
410-
self.scope_map.borrow().get(&id).map(|x| *x)
410+
self.scope_map.borrow().get(&id).cloned()
411411
}
412412

413413
#[allow(dead_code)] // used in middle::cfg

src/librustc/middle/resolve_lifetime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ pub fn early_bound_lifetimes<'a>(generics: &'a ast::Generics) -> Vec<ast::Lifeti
562562

563563
generics.lifetimes.iter()
564564
.filter(|l| referenced_idents.iter().any(|&i| i == l.lifetime.name))
565-
.map(|l| (*l).clone())
565+
.cloned()
566566
.collect()
567567
}
568568

src/librustc/middle/traits/select.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
738738
{
739739
let cache = self.pick_candidate_cache();
740740
let hashmap = cache.hashmap.borrow();
741-
hashmap.get(&cache_fresh_trait_pred.0.trait_ref).map(|c| (*c).clone())
741+
hashmap.get(&cache_fresh_trait_pred.0.trait_ref).cloned()
742742
}
743743

744744
fn insert_candidate_cache(&mut self,

src/librustc/middle/ty.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2868,7 +2868,7 @@ pub fn mk_ctor_fn<'tcx>(cx: &ctxt<'tcx>,
28682868
def_id: ast::DefId,
28692869
input_tys: &[Ty<'tcx>],
28702870
output: Ty<'tcx>) -> Ty<'tcx> {
2871-
let input_args = input_tys.iter().map(|ty| *ty).collect();
2871+
let input_args = input_tys.iter().cloned().collect();
28722872
mk_bare_fn(cx,
28732873
Some(def_id),
28742874
cx.mk_bare_fn(BareFnTy {
@@ -3837,7 +3837,7 @@ pub fn is_type_representable<'tcx>(cx: &ctxt<'tcx>, sp: Span, ty: Ty<'tcx>)
38373837
-> Representability {
38383838
match ty.sty {
38393839
ty_tup(ref ts) => {
3840-
find_nonrepresentable(cx, sp, seen, ts.iter().map(|ty| *ty))
3840+
find_nonrepresentable(cx, sp, seen, ts.iter().cloned())
38413841
}
38423842
// Fixed-length vectors.
38433843
// FIXME(#11924) Behavior undecided for zero-length vectors.
@@ -4965,7 +4965,7 @@ pub fn note_and_explain_type_err(cx: &ctxt, err: &type_err) {
49654965
}
49664966

49674967
pub fn provided_source(cx: &ctxt, id: ast::DefId) -> Option<ast::DefId> {
4968-
cx.provided_method_sources.borrow().get(&id).map(|x| *x)
4968+
cx.provided_method_sources.borrow().get(&id).cloned()
49694969
}
49704970

49714971
pub fn provided_trait_methods<'tcx>(cx: &ctxt<'tcx>, id: ast::DefId)

src/librustc/util/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ pub fn memoized<T, U, S, F>(cache: &RefCell<HashMap<T, U, S>>, arg: T, f: F) ->
214214
F: FnOnce(T) -> U,
215215
{
216216
let key = arg.clone();
217-
let result = cache.borrow().get(&key).map(|result| result.clone());
217+
let result = cache.borrow().get(&key).cloned();
218218
match result {
219219
Some(result) => result,
220220
None => {

src/librustc_back/rpath.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@ pub fn get_rpath_flags<F, G>(config: RPathConfig<F, G>) -> Vec<String> where
4040
debug!("preparing the RPATH!");
4141

4242
let libs = config.used_crates.clone();
43-
let libs = libs.into_iter().filter_map(|(_, l)| {
44-
l.map(|p| p.clone())
45-
}).collect::<Vec<_>>();
46-
43+
let libs = libs.into_iter().filter_map(|(_, l)| l).collect::<Vec<_>>();
4744
let rpaths = get_rpaths(config, &libs[]);
4845
flags.push_all(&rpaths_to_flags(&rpaths[])[]);
4946
flags

src/librustc_driver/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ impl<'a, 'tcx> Env<'a, 'tcx> {
254254
output_ty: Ty<'tcx>)
255255
-> Ty<'tcx>
256256
{
257-
let input_args = input_tys.iter().map(|ty| *ty).collect();
257+
let input_args = input_tys.iter().cloned().collect();
258258
ty::mk_bare_fn(self.infcx.tcx,
259259
None,
260260
self.infcx.tcx.mk_bare_fn(ty::BareFnTy {

src/librustc_resolve/lib.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,18 +1920,15 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
19201920
-> ResolveResult<(Rc<Module>, LastPrivate)> {
19211921
fn search_parent_externals(needle: Name, module: &Rc<Module>)
19221922
-> Option<Rc<Module>> {
1923-
module.external_module_children.borrow()
1924-
.get(&needle).cloned()
1925-
.map(|_| module.clone())
1926-
.or_else(|| {
1927-
match module.parent_link.clone() {
1928-
ModuleParentLink(parent, _) => {
1929-
search_parent_externals(needle,
1930-
&parent.upgrade().unwrap())
1923+
match module.external_module_children.borrow().get(&needle) {
1924+
Some(_) => Some(module.clone()),
1925+
None => match module.parent_link {
1926+
ModuleParentLink(ref parent, _) => {
1927+
search_parent_externals(needle, &parent.upgrade().unwrap())
19311928
}
19321929
_ => None
19331930
}
1934-
})
1931+
}
19351932
}
19361933

19371934
let mut search_module = module_;

src/librustc_trans/trans/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3211,7 +3211,7 @@ pub fn trans_crate<'tcx>(analysis: ty::CrateAnalysis<'tcx>)
32113211
reachable.push("rust_eh_personality_catch".to_string());
32123212

32133213
if codegen_units > 1 {
3214-
internalize_symbols(&shared_ccx, &reachable.iter().map(|x| x.clone()).collect());
3214+
internalize_symbols(&shared_ccx, &reachable.iter().cloned().collect());
32153215
}
32163216

32173217
let metadata_module = ModuleTranslation {

src/librustc_trans/trans/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,7 @@ fn trans_rvalue_dps_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
11971197
let trait_ref =
11981198
bcx.tcx().object_cast_map.borrow()
11991199
.get(&expr.id)
1200-
.map(|t| (*t).clone())
1200+
.cloned()
12011201
.unwrap();
12021202
let trait_ref = bcx.monomorphize(&trait_ref);
12031203
let datum = unpack_datum!(bcx, trans(bcx, &**val));

src/librustc_trans/trans/type_of.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub fn untuple_arguments_if_necessary<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
6767
abi: abi::Abi)
6868
-> Vec<Ty<'tcx>> {
6969
if abi != abi::RustCall {
70-
return inputs.iter().map(|x| (*x).clone()).collect()
70+
return inputs.iter().cloned().collect()
7171
}
7272

7373
if inputs.len() == 0 {

src/librustc_typeck/check/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3220,7 +3220,7 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
32203220
for field in ast_fields {
32213221
let mut expected_field_type = tcx.types.err;
32223222

3223-
let pair = class_field_map.get(&field.ident.node.name).map(|x| *x);
3223+
let pair = class_field_map.get(&field.ident.node.name).cloned();
32243224
match pair {
32253225
None => {
32263226
fcx.type_error_message(
@@ -3852,7 +3852,7 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
38523852
}
38533853
ast::ExprStruct(ref path, ref fields, ref base_expr) => {
38543854
// Resolve the path.
3855-
let def = tcx.def_map.borrow().get(&id).map(|i| *i);
3855+
let def = tcx.def_map.borrow().get(&id).cloned();
38563856
let struct_id = match def {
38573857
Some(def::DefVariant(enum_id, variant_id, true)) => {
38583858
check_struct_enum_variant(fcx, id, expr.span, enum_id,

src/librustdoc/passes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ pub fn collapse_docs(krate: clean::Crate) -> plugins::PluginResult {
293293
let mut a: Vec<clean::Attribute> = i.attrs.iter().filter(|&a| match a {
294294
&clean::NameValue(ref x, _) if "doc" == *x => false,
295295
_ => true
296-
}).map(|x| x.clone()).collect();
296+
}).cloned().collect();
297297
if docstr.len() > 0 {
298298
a.push(clean::NameValue("doc".to_string(), docstr));
299299
}

src/librustdoc/visit_ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
333333
name: name,
334334
items: items.clone(),
335335
generics: gen.clone(),
336-
bounds: b.iter().map(|x| (*x).clone()).collect(),
336+
bounds: b.iter().cloned().collect(),
337337
id: item.id,
338338
attrs: item.attrs.clone(),
339339
whence: item.span,

src/libstd/env.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ mod tests {
918918
#[cfg(unix)]
919919
fn join_paths_unix() {
920920
fn test_eq(input: &[&str], output: &str) -> bool {
921-
&*join_paths(input.iter().map(|s| *s)).unwrap() ==
921+
&*join_paths(input.iter().cloned()).unwrap() ==
922922
OsStr::from_str(output)
923923
}
924924

@@ -927,14 +927,14 @@ mod tests {
927927
"/bin:/usr/bin:/usr/local/bin"));
928928
assert!(test_eq(&["", "/bin", "", "", "/usr/bin", ""],
929929
":/bin:::/usr/bin:"));
930-
assert!(join_paths(["/te:st"].iter().map(|s| *s)).is_err());
930+
assert!(join_paths(["/te:st"].iter().cloned()).is_err());
931931
}
932932

933933
#[test]
934934
#[cfg(windows)]
935935
fn join_paths_windows() {
936936
fn test_eq(input: &[&str], output: &str) -> bool {
937-
&*join_paths(input.iter().map(|s| *s)).unwrap() ==
937+
&*join_paths(input.iter().cloned()).unwrap() ==
938938
OsStr::from_str(output)
939939
}
940940

@@ -945,6 +945,6 @@ mod tests {
945945
r";c:\windows;;;c:\;"));
946946
assert!(test_eq(&[r"c:\te;st", r"c:\"],
947947
r#""c:\te;st";c:\"#));
948-
assert!(join_paths([r#"c:\te"st"#].iter().map(|s| *s)).is_err());
948+
assert!(join_paths([r#"c:\te"st"#].iter().cloned()).is_err());
949949
}
950950
}

src/libsyntax/ast_map/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ impl<'ast> Map<'ast> {
251251
}
252252

253253
fn find_entry(&self, id: NodeId) -> Option<MapEntry<'ast>> {
254-
self.map.borrow().get(id as usize).map(|e| *e)
254+
self.map.borrow().get(id as usize).cloned()
255255
}
256256

257257
pub fn krate(&self) -> &'ast Crate {

src/libsyntax/diagnostics/registry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ impl Registry {
2121
}
2222

2323
pub fn find_description(&self, code: &str) -> Option<&'static str> {
24-
self.descriptions.get(code).map(|desc| *desc)
24+
self.descriptions.get(code).cloned()
2525
}
2626
}

src/libsyntax/ext/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ impl<'a> ExtCtxt<'a> {
639639
pub fn mod_path(&self) -> Vec<ast::Ident> {
640640
let mut v = Vec::new();
641641
v.push(token::str_to_ident(&self.ecfg.crate_name[]));
642-
v.extend(self.mod_path.iter().map(|a| *a));
642+
v.extend(self.mod_path.iter().cloned());
643643
return v;
644644
}
645645
pub fn bt_push(&mut self, ei: ExpnInfo) {

0 commit comments

Comments
 (0)