summaryrefslogtreecommitdiffstats
path: root/tests/ui-fulldeps/plugin/auxiliary/issue-40001-plugin.rs
blob: 3f6caecaa5ad0de40e27f2833a550309300839a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#![feature(plugin, rustc_private)]
#![crate_type = "dylib"]

extern crate rustc_ast_pretty;
extern crate rustc_driver;
extern crate rustc_hir;
extern crate rustc_lint;
#[macro_use]
extern crate rustc_session;
extern crate rustc_ast;
extern crate rustc_span;

use rustc_ast_pretty::pprust;
use rustc_driver::plugin::Registry;
use rustc_hir as hir;
use rustc_hir::intravisit;
use rustc_hir::Node;
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_span::def_id::LocalDefId;
use rustc_span::source_map;

#[no_mangle]
fn __rustc_plugin_registrar(reg: &mut Registry) {
    reg.lint_store.register_lints(&[&MISSING_ALLOWED_ATTR]);
    reg.lint_store.register_late_pass(|_| Box::new(MissingAllowedAttrPass));
}

declare_lint! {
    MISSING_ALLOWED_ATTR,
    Deny,
    "Checks for missing `allowed_attr` attribute"
}

declare_lint_pass!(MissingAllowedAttrPass => [MISSING_ALLOWED_ATTR]);

impl<'tcx> LateLintPass<'tcx> for MissingAllowedAttrPass {
    fn check_fn(
        &mut self,
        cx: &LateContext<'tcx>,
        _: intravisit::FnKind<'tcx>,
        _: &'tcx hir::FnDecl,
        _: &'tcx hir::Body,
        span: source_map::Span,
        def_id: LocalDefId,
    ) {
        let id = cx.tcx.hir().local_def_id_to_hir_id(def_id);
        let item = match cx.tcx.hir().get(id) {
            Node::Item(item) => item,
            _ => cx.tcx.hir().expect_item(cx.tcx.hir().get_parent_item(id).def_id),
        };

        let allowed = |attr| pprust::attribute_to_string(attr).contains("allowed_attr");
        if !cx.tcx.hir().attrs(item.hir_id()).iter().any(allowed) {
            cx.lint(
                MISSING_ALLOWED_ATTR,
                "Missing 'allowed_attr' attribute",
                |lint| lint.set_span(span)
            );
        }
    }
}