Skip to main content

forge_lint/sol/
macros.rs

1#[macro_export]
2macro_rules! link {
3    ($url:expr) => {
4        concat!("\x1b]8;;", $url, "\x1b\\", $url, "\x1b]8;;\x1b\\")
5    };
6}
7
8/// Macro for defining lints and relevant metadata for the Solidity linter.
9///
10/// # Parameters
11///
12/// Each lint requires the following input fields:
13/// - `$id`: Identifier of the generated `SolLint` constant.
14/// - `$severity`: The `Severity` of the lint (e.g. `High`, `Med`, `Low`, `Info`, `Gas`).
15/// - `$str_id`: A unique identifier used to reference a specific lint during configuration.
16/// - `$desc`: A short description of the lint.
17///
18/// # Note
19/// Each lint must have a `help` section in the foundry book. This help field is auto-generated by
20/// the macro. Because of that, to ensure that new lint rules have their corresponding docs in the
21/// book, the existence of the lint rule's help section is validated with a unit test.
22#[macro_export]
23macro_rules! declare_forge_lint {
24    ($id:ident, $severity:expr, $str_id:expr, $desc:expr) => {
25        // Declare the static `Lint` metadata
26        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/// Registers Solidity linter passes that can have both early and late variants.
40///
41/// # Parameters
42///
43/// Each pass is declared with:
44/// - `$pass_id`: Identifier of the generated struct that will implement the pass trait(s).
45/// - `$pass_type`: Either `early`, `late`, or `both` to indicate which traits to implement.
46/// - `$lints`: A parenthesized, comma-separated list of `SolLint` constants.
47///
48/// # Outputs
49///
50/// - Structs for each linting pass
51/// - Helper methods to create early and late passes with required lifetimes
52/// - `const REGISTERED_LINTS` containing all registered lint objects
53#[macro_export]
54macro_rules! register_lints {
55    // 1. Internal rule for declaring structs and their associated lints.
56    ( @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                /// Static slice of lints associated with this pass.
63                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    // 2. Internal rule for declaring the const array of ALL lints.
72    ( @declare_consts $( ($pass_id:ident, $pass_type:ident, ($($lint:expr),* $(,)?)) ),* $(,)? ) => {
73        pub const REGISTERED_LINTS: &[SolLint] = &[
74            $(
75                $($lint,)*
76            )*
77        ];
78    };
79
80    // 3. Internal rule for declaring the helper functions.
81    ( @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    // --- HELPERS ------------------------------------------------------------
106    (@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    // --- ENTRY POINT ---------------------------------------------------------
131    ( $($tokens:tt)* ) => {
132        register_lints! { @declare_structs $($tokens)* }
133        register_lints! { @declare_consts  $($tokens)* }
134        register_lints! { @declare_funcs   $($tokens)* }
135    };
136}