Skip to content

Commit c483aab

Browse files
committed
librustc: Implement a lint mode for mutable structures; deny by default. r=tjc
1 parent 9b8ce0d commit c483aab

File tree

9 files changed

+64
-36
lines changed

9 files changed

+64
-36
lines changed

src/libcore/core.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Implicitly, all crates behave as if they included the following prologue:
5151
#[warn(vecs_implicitly_copyable)];
5252
#[deny(non_camel_case_types)];
5353
#[allow(deprecated_self)];
54+
#[allow(deprecated_mutable_fields)];
5455

5556
/* The Prelude. */
5657

src/librustc/middle/lint.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ pub enum lint {
8080
type_limits,
8181
default_methods,
8282
deprecated_self,
83+
deprecated_mutable_fields,
8384

8485
managed_heap_memory,
8586
owned_heap_memory,
@@ -254,6 +255,13 @@ pub fn get_lint_dict() -> LintDict {
254255
default: warn
255256
}),
256257

258+
(@~"deprecated_mutable_fields",
259+
@LintSpec {
260+
lint: deprecated_mutable_fields,
261+
desc: "deprecated mutable fields in structures",
262+
default: deny
263+
}),
264+
257265
/* FIXME(#3266)--make liveness warnings lintable
258266
(@~"unused_variable",
259267
@LintSpec {
@@ -486,6 +494,7 @@ fn check_item(i: @ast::item, cx: ty::ctxt) {
486494
check_item_type_limits(cx, i);
487495
check_item_default_methods(cx, i);
488496
check_item_deprecated_self(cx, i);
497+
check_item_deprecated_mutable_fields(cx, i);
489498
}
490499

491500
// Take a visitor, and modify it so that it will not proceed past subitems.
@@ -703,6 +712,26 @@ fn check_item_deprecated_self(cx: ty::ctxt, item: @ast::item) {
703712
}
704713
}
705714
715+
fn check_item_deprecated_mutable_fields(cx: ty::ctxt, item: @ast::item) {
716+
match item.node {
717+
ast::item_struct(struct_def, _) => {
718+
for struct_def.fields.each |field| {
719+
match field.node.kind {
720+
ast::named_field(_, ast::struct_mutable, _) => {
721+
cx.sess.span_lint(deprecated_mutable_fields,
722+
item.id,
723+
item.id,
724+
field.span,
725+
~"mutable fields are deprecated");
726+
}
727+
ast::named_field(*) | ast::unnamed_field => {}
728+
}
729+
}
730+
}
731+
_ => {}
732+
}
733+
}
734+
706735
fn check_item_structural_records(cx: ty::ctxt, it: @ast::item) {
707736
let visit = item_stopping_visitor(
708737
visit::mk_simple_visitor(@visit::SimpleVisitor {

src/libstd/std.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ not required in or otherwise suitable for the core library.
2929
#[allow(vecs_implicitly_copyable)];
3030
#[deny(non_camel_case_types)];
3131
#[allow(deprecated_self)];
32+
#[allow(deprecated_mutable_fields)];
3233

3334
#[no_core];
3435

src/test/run-pass/cycle-collection4.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
struct foo { mut z : fn@() }
11+
struct foo { z : fn@() }
1212

1313
fn nop() { }
14-
fn nop_foo(_y: ~[int], _x : @foo) { }
14+
fn nop_foo(_y: ~[int], _x : @mut foo) { }
1515

1616
pub fn main() {
17-
let w = @foo{ z: || nop() };
17+
let w = @mut foo{ z: || nop() };
1818
let x : fn@() = || nop_foo(~[], w);
1919
w.z = x;
2020
}

src/test/run-pass/issue-1989.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,22 @@
1212

1313
enum maybe_pointy {
1414
none,
15-
p(@Pointy)
15+
p(@mut Pointy)
1616
}
1717

1818
struct Pointy {
19-
mut a : maybe_pointy,
20-
mut f : fn@()->(),
19+
a : maybe_pointy,
20+
f : fn@()->(),
2121
}
2222

23-
fn empty_pointy() -> @Pointy {
24-
return @Pointy{
25-
mut a : none,
26-
mut f : fn@()->(){},
23+
fn empty_pointy() -> @mut Pointy {
24+
return @mut Pointy{
25+
a : none,
26+
f : fn@()->(){},
2727
}
2828
}
2929

30-
pub fn main()
31-
{
30+
pub fn main() {
3231
let v = ~[empty_pointy(), empty_pointy()];
3332
v[0].a = p(v[0]);
3433
}

src/test/run-pass/issue-980.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010

1111
enum maybe_pointy {
1212
no_pointy,
13-
yes_pointy(@Pointy),
13+
yes_pointy(@mut Pointy),
1414
}
1515

1616
struct Pointy {
17-
mut x : maybe_pointy
17+
x : maybe_pointy
1818
}
1919

2020
pub fn main() {
21-
let m = @Pointy { mut x : no_pointy };
21+
let m = @mut Pointy { x : no_pointy };
2222
m.x = yes_pointy(m);
2323
}

src/test/run-pass/private-method.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@
99
// except according to those terms.
1010

1111
struct cat {
12-
priv mut meows : uint,
12+
priv meows : uint,
1313

14-
how_hungry : int,
14+
how_hungry : int,
1515
}
1616

1717
impl cat {
18-
fn play() {
19-
self.meows += 1u;
20-
self.nap();
21-
}
18+
fn play(&mut self) {
19+
self.meows += 1u;
20+
self.nap();
21+
}
2222
}
2323

2424
priv impl cat {
25-
fn nap() { for uint::range(1u, 10u) |_i| { }}
25+
fn nap(&mut self) { for uint::range(1u, 10u) |_i| { }}
2626
}
2727

2828
fn cat(in_x : uint, in_y : int) -> cat {
@@ -33,6 +33,6 @@ fn cat(in_x : uint, in_y : int) -> cat {
3333
}
3434

3535
pub fn main() {
36-
let nyan : cat = cat(52u, 99);
36+
let mut nyan : cat = cat(52u, 99);
3737
nyan.play();
3838
}

src/test/run-pass/uniq-cc-generic.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,26 @@
1010

1111
enum maybe_pointy {
1212
none,
13-
p(@Pointy),
13+
p(@mut Pointy),
1414
}
1515

1616
struct Pointy {
17-
mut a : maybe_pointy,
17+
a : maybe_pointy,
1818
d : fn~() -> uint,
1919
}
2020

2121
fn make_uniq_closure<A:Owned + Copy>(a: A) -> fn~() -> uint {
2222
fn~() -> uint { ptr::addr_of(&a) as uint }
2323
}
2424

25-
fn empty_pointy() -> @Pointy {
26-
return @Pointy {
25+
fn empty_pointy() -> @mut Pointy {
26+
return @mut Pointy {
2727
mut a : none,
2828
d : make_uniq_closure(~"hi")
2929
}
3030
}
3131

32-
pub fn main()
33-
{
32+
pub fn main() {
3433
let v = empty_pointy();
3534
v.a = p(v);
3635
}

src/test/run-pass/uniq-cc.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,24 @@
1010

1111
enum maybe_pointy {
1212
none,
13-
p(@Pointy),
13+
p(@mut Pointy),
1414
}
1515

1616
struct Pointy {
17-
mut a : maybe_pointy,
17+
a : maybe_pointy,
1818
c : ~int,
1919
d : fn~()->(),
2020
}
2121

22-
fn empty_pointy() -> @Pointy {
23-
return @Pointy {
24-
mut a : none,
22+
fn empty_pointy() -> @mut Pointy {
23+
return @mut Pointy {
24+
a : none,
2525
c : ~22,
2626
d : fn~()->(){},
2727
}
2828
}
2929

30-
pub fn main()
31-
{
30+
pub fn main() {
3231
let v = empty_pointy();
3332
v.a = p(v);
3433
}

0 commit comments

Comments
 (0)