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