]> git.lizzy.rs Git - rust.git/blob - tests/ui-fulldeps/auxiliary/lint-for-crate-rpass.rs
Rollup merge of #106664 - chenyukang:yukang/fix-106597-remove-lseek, r=cuviper
[rust.git] / tests / ui-fulldeps / auxiliary / lint-for-crate-rpass.rs
1 // force-host
2
3 #![feature(rustc_private)]
4
5 extern crate rustc_driver;
6 extern crate rustc_hir;
7 extern crate rustc_lint;
8 extern crate rustc_span;
9 #[macro_use]
10 extern crate rustc_session;
11 extern crate rustc_ast;
12
13 use rustc_ast::attr;
14 use rustc_driver::plugin::Registry;
15 use rustc_lint::{LateContext, LateLintPass, LintContext, LintPass};
16 use rustc_span::def_id::CRATE_DEF_ID;
17 use rustc_span::symbol::Symbol;
18
19 macro_rules! fake_lint_pass {
20     ($struct:ident, $($attr:expr),*) => {
21         struct $struct;
22
23         impl LintPass for $struct {
24             fn name(&self) -> &'static str {
25                 stringify!($struct)
26             }
27         }
28
29         impl LateLintPass<'_> for $struct {
30             fn check_crate(&mut self, cx: &LateContext) {
31                 let attrs = cx.tcx.hir().attrs(rustc_hir::CRATE_HIR_ID);
32                 let span = cx.tcx.def_span(CRATE_DEF_ID);
33                 $(
34                     if !cx.sess().contains_name(attrs, $attr) {
35                         cx.lint(CRATE_NOT_OKAY, |lint| {
36                              let msg = format!("crate is not marked with #![{}]", $attr);
37                              lint.build(&msg).set_span(span).emit();
38                         });
39                     }
40                 )*
41             }
42         }
43
44     }
45 }
46
47 declare_lint!(CRATE_NOT_OKAY, Warn, "crate not marked with #![crate_okay]");
48 declare_lint!(CRATE_NOT_RED, Warn, "crate not marked with #![crate_red]");
49 declare_lint!(CRATE_NOT_BLUE, Warn, "crate not marked with #![crate_blue]");
50 declare_lint!(CRATE_NOT_GREY, Warn, "crate not marked with #![crate_grey]");
51 declare_lint!(CRATE_NOT_GREEN, Warn, "crate not marked with #![crate_green]");
52
53 fake_lint_pass! {
54     PassOkay,
55     Symbol::intern("crate_okay")
56 }
57
58 fake_lint_pass! {
59     PassRedBlue,
60     Symbol::intern("crate_red"), Symbol::intern("crate_blue")
61 }
62
63 fake_lint_pass! {
64     PassGreyGreen,
65     Symbol::intern("crate_grey"), Symbol::intern("crate_green")
66 }
67
68 #[no_mangle]
69 fn __rustc_plugin_registrar(reg: &mut Registry) {
70     reg.lint_store.register_lints(&[
71         &CRATE_NOT_OKAY,
72         &CRATE_NOT_RED,
73         &CRATE_NOT_BLUE,
74         &CRATE_NOT_GREY,
75         &CRATE_NOT_GREEN,
76     ]);
77     reg.lint_store.register_late_pass(|_| Box::new(PassOkay));
78     reg.lint_store.register_late_pass(|_| Box::new(PassRedBlue));
79     reg.lint_store.register_late_pass(|_| Box::new(PassGreyGreen));
80 }