]> git.lizzy.rs Git - rust.git/commitdiff
librustc: Implement a lint mode for mutable structures; deny by default. r=tjc
authorPatrick Walton <pcwalton@mimiga.net>
Mon, 25 Feb 2013 19:34:16 +0000 (11:34 -0800)
committerPatrick Walton <pcwalton@mimiga.net>
Tue, 26 Feb 2013 12:18:11 +0000 (04:18 -0800)
src/libcore/core.rc
src/librustc/middle/lint.rs
src/libstd/std.rc
src/test/run-pass/cycle-collection4.rs
src/test/run-pass/issue-1989.rs
src/test/run-pass/issue-980.rs
src/test/run-pass/private-method.rs
src/test/run-pass/uniq-cc-generic.rs
src/test/run-pass/uniq-cc.rs

index 01669557389ae575dc893296c9dce9b33cfc0af5..829651ae9092553b4ece969dcdcfa61e47948fe6 100644 (file)
@@ -51,6 +51,7 @@ Implicitly, all crates behave as if they included the following prologue:
 #[warn(vecs_implicitly_copyable)];
 #[deny(non_camel_case_types)];
 #[allow(deprecated_self)];
+#[allow(deprecated_mutable_fields)];
 
 /* The Prelude. */
 
index a25bc84f8a214d5a4cbc0feb9c8812dcc9d7e2bd..f4c3a1e8d12613062b1ea4590b07fc3274bc67d5 100644 (file)
@@ -80,6 +80,7 @@ pub enum lint {
     type_limits,
     default_methods,
     deprecated_self,
+    deprecated_mutable_fields,
 
     managed_heap_memory,
     owned_heap_memory,
@@ -254,6 +255,13 @@ pub fn get_lint_dict() -> LintDict {
             default: warn
          }),
 
