rust/tests/ui-fulldeps/plugin/auxiliary/issue-40001-plugin.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

62 lines
1.7 KiB
Rust
Raw Normal View History

#![feature(plugin, rustc_private)]
#![crate_type = "dylib"]
2020-01-18 19:21:05 +01:00
extern crate rustc_ast_pretty;
2019-07-06 19:56:20 +02:00
extern crate rustc_driver;
2020-01-05 05:35:51 +01:00
extern crate rustc_hir;
extern crate rustc_lint;
#[macro_use]
extern crate rustc_session;
extern crate rustc_ast;
2021-10-20 22:38:10 +02:00
extern crate rustc_span;
2020-01-18 19:21:05 +01:00
use rustc_ast_pretty::pprust;
use rustc_driver::plugin::Registry;
2020-01-05 05:35:51 +01:00
use rustc_hir as hir;
use rustc_hir::intravisit;
2020-01-05 05:35:51 +01:00
use rustc_hir::Node;
use rustc_lint::{LateContext, LateLintPass, LintContext};
2023-01-22 18:45:37 +00:00
use rustc_span::def_id::LocalDefId;
2020-01-02 00:01:07 +01:00
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,
2023-01-22 18:45:37 +00:00
def_id: LocalDefId,
) {
2023-01-22 18:45:37 +00:00
let id = cx.tcx.hir().local_def_id_to_hir_id(def_id);
2019-06-20 10:39:19 +02:00
let item = match cx.tcx.hir().get(id) {
2018-08-25 15:56:16 +01:00
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");
2021-02-19 09:07:58 +01:00
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)
);
}
}
}