diff options
author | Rohan Prinja <rohan.prinja@gmail.com> | 2014-12-31 19:36:13 +0530 |
---|---|---|
committer | Rohan Prinja <rohan.prinja@gmail.com> | 2015-01-01 13:50:55 +0530 |
commit | 7ac58f202f44bebf1021ff523a3ac913dcfe0b4c (patch) | |
tree | 5a2c525e6cb7bacaf7a5037f29f42c9d0cec3e0d /components/plugins/lints/privatize.rs | |
parent | 0da57abec6014f8a6edde9781598053deab996c7 (diff) | |
download | servo-7ac58f202f44bebf1021ff523a3ac913dcfe0b4c.tar.gz servo-7ac58f202f44bebf1021ff523a3ac913dcfe0b4c.zip |
break up lints.rs into separate files
Diffstat (limited to 'components/plugins/lints/privatize.rs')
-rw-r--r-- | components/plugins/lints/privatize.rs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/components/plugins/lints/privatize.rs b/components/plugins/lints/privatize.rs new file mode 100644 index 00000000000..07e7ca594da --- /dev/null +++ b/components/plugins/lints/privatize.rs @@ -0,0 +1,38 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use syntax::{ast, ast_util}; +use syntax::ast::Public; +use syntax::attr::AttrMetaMethods; +use rustc::lint::{Context, LintPass, LintArray}; +use rustc::middle::ty; +use rustc::middle::typeck::astconv::AstConv; + +declare_lint!(PRIVATIZE, Deny, + "Allows to enforce private fields for struct definitions") + +/// Lint for keeping DOM fields private +/// +/// This lint (disable with `-A privatize`/`#[allow(privatize)]`) ensures all types marked with `#[privatize]` have no private fields +pub struct PrivatizePass; + +impl LintPass for PrivatizePass { + fn get_lints(&self) -> LintArray { + lint_array!(PRIVATIZE) + } + + fn check_struct_def(&mut self, cx: &Context, def: &ast::StructDef, _i: ast::Ident, _gen: &ast::Generics, id: ast::NodeId) { + if ty::has_attr(cx.tcx, ast_util::local_def(id), "privatize") { + for field in def.fields.iter() { + match field.node { + ast::StructField_ { kind: ast::NamedField(ident, visibility), .. } if visibility == Public => { + cx.span_lint(PRIVATIZE, field.span, + format!("Field {} is public where only private fields are allowed", ident.name).as_slice()); + } + _ => {} + } + } + } + } +} |