]> git.lizzy.rs Git - rust.git/blob - tests/ui-fulldeps/auxiliary/issue-40001-plugin.rs
Rollup merge of #106321 - compiler-errors:delayed-bug-backtrace, r=Nilstrieb
[rust.git] / tests / ui-fulldeps / auxiliary / issue-40001-plugin.rs
1 #![feature(plugin, rustc_private)]
2 #![crate_type = "dylib"]
3
4 extern crate rustc_ast_pretty;
5 extern crate rustc_driver;
6 extern crate rustc_hir;
7 extern crate rustc_lint;
8 #[macro_use]
9 extern crate rustc_session;
10 extern crate rustc_ast;
11 extern crate rustc_span;
12
13 use rustc_ast_pretty::pprust;
14 use rustc_driver::plugin::Registry;
15 use rustc_hir as hir;
16 use rustc_hir::intravisit;
17 use rustc_hir::Node;
18 use rustc_lint::{LateContext, LateLintPass, LintContext};
19 use rustc_span::source_map;
20
21 #[no_mangle]
22 fn __rustc_plugin_registrar(reg: &mut Registry) {
23     reg.lint_store.register_lints(&[&MISSING_ALLOWED_ATTR]);
24     reg.lint_store.register_late_pass(|_| Box::new(MissingAllowedAttrPass));
25 }
26
27 declare_lint! {
28     MISSING_ALLOWED_ATTR,
29     Deny,
30     "Checks for missing `allowed_attr` attribute"
31 }
32
33 declare_lint_pass!(MissingAllowedAttrPass => [MISSING_ALLOWED_ATTR]);
34
35 impl<'tcx> LateLintPass<'tcx> for MissingAllowedAttrPass {
36     fn check_fn(
37         &mut self,
38         cx: &LateContext<'tcx>,
39         _: intravisit::FnKind<'tcx>,
40         _: &'tcx hir::FnDecl,
41         _: &'tcx hir::Body,
42         span: source_map::Span,
43         id: hir::HirId,
44     ) {
45         let item = match cx.tcx.hir().get(id) {
46             Node::Item(item) => item,
47             _ => cx.tcx.hir().expect_item(cx.tcx.hir().get_parent_item(id).def_id),
48         };
49
50         let allowed = |attr| pprust::attribute_to_string(attr).contains("allowed_attr");
51         if !cx.tcx.hir().attrs(item.hir_id()).iter().any(allowed) {
52             cx.lint(
53                 MISSING_ALLOWED_ATTR,
54                 "Missing 'allowed_attr' attribute",
55                 |lint| lint.set_span(span)
56             );
57         }
58     }
59 }