Skip to content

Commit b4e674f

Browse files
committed
librustc: Add a lint mode for unnecessary copy and remove a bunch of them.
1 parent 8c08265 commit b4e674f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+256
-235
lines changed

src/libextra/arena.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl Arena {
177177
// Allocate a new chunk.
178178
let chunk_size = at_vec::capacity(self.pod_head.data);
179179
let new_min_chunk_size = num::max(n_bytes, chunk_size);
180-
self.chunks = @mut MutCons(copy self.pod_head, self.chunks);
180+
self.chunks = @mut MutCons(self.pod_head, self.chunks);
181181
self.pod_head =
182182
chunk(uint::next_power_of_two(new_min_chunk_size + 1u), true);
183183

@@ -219,7 +219,7 @@ impl Arena {
219219
// Allocate a new chunk.
220220
let chunk_size = at_vec::capacity(self.head.data);
221221
let new_min_chunk_size = num::max(n_bytes, chunk_size);
222-
self.chunks = @mut MutCons(copy self.head, self.chunks);
222+
self.chunks = @mut MutCons(self.head, self.chunks);
223223
self.head =
224224
chunk(uint::next_power_of_two(new_min_chunk_size + 1u), false);
225225

src/libextra/ebml.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,12 @@ pub mod reader {
318318
let TaggedDoc { tag: r_tag, doc: r_doc } =
319319
doc_at(self.parent.data, self.pos);
320320
debug!("self.parent=%?-%? self.pos=%? r_tag=%? r_doc=%?-%?",
321-
copy self.parent.start, copy self.parent.end,
322-
copy self.pos, r_tag, r_doc.start, r_doc.end);
321+
self.parent.start,
322+
self.parent.end,
323+
self.pos,
324+
r_tag,
325+
r_doc.start,
326+
r_doc.end);
323327
if r_tag != (exp_tag as uint) {
324328
fail!("expected EBML doc with tag %? but found tag %?", exp_tag, r_tag);
325329
}

src/libextra/net/ip.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ pub mod v4 {
240240
err_msg: ~"uv_ip4_name produced invalid result.",
241241
})
242242
} else {
243-
Ok(Ipv4(copy(new_addr)))
243+
Ok(Ipv4(new_addr))
244244
}
245245
}
246246
}
@@ -312,12 +312,10 @@ extern fn get_addr_cb(handle: *uv_getaddrinfo_t,
312312
let mut curr_addr = res;
313313
loop {
314314
let new_ip_addr = if ll::is_ipv4_addrinfo(curr_addr) {
315-
Ipv4(copy((
316-
*ll::addrinfo_as_sockaddr_in(curr_addr))))
315+
Ipv4(*ll::addrinfo_as_sockaddr_in(curr_addr))
317316
}
318317
else if ll::is_ipv6_addrinfo(curr_addr) {
319-
Ipv6(copy((
320-
*ll::addrinfo_as_sockaddr_in6(curr_addr))))
318+
Ipv6(*ll::addrinfo_as_sockaddr_in6(curr_addr))
321319
}
322320
else {
323321
debug!("curr_addr is not of family AF_INET or \

src/libextra/net/tcp.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -682,8 +682,8 @@ fn listen_common(host_ip: ip::IpAddr,
682682
// will defeat a move sigil, as is done to the host_ip
683683
// arg above.. this same pattern works w/o complaint in
684684
// tcp::connect (because the iotask::interact cb isn't
685-
// nested within a std::comm::listen block)
686-
let loc_ip = copy(host_ip);
685+
// nested within a core::comm::listen block)
686+
let loc_ip = host_ip;
687687
do iotask::interact(iotask) |loop_ptr| {
688688
unsafe {
689689
match uv::ll::tcp_init(loop_ptr, server_stream_ptr) {

src/librust/rust.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ fn rustc_help() {
124124
fn find_cmd(command_string: &str) -> Option<Command> {
125125
do COMMANDS.iter().find_ |command| {
126126
command.cmd == command_string
127-
}.map_consume(|x| copy *x)
127+
}.map_consume(|x| *x)
128128
}
129129

130130
fn cmd_help(args: &[~str]) -> ValidUsage {

src/librustc/front/config.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@ pub fn strip_items(crate: &ast::crate, in_cfg: in_cfg_pred)
3535
fold_mod: |a,b| fold_mod(ctxt, a, b),
3636
fold_block: |a,b| fold_block(ctxt, a, b),
3737
fold_foreign_mod: |a,b| fold_foreign_mod(ctxt, a, b),
38-
fold_item_underscore: |a,b| {
39-
// Bad copy.
40-
fold_item_underscore(ctxt, copy a, b)
41-
},
42-
.. *fold::default_ast_fold()};
38+
fold_item_underscore: |a,b| fold_item_underscore(ctxt, a, b),
39+
.. *fold::default_ast_fold()
40+
};
4341

4442
let fold = fold::make_fold(precursor);
4543
@fold.fold_crate(crate)

src/librustc/metadata/decoder.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -538,14 +538,12 @@ impl<'self> EachItemContext<'self> {
538538

539539
fn each_item_of_module(&mut self, def_id: ast::def_id) -> bool {
540540
// This item might not be in this crate. If it's not, look it up.
541-
let (_cdata, items) = if def_id.crate == self.cdata.cnum {
542-
let items = reader::get_doc(reader::Doc(self.cdata.data),
543-
tag_items);
544-
(self.cdata, items)
541+
let items = if def_id.crate == self.cdata.cnum {
542+
reader::get_doc(reader::Doc(self.cdata.data), tag_items)
545543
} else {
546544
let crate_data = (self.get_crate_data)(def_id.crate);
547545
let root = reader::Doc(crate_data.data);
548-
(crate_data, reader::get_doc(root, tag_items))
546+
reader::get_doc(root, tag_items)
549547
};
550548

551549
// Look up the item.
@@ -717,14 +715,14 @@ pub fn maybe_get_item_ast(cdata: cmd, tcx: ty::ctxt,
717715
item_path.init().to_owned()
718716
};
719717
match decode_inlined_item(cdata, tcx, copy path, item_doc) {
720-
Some(ref ii) => csearch::found((/*bad*/copy *ii)),
718+
Some(ref ii) => csearch::found(*ii),
721719
None => {
722720
match item_parent_item(item_doc) {
723721
Some(did) => {
724722
let did = translate_def_id(cdata, did);
725723
let parent_item = lookup_item(did.node, cdata.data);
726724
match decode_inlined_item(cdata, tcx, path, parent_item) {
727-
Some(ref ii) => csearch::found_parent(did, (/*bad*/copy *ii)),
725+
Some(ref ii) => csearch::found_parent(did, *ii),
728726
None => csearch::not_found
729727
}
730728
}

src/librustc/metadata/encoder.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -780,8 +780,12 @@ fn encode_info_for_method(ecx: &EncodeContext,
780780
}
781781

782782
let mut combined_ty_params = opt_vec::Empty;
783-
for owner_generics.ty_params.iter().advance |x| { combined_ty_params.push(copy *x) }
784-
for method_generics.ty_params.iter().advance |x| { combined_ty_params.push(copy *x) }
783+
for owner_generics.ty_params.iter().advance |x| {
784+
combined_ty_params.push(copy *x)
785+
}
786+
for method_generics.ty_params.iter().advance |x| {
787+
combined_ty_params.push(copy *x)
788+
}
785789
let len = combined_ty_params.len();
786790
encode_type_param_bounds(ebml_w, ecx, &combined_ty_params);
787791

@@ -1392,14 +1396,14 @@ fn synthesize_crate_attrs(ecx: &EncodeContext,
13921396
for crate.node.attrs.iter().advance |attr| {
13931397
attrs.push(
13941398
if "link" != attr::get_attr_name(attr) {
1395-
copy *attr
1399+
*attr
13961400
} else {
13971401
match attr.node.value.node {
13981402
meta_list(_, ref l) => {
13991403
found_link_attr = true;;
14001404
synthesize_link_attr(ecx, /*bad*/copy *l)
14011405
}
1402-
_ => copy *attr
1406+
_ => *attr
14031407
}
14041408
});
14051409
}

src/librustc/metadata/tydecode.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,10 @@ fn parse_ty(st: &mut PState, conv: conv_did) -> ty::t {
371371
match st.tcx.rcache.find(&key) {
372372
Some(&tt) => return tt,
373373
None => {
374-
let mut ps = PState {pos: pos ,.. copy *st};
374+
let mut ps = PState {
375+
pos: pos,
376+
.. *st
377+
};
375378
let tt = parse_ty(&mut ps, conv);
376379
st.tcx.rcache.insert(key, tt);
377380
return tt;

src/librustc/middle/check_match.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,15 @@ pub fn is_useful(cx: &MatchCheckCtxt, m: &matrix, v: &[@pat]) -> useful {
218218
val(const_bool(false)),
219219
0u, left_ty)
220220
}
221-
ref u => (/*bad*/copy *u)
221+
ref u => *u,
222222
}
223223
}
224224
ty::ty_enum(eid, _) => {
225225
for (*ty::enum_variants(cx.tcx, eid)).iter().advance |va| {
226226
match is_useful_specialized(cx, m, v, variant(va.id),
227227
va.args.len(), left_ty) {
228228
not_useful => (),
229-
ref u => return (/*bad*/copy *u)
229+
ref u => return *u,
230230
}
231231
}
232232
not_useful
@@ -243,7 +243,7 @@ pub fn is_useful(cx: &MatchCheckCtxt, m: &matrix, v: &[@pat]) -> useful {
243243
for uint::range(0, max_len + 1) |n| {
244244
match is_useful_specialized(cx, m, v, vec(n), n, left_ty) {
245245
not_useful => (),
246-
ref u => return (/*bad*/copy *u)
246+
ref u => return *u,
247247
}
248248
}
249249
not_useful
@@ -258,15 +258,15 @@ pub fn is_useful(cx: &MatchCheckCtxt, m: &matrix, v: &[@pat]) -> useful {
258258
match is_useful(cx,
259259
&m.iter().filter_map(|r| default(cx, *r)).collect::<matrix>(),
260260
v.tail()) {
261-
useful_ => useful(left_ty, /*bad*/copy *ctor),
262-
ref u => (/*bad*/copy *u)
261+
useful_ => useful(left_ty, *ctor),
262+
ref u => *u,
263263
}
264264
}
265265
}
266266
}
267267
Some(ref v0_ctor) => {
268268
let arity = ctor_arity(cx, v0_ctor, left_ty);
269-
is_useful_specialized(cx, m, v, /*bad*/copy *v0_ctor, arity, left_ty)
269+
is_useful_specialized(cx, m, v, *v0_ctor, arity, left_ty)
270270
}
271271
}
272272
}
@@ -283,7 +283,7 @@ pub fn is_useful_specialized(cx: &MatchCheckCtxt,
283283
cx, &ms, specialize(cx, v, &ctor, arity, lty).get());
284284
match could_be_useful {
285285
useful_ => useful(lty, ctor),
286-
ref u => (/*bad*/copy *u)
286+
ref u => *u,
287287
}
288288
}
289289

@@ -355,7 +355,7 @@ pub fn missing_ctor(cx: &MatchCheckCtxt,
355355
let r = pat_ctor_id(cx, r[0]);
356356
for r.iter().advance |id| {
357357
if !found.contains(id) {
358-
found.push(/*bad*/copy *id);
358+
found.push(*id);
359359
}
360360
}
361361
}
@@ -680,9 +680,8 @@ pub fn specialize(cx: &MatchCheckCtxt,
680680
}
681681
pat_range(lo, hi) => {
682682
let (c_lo, c_hi) = match *ctor_id {
683-
val(ref v) => ((/*bad*/copy *v), (/*bad*/copy *v)),
684-
range(ref lo, ref hi) =>
685-
((/*bad*/copy *lo), (/*bad*/copy *hi)),
683+
val(ref v) => (*v, *v),
684+
range(ref lo, ref hi) => (*lo, *hi),
686685
single => return Some(r.tail().to_owned()),
687686
_ => fail!("type error")
688687
};

src/librustc/middle/lint.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ pub enum lint {
8383
type_limits,
8484
default_methods,
8585
unused_unsafe,
86+
copy_implicitly_copyable,
8687

8788
managed_heap_memory,
8889
owned_heap_memory,
@@ -259,6 +260,14 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
259260
default: warn
260261
}),
261262

263+
("copy_implicitly_copyable",
264+
LintSpec {
265+
lint: copy_implicitly_copyable,
266+
desc: "detect unnecessary uses of `copy` on implicitly copyable \
267+
values",
268+
default: warn
269+
}),
270+
262271
("unused_variable",
263272
LintSpec {
264273
lint: unused_variable,
@@ -514,7 +523,7 @@ impl Context {
514523
// item_stopping_visitor has overridden visit_fn(&fk_method(... ))
515524
// to be a no-op, so manually invoke visit_fn.
516525
Method(m) => {
517-
let fk = visit::fk_method(copy m.ident, &m.generics, m);
526+
let fk = visit::fk_method(m.ident, &m.generics, m);
518527
for self.visitors.iter().advance |&(orig, stopping)| {
519528
(orig.visit_fn)(&fk, &m.decl, &m.body, m.span, m.id,
520529
(self, stopping));
@@ -935,6 +944,26 @@ fn lint_unused_unsafe() -> visit::vt<@mut Context> {
935944
})
936945
}
937946

947+
fn lint_copy_implicitly_copyable() -> visit::vt<@mut Context> {
948+
visit::mk_vt(@visit::Visitor {
949+
visit_expr: |e, (cx, vt): (@mut Context, visit::vt<@mut Context>)| {
950+
match e.node {
951+
ast::expr_copy(subexpr) => {
952+
let ty = ty::expr_ty(cx.tcx, subexpr);
953+
if !ty::type_moves_by_default(cx.tcx, ty) {
954+
cx.span_lint(copy_implicitly_copyable,
955+
e.span,
956+
"unnecessary `copy`; this value is implicitly copyable");
957+
}
958+
}
959+
_ => ()
960+
}
961+
visit::visit_expr(e, (cx, vt));
962+
},
963+
.. *visit::default_visitor()
964+
})
965+
}
966+
938967
fn lint_unused_mut() -> visit::vt<@mut Context> {
939968
fn check_pat(cx: &Context, p: @ast::pat) {
940969
let mut used = false;
@@ -1147,6 +1176,7 @@ pub fn check_crate(tcx: ty::ctxt, crate: @ast::crate) {
11471176
cx.add_lint(lint_heap());
11481177
cx.add_lint(lint_type_limits());
11491178
cx.add_lint(lint_unused_unsafe());
1179+
cx.add_lint(lint_copy_implicitly_copyable());
11501180
cx.add_lint(lint_unused_mut());
11511181
cx.add_lint(lint_session());
11521182
cx.add_lint(lint_unnecessary_allocations());

src/librustc/middle/liveness.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ impl Liveness {
680680
*/
681681
pub fn live_on_exit(&self, ln: LiveNode, var: Variable)
682682
-> Option<LiveNodeKind> {
683-
self.live_on_entry(copy self.successors[*ln], var)
683+
self.live_on_entry(self.successors[*ln], var)
684684
}
685685

686686
pub fn used_on_entry(&self, ln: LiveNode, var: Variable) -> bool {
@@ -697,7 +697,7 @@ impl Liveness {
697697

698698
pub fn assigned_on_exit(&self, ln: LiveNode, var: Variable)
699699
-> Option<LiveNodeKind> {
700-
self.assigned_on_entry(copy self.successors[*ln], var)
700+
self.assigned_on_entry(self.successors[*ln], var)
701701
}
702702

703703
pub fn indices(&self, ln: LiveNode, op: &fn(uint)) {
@@ -768,14 +768,14 @@ impl Liveness {
768768
wr.write_str("[ln(");
769769
wr.write_uint(*ln);
770770
wr.write_str(") of kind ");
771-
wr.write_str(fmt!("%?", copy self.ir.lnks[*ln]));
771+
wr.write_str(fmt!("%?", self.ir.lnks[*ln]));
772772
wr.write_str(" reads");
773773
self.write_vars(wr, ln, |idx| self.users[idx].reader );
774774
wr.write_str(" writes");
775775
self.write_vars(wr, ln, |idx| self.users[idx].writer );
776776
wr.write_str(" ");
777777
wr.write_str(" precedes ");
778-
wr.write_str((copy self.successors[*ln]).to_str());
778+
wr.write_str((self.successors[*ln]).to_str());
779779
wr.write_str("]");
780780
}
781781
}
@@ -813,9 +813,9 @@ impl Liveness {
813813
let mut changed = false;
814814
do self.indices2(ln, succ_ln) |idx, succ_idx| {
815815
let users = &mut *self.users;
816-
changed |= copy_if_invalid(copy users[succ_idx].reader,
816+
changed |= copy_if_invalid(users[succ_idx].reader,
817817
&mut users[idx].reader);
818-
changed |= copy_if_invalid(copy users[succ_idx].writer,
818+
changed |= copy_if_invalid(users[succ_idx].writer,
819819
&mut users[idx].writer);
820820
if users[succ_idx].used && !users[idx].used {
821821
users[idx].used = true;

src/librustc/middle/region.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ impl DetermineRpCtxt {
604604
token::get_ident_interner()),
605605
ast_map::node_id_to_str(self.ast_map, self.item_id,
606606
token::get_ident_interner()),
607-
copy self.ambient_variance);
607+
self.ambient_variance);
608608
let vec = do self.dep_map.find_or_insert_with(from) |_| {
609609
@mut ~[]
610610
};

0 commit comments

Comments
 (0)