1#[macro_export]
2macro_rules! link {
3 ($url:expr) => {
4 concat!("\x1b]8;;", $url, "\x1b\\", $url, "\x1b]8;;\x1b\\")
5 };
6}
7
8#[macro_export]
23macro_rules! declare_forge_lint {
24 ($id:ident, $severity:expr, $str_id:expr, $desc:expr) => {
25 pub static $id: SolLint = SolLint {
27 id: $str_id,
28 severity: $severity,
29 description: $desc,
30 help: link!(concat!("https://book.getfoundry.sh/reference/forge/forge-lint#", $str_id)),
31 };
32 };
33
34 ($id:ident, $severity:expr, $str_id:expr, $desc:expr) => {
35 $crate::declare_forge_lint!($id, $severity, $str_id, $desc, "");
36 };
37}
38
39#[macro_export]
54macro_rules! register_lints {
55 ( @declare_structs $( ($pass_id:ident, $pass_type:ident, ($($lint:expr),* $(,)?)) ),* $(,)? ) => {
57 $(
58 #[derive(Debug, Default, Clone, Copy, Eq, PartialEq)]
59 pub struct $pass_id;
60
61 impl $pass_id {
62 const LINTS: &'static [SolLint] = &[$($lint),*];
64
65 register_lints!(@early_impl $pass_id, $pass_type);
66 register_lints!(@late_impl $pass_id, $pass_type);
67 }
68 )*
69 };
70
71 ( @declare_consts $( ($pass_id:ident, $pass_type:ident, ($($lint:expr),* $(,)?)) ),* $(,)? ) => {
73 pub const REGISTERED_LINTS: &[SolLint] = &[
74 $(
75 $($lint,)*
76 )*
77 ];
78 };
79
80 ( @declare_funcs $( ($pass_id:ident, $pass_type:ident, $lints:tt) ),* $(,)? ) => {
82 pub fn create_early_lint_passes<'ast>() -> Vec<(Box<dyn EarlyLintPass<'ast>>, &'static [SolLint])> {
83 [
84 $(
85 register_lints!(@early_create $pass_id, $pass_type),
86 )*
87 ]
88 .into_iter()
89 .flatten()
90 .collect()
91 }
92
93 pub fn create_late_lint_passes<'hir>() -> Vec<(Box<dyn LateLintPass<'hir>>, &'static [SolLint])> {
94 [
95 $(
96 register_lints!(@late_create $pass_id, $pass_type),
97 )*
98 ]
99 .into_iter()
100 .flatten()
101 .collect()
102 }
103 };
104
105 (@early_impl $_pass_id:ident, late) => {};
107 (@early_impl $pass_id:ident, $other:ident) => {
108 pub fn as_early_lint_pass<'a>() -> Box<dyn EarlyLintPass<'a>> {
109 Box::new(Self::default())
110 }
111 };
112
113 (@late_impl $_pass_id:ident, early) => {};
114 (@late_impl $pass_id:ident, $other:ident) => {
115 pub fn as_late_lint_pass<'hir>() -> Box<dyn LateLintPass<'hir>> {
116 Box::new(Self::default())
117 }
118 };
119
120 (@early_create $_pass_id:ident, late) => { None };
121 (@early_create $pass_id:ident, $_other:ident) => {
122 Some(($pass_id::as_early_lint_pass(), $pass_id::LINTS))
123 };
124
125 (@late_create $_pass_id:ident, early) => { None };
126 (@late_create $pass_id:ident, $_other:ident) => {
127 Some(($pass_id::as_late_lint_pass(), $pass_id::LINTS))
128 };
129
130 ( $($tokens:tt)* ) => {
132 register_lints! { @declare_structs $($tokens)* }
133 register_lints! { @declare_consts $($tokens)* }
134 register_lints! { @declare_funcs $($tokens)* }
135 };
136}