+        (@~"deprecated_mutable_fields",
+         @LintSpec {
+            lint: deprecated_mutable_fields,
+            desc: "deprecated mutable fields in structures",
+            default: deny
+        }),
+
         /* FIXME(#3266)--make liveness warnings lintable
         (@~"unused_variable",
          @LintSpec {
@@ -486,6 +494,7 @@ fn check_item(i: @ast::item, cx: ty::ctxt) {
     check_item_type_limits(cx, i);
     check_item_default_methods(cx, i);
     check_item_deprecated_self(cx, i);
+    check_item_deprecated_mutable_fields(cx, i);
 }
 
 // Take a visitor, and modify it so that it will not proceed past subitems.
@@ -703,6 +712,26 @@ fn maybe_warn(cx: ty::ctxt,
     }
 }
 
+fn check_item_deprecated_mutable_fields(cx: ty::ctxt, item: @ast::item) {
+    match item.node {
+        ast::item_struct(struct_def, _) => {
+            for struct_def.fields.each |field| {
+                match field.node.kind {
+                    ast::named_field(_, ast::struct_mutable, _) => {
+                        cx.sess.span_lint(deprecated_mutable_fields,
+                                          item.id,
+                                          item.id,
+                                          field.span,
+                                          ~"mutable fields are deprecated");
+                    }
+                    ast::named_field(*) | ast::unnamed_field => {}
+                }
+            }
+        }
+        _ => {}
+    }
+}
+
 fn check_item_structural_records(cx: ty::ctxt, it: @ast::item) {
     let visit = item_stopping_visitor(
         visit::mk_simple_visitor(@visit::SimpleVisitor {
index 1ece8c17ff7a794144edb000c57db609f914ce7a..b6d99800aa2a39dce43580c1ced478055b32bed5 100644 (file)
@@ -29,6 +29,7 @@ not required in or otherwise suitable for the core library.
 #[allow(vecs_implicitly_copyable)];
 #[deny(non_camel_case_types)];
 #[allow(deprecated_self)];
+#[allow(deprecated_mutable_fields)];
 
 #[no_core];
 
index 4be43fc1296ed203a3bb408f0a1b537989ed1299..8a3139157fdf602dc59e9ad40a60d73c82eb1fa2 100644 (file)
@@ -8,13 +8,13 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-struct foo { mut z : fn@() }
+struct foo { z : fn@() }
 
 fn nop() { }
-fn nop_foo(_y: ~[int], _x : @foo) { }
+fn nop_foo(_y: ~[int], _x : @mut foo) { }
 
 pub fn main() {
-    let w = @foo{ z: || nop() };
+    let w = @mut foo{ z: || nop() };
     let x : fn@() = || nop_foo(~[], w);
     w.z = x;
 }
index f941a9002ef74175cd57340862bab6b0c0d91e98..95129851d5bcd979281e2ca0337db6b2d05b73cb 100644 (file)
 
 enum maybe_pointy {
     none,
-    p(@Pointy)
+    p(@mut Pointy)
 }
 
 struct Pointy {
-    mut a : maybe_pointy,
-    mut f : fn@()->(),
+    a : maybe_pointy,
+    f : fn@()->(),
 }
 
-fn empty_pointy() -> @Pointy {
-    return @Pointy{
-        mut a : none,
-        mut f : fn@()->(){},
+fn empty_pointy() -> @mut Pointy {
+    return @mut Pointy{
+        a : none,
+        f : fn@()->(){},
     }
 }
 
-pub fn main()
-{
+pub fn main() {
     let v = ~[empty_pointy(), empty_pointy()];
     v[0].a = p(v[0]);
 }
index 842eabf8fc50235d3d1f13af860a7651911feb48..e2e5dfcc7d5195bb4193c1abfc97fa9a17ec8a8c 100644 (file)
 
 enum maybe_pointy {
     no_pointy,
-    yes_pointy(@Pointy),
+    yes_pointy(@mut Pointy),
 }
 
 struct Pointy {
-    mut x : maybe_pointy
+    x : maybe_pointy
 }
 
 pub fn main() {
-    let m = @Pointy { mut x : no_pointy };
+    let m = @mut Pointy { x : no_pointy };
     m.x = yes_pointy(m);
 }
index ae149421f0a163aa80f6542f53b76daeb26070e7..432c189ae423c742537d60264217430fa6edc524 100644 (file)
@@ -9,20 +9,20 @@
 // except according to those terms.
 
 struct cat {
-  priv mut meows : uint,
+    priv meows : uint,
 
-  how_hungry : int,
+    how_hungry : int,
 }
 
 impl cat {
-  fn play() {
-    self.meows += 1u;
-    self.nap();
-  }
+    fn play(&mut self) {
+        self.meows += 1u;
+        self.nap();
+    }
 }
 
 priv impl cat {
-    fn nap() { for uint::range(1u, 10u) |_i| { }}
+    fn nap(&mut self) { for uint::range(1u, 10u) |_i| { }}
 }
 
 fn cat(in_x : uint, in_y : int) -> cat {
@@ -33,6 +33,6 @@ fn cat(in_x : uint, in_y : int) -> cat {
 }
 
 pub fn main() {
-  let nyan : cat = cat(52u, 99);
+  let mut nyan : cat = cat(52u, 99);
   nyan.play();
 }
index dd86150b93bf9a5ff0caaca05073abdec34a267d..1b602ab7d3009bb8560d20dfdeb607d20098a38e 100644 (file)
 
 enum maybe_pointy {
     none,
-    p(@Pointy),
+    p(@mut Pointy),
 }
 
 struct Pointy {
-    mut a : maybe_pointy,
+    a : maybe_pointy,
     d : fn~() -> uint,
 }
 
@@ -22,15 +22,14 @@ fn make_uniq_closure<A:Owned + Copy>(a: A) -> fn~() -> uint {
     fn~() -> uint { ptr::addr_of(&a) as uint }
 }
 
-fn empty_pointy() -> @Pointy {
-    return @Pointy {
+fn empty_pointy() -> @mut Pointy {
+    return @mut Pointy {
         mut a : none,
         d : make_uniq_closure(~"hi")
     }
 }
 
-pub fn main()
-{
+pub fn main() {
     let v = empty_pointy();
     v.a = p(v);
 }
index 384450ec57feddd591b7b301ec58a55f6e1ef447..3d72a41182868076cf16446004d4c5c7331bbafa 100644 (file)
 
 enum maybe_pointy {
     none,
-    p(@Pointy),
+    p(@mut Pointy),
 }
 
 struct Pointy {
-    mut a : maybe_pointy,
+    a : maybe_pointy,
     c : ~int,
     d : fn~()->(),
 }
 
-fn empty_pointy() -> @Pointy {
-    return @Pointy {
-        mut a : none,
+fn empty_pointy() -> @mut Pointy {
+    return @mut Pointy {
+        a : none,
         c : ~22,
         d : fn~()->(){},
     }
 }
 
-pub fn main()
-{
+pub fn main() {
     let v = empty_pointy();
     v.a = p(v);
 }