2021-08-24 19:39:40 -05:00
|
|
|
#![feature(plugin, rustc_private)]
|
2017-02-22 13:15:12 -06:00
|
|
|
#![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;
|
2020-02-06 04:22:25 -06:00
|
|
|
extern crate rustc_lint;
|
|
|
|
#[macro_use]
|
|
|
|
extern crate rustc_session;
|
2020-02-29 11:37:32 -06:00
|
|
|
extern crate rustc_ast;
|
2021-10-20 15:38:10 -05:00
|
|
|
extern crate rustc_span;
|
2017-02-22 13:15:12 -06:00
|
|
|
|
2020-01-18 12:21:05 -06:00
|
|
|
use rustc_ast_pretty::pprust;
|
2020-02-06 04:22:25 -06:00
|
|
|
use rustc_driver::plugin::Registry;
|
2020-01-04 22:35:51 -06:00
|
|
|
use rustc_hir as hir;
|
2020-02-06 04:22:25 -06:00
|
|
|
use rustc_hir::intravisit;
|
2020-01-04 22:35:51 -06:00
|
|
|
use rustc_hir::Node;
|
2021-05-14 08:37:53 -05:00
|
|
|
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;
|
2017-02-22 13:15:12 -06:00
|
|
|
|
2021-05-14 08:37:53 -05:00
|
|
|
#[no_mangle]
|
|
|
|
fn __rustc_plugin_registrar(reg: &mut Registry) {
|
2020-07-07 10:12:44 -05:00
|
|
|
reg.lint_store.register_lints(&[&MISSING_ALLOWED_ATTR]);
|
2022-09-06 13:23:03 -05:00
|
|
|
reg.lint_store.register_late_pass(|_| Box::new(MissingAllowedAttrPass));
|
2017-02-22 13:15:12 -06:00
|
|
|
}
|
|
|
|
|
2019-04-03 09:05:40 -05:00
|
|
|
declare_lint! {
|
2020-07-07 10:12:44 -05:00
|
|
|
MISSING_ALLOWED_ATTR,
|
2019-04-03 09:05:40 -05:00
|
|
|
Deny,
|
2020-07-07 10:12:44 -05:00
|
|
|
"Checks for missing `allowed_attr` attribute"
|
2017-02-22 13:15:12 -06:00
|
|
|
}
|
|
|
|
|
2020-07-07 10:12:44 -05:00
|
|
|
declare_lint_pass!(MissingAllowedAttrPass => [MISSING_ALLOWED_ATTR]);
|
2019-04-03 09:05:40 -05:00
|
|
|
|
2020-07-07 10:12:44 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for MissingAllowedAttrPass {
|
2020-02-06 04:22:25 -06:00
|
|
|
fn check_fn(
|
|
|
|
&mut self,
|
2020-06-25 15:41:36 -05:00
|
|
|
cx: &LateContext<'tcx>,
|
2020-02-06 04:22:25 -06:00
|
|
|
_: intravisit::FnKind<'tcx>,
|
|
|
|
_: &'tcx hir::FnDecl,
|
|
|
|
_: &'tcx hir::Body,
|
|
|
|
span: source_map::Span,
|
2023-01-22 12:45:37 -06:00
|
|
|
def_id: LocalDefId,
|
2020-02-06 04:22:25 -06:00
|
|
|
) {
|
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,
|
2022-09-20 00:11:23 -05:00
|
|
|
_ => cx.tcx.hir().expect_item(cx.tcx.hir().get_parent_item(id).def_id),
|
2017-02-22 13:15:12 -06:00
|
|
|
};
|
|
|
|
|
2020-07-07 10:12:44 -05:00
|
|
|
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) {
|
2022-09-22 09:25:05 -05:00
|
|
|
cx.lint(
|
|
|
|
MISSING_ALLOWED_ATTR,
|
|
|
|
"Missing 'allowed_attr' attribute",
|
|
|
|
|lint| lint.set_span(span)
|
|
|
|
);
|
2017-02-22 13:15:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|