]> git.lizzy.rs Git - rust.git/blob - src/test/ui-fulldeps/auxiliary/lint-for-crate-rpass.rs
Rollup merge of #82500 - CDirkx:hermit-pipe, r=joshtriplett
[rust.git] / src / test / ui-fulldeps / auxiliary / lint-for-crate-rpass.rs
1 // force-host
2
3 #![feature(plugin_registrar, rustc_private)]
4 #![feature(box_syntax)]
5
6 extern crate rustc_driver;
7 extern crate rustc_hir;
8 extern crate rustc_lint;
9 extern crate rustc_span;
10 #[macro_use]
11 extern crate rustc_session;
12 extern crate rustc_ast;
13
14 use rustc_ast::attr;
15 use rustc_driver::plugin::Registry;
16 use rustc_lint::{LateContext, LateLintPass, LintContext, LintPass};
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, krate: &rustc_hir::Crate) {
31                 let attrs = cx.tcx.hir().attrs(rustc_hir::CRATE_HIR_ID);
32                 $(
33                     if !cx.sess().contains_name(attrs, $attr) {
34                         cx.lint(CRATE_NOT_OKAY, |lint| {
35                              let msg = format!("crate is not marked with #![{}]", $attr);
36                              lint.build(&msg).set_span(krate.item.span).emit()
37                         });
38                     }
39                 )*
40             }
41         }
42
43     }
44 }
45
46 declare_lint!(CRATE_NOT_OKAY, Warn, "crate not marked with #![crate_okay]");
47 declare_lint!(CRATE_NOT_RED, Warn, "crate not marked with #![crate_red]");
48 declare_lint!(CRATE_NOT_BLUE, Warn, "crate not marked with #![crate_blue]");
49 declare_lint!(CRATE_NOT_GREY, Warn, "crate not marked with #![crate_grey]");
50 declare_lint!(CRATE_NOT_GREEN, Warn, "crate not marked with #![crate_green]");
51
52 fake_lint_pass! {
53     PassOkay,
54     Symbol::intern("crate_okay")
55 }
56
57 fake_lint_pass! {
58     PassRedBlue,
59     Symbol::intern("crate_red"), Symbol::intern("crate_blue")
60 }
61
62 fake_lint_pass! {
63     PassGreyGreen,
64     Symbol::intern("crate_grey"), Symbol::intern("crate_green")
65 }
66
67 #[plugin_registrar]
68 pub fn plugin_registrar(reg: &mut Registry) {
69     reg.lint_store.register_lints(&[
70         &CRATE_NOT_OKAY,
71         &CRATE_NOT_RED,
72         &CRATE_NOT_BLUE,
73         &CRATE_NOT_GREY,
74         &CRATE_NOT_GREEN,
75     ]);
76     reg.lint_store.register_late_pass(|| box PassOkay);
77     reg.lint_store.register_late_pass(|| box PassRedBlue);
78     reg.lint_store.register_late_pass(|| box PassGreyGreen);
79 }