|
| 1 | +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | +#![feature(box_syntax, plugin, plugin_registrar, rustc_private)] |
| 11 | +#![crate_type = "dylib"] |
| 12 | + |
| 13 | +#[macro_use] |
| 14 | +extern crate rustc; |
| 15 | +extern crate rustc_plugin; |
| 16 | +extern crate syntax; |
| 17 | + |
| 18 | +use rustc_plugin::Registry; |
| 19 | +use syntax::ext::base::*; |
| 20 | +use syntax::feature_gate::AttributeType::Whitelisted; |
| 21 | +use syntax::symbol::Symbol; |
| 22 | + |
| 23 | +use rustc::hir; |
| 24 | +use rustc::hir::intravisit; |
| 25 | +use rustc::hir::map as hir_map; |
| 26 | +use rustc::lint::{LateContext, LintPass, LintArray, LateLintPass, LintContext}; |
| 27 | +use rustc::ty; |
| 28 | +use syntax::{ast, codemap}; |
| 29 | + |
| 30 | +#[plugin_registrar] |
| 31 | +pub fn plugin_registrar(reg: &mut Registry) { |
| 32 | + reg.register_late_lint_pass(box MissingWhitelistedAttrPass); |
| 33 | + reg.register_attribute("whitelisted_attr".to_string(), Whitelisted); |
| 34 | +} |
| 35 | + |
| 36 | +declare_lint!(MISSING_WHITELISTED_ATTR, Deny, |
| 37 | + "Checks for missing `whitelisted_attr` attribute"); |
| 38 | + |
| 39 | +struct MissingWhitelistedAttrPass; |
| 40 | + |
| 41 | +impl LintPass for MissingWhitelistedAttrPass { |
| 42 | + fn get_lints(&self) -> LintArray { |
| 43 | + lint_array!(MISSING_WHITELISTED_ATTR) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingWhitelistedAttrPass { |
| 48 | + fn check_fn(&mut self, |
| 49 | + cx: &LateContext<'a, 'tcx>, |
| 50 | + _: intravisit::FnKind<'tcx>, |
| 51 | + _: &'tcx hir::FnDecl, |
| 52 | + _: &'tcx hir::Body, |
| 53 | + span: codemap::Span, |
| 54 | + id: ast::NodeId) { |
| 55 | + |
| 56 | + let item = match cx.tcx.hir.get(id) { |
| 57 | + hir_map::Node::NodeItem(item) => item, |
| 58 | + _ => cx.tcx.hir.expect_item(cx.tcx.hir.get_parent(id)), |
| 59 | + }; |
| 60 | + |
| 61 | + if !item.attrs.iter().any(|a| a.check_name("whitelisted_attr")) { |
| 62 | + cx.span_lint(MISSING_WHITELISTED_ATTR, span, |
| 63 | + "Missing 'whitelited_attr' attribute"); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments