]>
Commit | Line | Data |
---|---|---|
8bb4bdeb XL |
1 | #![feature(box_syntax, plugin, plugin_registrar, rustc_private)] |
2 | #![crate_type = "dylib"] | |
3 | ||
74b04a01 | 4 | extern crate rustc_ast_pretty; |
416331ca | 5 | extern crate rustc_driver; |
dfeec247 | 6 | extern crate rustc_hir; |
74b04a01 XL |
7 | #[macro_use] |
8 | extern crate rustc_lint; | |
9 | #[macro_use] | |
10 | extern crate rustc_session; | |
dfeec247 | 11 | extern crate rustc_span; |
74b04a01 | 12 | extern crate rustc_ast; |
8bb4bdeb | 13 | |
74b04a01 XL |
14 | use rustc_ast_pretty::pprust; |
15 | use rustc_driver::plugin::Registry; | |
dfeec247 | 16 | use rustc_hir as hir; |
74b04a01 | 17 | use rustc_hir::intravisit; |
dfeec247 | 18 | use rustc_hir::Node; |
74b04a01 | 19 | use rustc_lint::{LateContext, LateLintPass, LintArray, LintContext, LintPass}; |
dfeec247 | 20 | use rustc_span::source_map; |
8bb4bdeb XL |
21 | |
22 | #[plugin_registrar] | |
23 | pub fn plugin_registrar(reg: &mut Registry) { | |
f035d41b XL |
24 | reg.lint_store.register_lints(&[&MISSING_ALLOWED_ATTR]); |
25 | reg.lint_store.register_late_pass(|| box MissingAllowedAttrPass); | |
8bb4bdeb XL |
26 | } |
27 | ||
532ac7d7 | 28 | declare_lint! { |
f035d41b | 29 | MISSING_ALLOWED_ATTR, |
532ac7d7 | 30 | Deny, |
f035d41b | 31 | "Checks for missing `allowed_attr` attribute" |
8bb4bdeb XL |
32 | } |
33 | ||
f035d41b | 34 | declare_lint_pass!(MissingAllowedAttrPass => [MISSING_ALLOWED_ATTR]); |
532ac7d7 | 35 | |
f035d41b | 36 | impl<'tcx> LateLintPass<'tcx> for MissingAllowedAttrPass { |
74b04a01 XL |
37 | fn check_fn( |
38 | &mut self, | |
f035d41b | 39 | cx: &LateContext<'tcx>, |
74b04a01 XL |
40 | _: intravisit::FnKind<'tcx>, |
41 | _: &'tcx hir::FnDecl, | |
42 | _: &'tcx hir::Body, | |
43 | span: source_map::Span, | |
44 | id: hir::HirId, | |
45 | ) { | |
dc9dc135 | 46 | let item = match cx.tcx.hir().get(id) { |
b7449926 | 47 | Node::Item(item) => item, |
dc9dc135 | 48 | _ => cx.tcx.hir().expect_item(cx.tcx.hir().get_parent_item(id)), |
8bb4bdeb XL |
49 | }; |
50 | ||
f035d41b | 51 | let allowed = |attr| pprust::attribute_to_string(attr).contains("allowed_attr"); |
6a06907d | 52 | if !cx.tcx.hir().attrs(item.hir_id()).iter().any(allowed) { |
f035d41b XL |
53 | cx.lint(MISSING_ALLOWED_ATTR, |lint| { |
54 | lint.build("Missing 'allowed_attr' attribute").set_span(span).emit() | |
74b04a01 | 55 | }); |
8bb4bdeb XL |
56 | } |
57 | } | |
58 | } |