Skip to content

Commit 3147259

Browse files
committed
librustc: Implement a lint mode for mutable structures; deny by default. r=tjc
1 parent 00d8db5 commit 3147259

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
@@ -81,6 +81,7 @@ pub enum lint {
8181
type_limits,
8282
default_methods,
8383
deprecated_self,
84+
deprecated_mutable_fields,
8485

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

259+
(@~"deprecated_mutable_fields",
260+
@LintSpec {
261+
lint: deprecated_mutable_fields,
262+
desc: "deprecated mutable fields in structures",
263+
default: deny
264+
}),
265+
258266
/* FIXME(#3266)--make liveness warnings lintable
259267
(@~"unused_variable",
260268
@LintSpec {
@@ -488,6 +496,7 @@ fn check_item(i: @ast::item, cx: ty::ctxt) {
488496
check_item_type_limits(cx, i);
489497
check_item_default_methods(cx, i);
490498
check_item_deprecated_self(cx, i);
499+
check_item_deprecated_mutable_fields(cx, i);
491500
}
492501

493502
// Take a visitor, and modify it so that it will not proceed past subitems.
@@ -705,6 +714,26 @@ fn check_item_deprecated_self(cx: ty::ctxt, item: @ast::item) {
705714
}
706715
}
707716
717+
fn check_item_deprecated_mutable_fields(cx: ty::ctxt, item: @ast::item) {
718+
match item.node {
719+
ast::item_struct(struct_def, _) => {
720+
for struct_def.fields.each |field| {
721+
match field.node.kind {
722+
ast::named_field(_, ast::struct_mutable, _) => {
723+
cx.sess.span_lint(deprecated_mutable_fields,
724+
item.id,
725+
item.id,
726+
field.span,
727+
~"mutable fields are deprecated");
728+
}
729+
ast::named_field(*) | ast::unnamed_field => {}
730+
}
731+
}
732+
}
733+
_ => {}
734+
}
735+
}
736+
708737
fn check_item_structural_records(cx: ty::ctxt, it: @ast::item) {
709738
let visit = item_stopping_visitor(
710739
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)