Check unnecessary visibility for struct variants

This commit is contained in:
Steven Fackler 2013-10-18 22:43:16 -07:00
parent 3a7337ff17
commit c18afcd83a
2 changed files with 39 additions and 13 deletions

View File

@ -563,6 +563,20 @@ fn check_sane_privacy(&self, item: @ast::item) {
}
}
};
let check_struct = |def: &@ast::struct_def| {
for f in def.fields.iter() {
match f.node.kind {
ast::named_field(_, ast::public) => {
tcx.sess.span_err(f.span, "unnecessary `pub` \
visibility");
}
ast::named_field(_, ast::private) => {
// Fields should really be private by default...
}
ast::named_field(*) | ast::unnamed_field => {}
}
}
};
match item.node {
// implementations of traits don't need visibility qualifiers because
// that's controlled by having the trait in scope.
@ -610,24 +624,16 @@ fn check_sane_privacy(&self, item: @ast::item) {
}
ast::inherited => {}
}
}
}
ast::item_struct(ref def, _) => {
for f in def.fields.iter() {
match f.node.kind {
ast::named_field(_, ast::public) => {
tcx.sess.span_err(f.span, "unnecessary `pub` \
visibility");
}
ast::named_field(_, ast::private) => {
// Fields should really be private by default...
}
ast::named_field(*) | ast::unnamed_field => {}
match v.node.kind {
ast::struct_variant_kind(ref s) => check_struct(s),
ast::tuple_variant_kind(*) => {}
}
}
}
ast::item_struct(ref def, _) => check_struct(def),
ast::item_trait(_, _, ref methods) => {
for m in methods.iter() {
match *m {

View File

@ -0,0 +1,20 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#[feature(struct_variant)];
pub enum Foo {
Bar {
pub x: int, //~ ERROR unnecessary `pub` visibility
y: int,
priv z: int
}
}
fn main() {}