]> git.lizzy.rs Git - rust.git/blob - src/librustc_lint/passes.rs
Rollup merge of #72061 - lcnr:const-inference-test, r=eddyb
[rust.git] / src / librustc_lint / passes.rs
1 use crate::context::{EarlyContext, LateContext};
2
3 use rustc_ast::ast;
4 use rustc_data_structures::sync;
5 use rustc_hir as hir;
6 use rustc_session::lint::builtin::HardwiredLints;
7 use rustc_session::lint::LintPass;
8 use rustc_span::symbol::{Ident, Symbol};
9 use rustc_span::Span;
10
11 #[macro_export]
12 macro_rules! late_lint_methods {
13     ($macro:path, $args:tt, [$hir:tt]) => (
14         $macro!($args, [$hir], [
15             fn check_param(a: &$hir hir::Param<$hir>);
16             fn check_body(a: &$hir hir::Body<$hir>);
17             fn check_body_post(a: &$hir hir::Body<$hir>);
18             fn check_name(a: Span, b: Symbol);
19             fn check_crate(a: &$hir hir::Crate<$hir>);
20             fn check_crate_post(a: &$hir hir::Crate<$hir>);
21             fn check_mod(a: &$hir hir::Mod<$hir>, b: Span, c: hir::HirId);
22             fn check_mod_post(a: &$hir hir::Mod<$hir>, b: Span, c: hir::HirId);
23             fn check_foreign_item(a: &$hir hir::ForeignItem<$hir>);
24             fn check_foreign_item_post(a: &$hir hir::ForeignItem<$hir>);
25             fn check_item(a: &$hir hir::Item<$hir>);
26             fn check_item_post(a: &$hir hir::Item<$hir>);
27             fn check_local(a: &$hir hir::Local<$hir>);
28             fn check_block(a: &$hir hir::Block<$hir>);
29             fn check_block_post(a: &$hir hir::Block<$hir>);
30             fn check_stmt(a: &$hir hir::Stmt<$hir>);
31             fn check_arm(a: &$hir hir::Arm<$hir>);
32             fn check_pat(a: &$hir hir::Pat<$hir>);
33             fn check_expr(a: &$hir hir::Expr<$hir>);
34             fn check_expr_post(a: &$hir hir::Expr<$hir>);
35             fn check_ty(a: &$hir hir::Ty<$hir>);
36             fn check_generic_param(a: &$hir hir::GenericParam<$hir>);
37             fn check_generics(a: &$hir hir::Generics<$hir>);
38             fn check_where_predicate(a: &$hir hir::WherePredicate<$hir>);
39             fn check_poly_trait_ref(a: &$hir hir::PolyTraitRef<$hir>, b: hir::TraitBoundModifier);
40             fn check_fn(
41                 a: rustc_hir::intravisit::FnKind<$hir>,
42                 b: &$hir hir::FnDecl<$hir>,
43                 c: &$hir hir::Body<$hir>,
44                 d: Span,
45                 e: hir::HirId);
46             fn check_fn_post(
47                 a: rustc_hir::intravisit::FnKind<$hir>,
48                 b: &$hir hir::FnDecl<$hir>,
49                 c: &$hir hir::Body<$hir>,
50                 d: Span,
51                 e: hir::HirId
52             );
53             fn check_trait_item(a: &$hir hir::TraitItem<$hir>);
54             fn check_trait_item_post(a: &$hir hir::TraitItem<$hir>);
55             fn check_impl_item(a: &$hir hir::ImplItem<$hir>);
56             fn check_impl_item_post(a: &$hir hir::ImplItem<$hir>);
57             fn check_struct_def(a: &$hir hir::VariantData<$hir>);
58             fn check_struct_def_post(a: &$hir hir::VariantData<$hir>);
59             fn check_struct_field(a: &$hir hir::StructField<$hir>);
60             fn check_variant(a: &$hir hir::Variant<$hir>);
61             fn check_variant_post(a: &$hir hir::Variant<$hir>);
62             fn check_lifetime(a: &$hir hir::Lifetime);
63             fn check_path(a: &$hir hir::Path<$hir>, b: hir::HirId);
64             fn check_attribute(a: &$hir ast::Attribute);
65
66             /// Called when entering a syntax node that can have lint attributes such
67             /// as `#[allow(...)]`. Called with *all* the attributes of that node.
68             fn enter_lint_attrs(a: &$hir [ast::Attribute]);
69
70             /// Counterpart to `enter_lint_attrs`.
71             fn exit_lint_attrs(a: &$hir [ast::Attribute]);
72         ]);
73     )
74 }
75
76 /// Trait for types providing lint checks.
77 ///
78 /// Each `check` method checks a single syntax node, and should not
79 /// invoke methods recursively (unlike `Visitor`). By default they
80 /// do nothing.
81 //
82 // FIXME: eliminate the duplication with `Visitor`. But this also
83 // contains a few lint-specific methods with no equivalent in `Visitor`.
84
85 macro_rules! expand_lint_pass_methods {
86     ($context:ty, [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
87         $(#[inline(always)] fn $name(&mut self, _: $context, $(_: $arg),*) {})*
88     )
89 }
90
91 macro_rules! declare_late_lint_pass {
92     ([], [$hir:tt], [$($methods:tt)*]) => (
93         pub trait LateLintPass<'a, $hir>: LintPass {
94             expand_lint_pass_methods!(&LateContext<'a, $hir>, [$($methods)*]);
95         }
96     )
97 }
98
99 late_lint_methods!(declare_late_lint_pass, [], ['tcx]);
100
101 impl LateLintPass<'_, '_> for HardwiredLints {}
102
103 #[macro_export]
104 macro_rules! expand_combined_late_lint_pass_method {
105     ([$($passes:ident),*], $self: ident, $name: ident, $params:tt) => ({
106         $($self.$passes.$name $params;)*
107     })
108 }
109
110 #[macro_export]
111 macro_rules! expand_combined_late_lint_pass_methods {
112     ($passes:tt, [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
113         $(fn $name(&mut self, context: &LateContext<'a, 'tcx>, $($param: $arg),*) {
114             expand_combined_late_lint_pass_method!($passes, self, $name, (context, $($param),*));
115         })*
116     )
117 }
118
119 #[macro_export]
120 macro_rules! declare_combined_late_lint_pass {
121     ([$v:vis $name:ident, [$($passes:ident: $constructor:expr,)*]], [$hir:tt], $methods:tt) => (
122         #[allow(non_snake_case)]
123         $v struct $name {
124             $($passes: $passes,)*
125         }
126
127         impl $name {
128             $v fn new() -> Self {
129                 Self {
130                     $($passes: $constructor,)*
131                 }
132             }
133
134             $v fn get_lints() -> LintArray {
135                 let mut lints = Vec::new();
136                 $(lints.extend_from_slice(&$passes::get_lints());)*
137                 lints
138             }
139         }
140
141         impl<'a, 'tcx> LateLintPass<'a, 'tcx> for $name {
142             expand_combined_late_lint_pass_methods!([$($passes),*], $methods);
143         }
144
145         #[allow(rustc::lint_pass_impl_without_macro)]
146         impl LintPass for $name {
147             fn name(&self) -> &'static str {
148                 panic!()
149             }
150         }
151     )
152 }
153
154 #[macro_export]
155 macro_rules! early_lint_methods {
156     ($macro:path, $args:tt) => (
157         $macro!($args, [
158             fn check_param(a: &ast::Param);
159             fn check_ident(a: Ident);
160             fn check_crate(a: &ast::Crate);
161             fn check_crate_post(a: &ast::Crate);
162             fn check_mod(a: &ast::Mod, b: Span, c: ast::NodeId);
163             fn check_mod_post(a: &ast::Mod, b: Span, c: ast::NodeId);
164             fn check_foreign_item(a: &ast::ForeignItem);
165             fn check_foreign_item_post(a: &ast::ForeignItem);
166             fn check_item(a: &ast::Item);
167             fn check_item_post(a: &ast::Item);
168             fn check_local(a: &ast::Local);
169             fn check_block(a: &ast::Block);
170             fn check_block_post(a: &ast::Block);
171             fn check_stmt(a: &ast::Stmt);
172             fn check_arm(a: &ast::Arm);
173             fn check_pat(a: &ast::Pat);
174             fn check_anon_const(a: &ast::AnonConst);
175             fn check_pat_post(a: &ast::Pat);
176             fn check_expr(a: &ast::Expr);
177             fn check_expr_post(a: &ast::Expr);
178             fn check_ty(a: &ast::Ty);
179             fn check_generic_param(a: &ast::GenericParam);
180             fn check_generics(a: &ast::Generics);
181             fn check_where_predicate(a: &ast::WherePredicate);
182             fn check_poly_trait_ref(a: &ast::PolyTraitRef,
183                                     b: &ast::TraitBoundModifier);
184             fn check_fn(a: rustc_ast::visit::FnKind<'_>, c: Span, d_: ast::NodeId);
185             fn check_fn_post(
186                 a: rustc_ast::visit::FnKind<'_>,
187                 c: Span,
188                 d: ast::NodeId
189             );
190             fn check_trait_item(a: &ast::AssocItem);
191             fn check_trait_item_post(a: &ast::AssocItem);
192             fn check_impl_item(a: &ast::AssocItem);
193             fn check_impl_item_post(a: &ast::AssocItem);
194             fn check_struct_def(a: &ast::VariantData);
195             fn check_struct_def_post(a: &ast::VariantData);
196             fn check_struct_field(a: &ast::StructField);
197             fn check_variant(a: &ast::Variant);
198             fn check_variant_post(a: &ast::Variant);
199             fn check_lifetime(a: &ast::Lifetime);
200             fn check_path(a: &ast::Path, b: ast::NodeId);
201             fn check_attribute(a: &ast::Attribute);
202             fn check_mac_def(a: &ast::MacroDef, b: ast::NodeId);
203             fn check_mac(a: &ast::MacCall);
204
205             /// Called when entering a syntax node that can have lint attributes such
206             /// as `#[allow(...)]`. Called with *all* the attributes of that node.
207             fn enter_lint_attrs(a: &[ast::Attribute]);
208
209             /// Counterpart to `enter_lint_attrs`.
210             fn exit_lint_attrs(a: &[ast::Attribute]);
211         ]);
212     )
213 }
214
215 macro_rules! expand_early_lint_pass_methods {
216     ($context:ty, [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
217         $(#[inline(always)] fn $name(&mut self, _: $context, $(_: $arg),*) {})*
218     )
219 }
220
221 macro_rules! declare_early_lint_pass {
222     ([], [$($methods:tt)*]) => (
223         pub trait EarlyLintPass: LintPass {
224             expand_early_lint_pass_methods!(&EarlyContext<'_>, [$($methods)*]);
225         }
226     )
227 }
228
229 early_lint_methods!(declare_early_lint_pass, []);
230
231 #[macro_export]
232 macro_rules! expand_combined_early_lint_pass_method {
233     ([$($passes:ident),*], $self: ident, $name: ident, $params:tt) => ({
234         $($self.$passes.$name $params;)*
235     })
236 }
237
238 #[macro_export]
239 macro_rules! expand_combined_early_lint_pass_methods {
240     ($passes:tt, [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
241         $(fn $name(&mut self, context: &EarlyContext<'_>, $($param: $arg),*) {
242             expand_combined_early_lint_pass_method!($passes, self, $name, (context, $($param),*));
243         })*
244     )
245 }
246
247 #[macro_export]
248 macro_rules! declare_combined_early_lint_pass {
249     ([$v:vis $name:ident, [$($passes:ident: $constructor:expr,)*]], $methods:tt) => (
250         #[allow(non_snake_case)]
251         $v struct $name {
252             $($passes: $passes,)*
253         }
254
255         impl $name {
256             $v fn new() -> Self {
257                 Self {
258                     $($passes: $constructor,)*
259                 }
260             }
261
262             $v fn get_lints() -> LintArray {
263                 let mut lints = Vec::new();
264                 $(lints.extend_from_slice(&$passes::get_lints());)*
265                 lints
266             }
267         }
268
269         impl EarlyLintPass for $name {
270             expand_combined_early_lint_pass_methods!([$($passes),*], $methods);
271         }
272
273         #[allow(rustc::lint_pass_impl_without_macro)]
274         impl LintPass for $name {
275             fn name(&self) -> &'static str {
276                 panic!()
277             }
278         }
279     )
280 }
281
282 /// A lint pass boxed up as a trait object.
283 pub type EarlyLintPassObject = Box<dyn EarlyLintPass + sync::Send + sync::Sync + 'static>;
284 pub type LateLintPassObject =
285     Box<dyn for<'a, 'tcx> LateLintPass<'a, 'tcx> + sync::Send + sync::Sync + 'static>;