rust/tests/ui-fulldeps/auxiliary/lint-for-crate.rs

46 lines
1.1 KiB
Rust
Raw Normal View History

// force-host
#![feature(rustc_private)]
2019-07-06 19:56:20 +02:00
extern crate rustc_driver;
2020-01-05 05:35:51 +01:00
extern crate rustc_hir;
#[macro_use]
extern crate rustc_lint;
#[macro_use]
extern crate rustc_session;
extern crate rustc_ast;
2021-09-12 13:19:04 +02:00
extern crate rustc_span;
use rustc_driver::plugin::Registry;
2021-09-12 13:19:04 +02:00
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_span::def_id::CRATE_DEF_ID;
2020-01-02 00:01:07 +01:00
use rustc_span::symbol::Symbol;
declare_lint! {
CRATE_NOT_OKAY,
Warn,
"crate not marked with #![crate_okay]"
}
declare_lint_pass!(Pass => [CRATE_NOT_OKAY]);
impl<'tcx> LateLintPass<'tcx> for Pass {
2021-09-12 13:19:04 +02:00
fn check_crate(&mut self, cx: &LateContext) {
2020-12-05 21:23:37 +01:00
let attrs = cx.tcx.hir().attrs(rustc_hir::CRATE_HIR_ID);
2021-09-12 13:19:04 +02:00
let span = cx.tcx.def_span(CRATE_DEF_ID);
2020-12-05 21:23:37 +01:00
if !cx.sess().contains_name(attrs, Symbol::intern("crate_okay")) {
cx.lint(
CRATE_NOT_OKAY,
"crate is not marked with #![crate_okay]",
|lint| lint.set_span(span)
);
}
}
}
#[no_mangle]
fn __rustc_plugin_registrar(reg: &mut Registry) {
2019-10-09 15:41:17 -04:00
reg.lint_store.register_lints(&[&CRATE_NOT_OKAY]);
reg.lint_store.register_late_pass(|_| Box::new(Pass));
}