]> git.proxmox.com Git - rustc.git/blame - src/test/ui-fulldeps/auxiliary/issue-40001-plugin.rs
New upstream version 1.52.0~beta.3+dfsg1
[rustc.git] / src / test / ui-fulldeps / auxiliary / issue-40001-plugin.rs
CommitLineData
8bb4bdeb
XL
1#![feature(box_syntax, plugin, plugin_registrar, rustc_private)]
2#![crate_type = "dylib"]
3
74b04a01 4extern crate rustc_ast_pretty;
416331ca 5extern crate rustc_driver;
dfeec247 6extern crate rustc_hir;
74b04a01
XL
7#[macro_use]
8extern crate rustc_lint;
9#[macro_use]
10extern crate rustc_session;
dfeec247 11extern crate rustc_span;
74b04a01 12extern crate rustc_ast;
8bb4bdeb 13
74b04a01
XL
14use rustc_ast_pretty::pprust;
15use rustc_driver::plugin::Registry;
dfeec247 16use rustc_hir as hir;
74b04a01 17use rustc_hir::intravisit;
dfeec247 18use rustc_hir::Node;
74b04a01 19use rustc_lint::{LateContext, LateLintPass, LintArray, LintContext, LintPass};
dfeec247 20use rustc_span::source_map;
8bb4bdeb
XL
21
22#[plugin_registrar]
23pub 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 28declare_lint! {
f035d41b 29 MISSING_ALLOWED_ATTR,
532ac7d7 30 Deny,
f035d41b 31 "Checks for missing `allowed_attr` attribute"
8bb4bdeb
XL
32}
33
f035d41b 34declare_lint_pass!(MissingAllowedAttrPass => [MISSING_ALLOWED_ATTR]);
532ac7d7 35
f035d41b 36impl<'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}