rustc_lint/
early.rs

1//! Implementation of the early lint pass.
2//!
3//! The early lint pass works on AST nodes after macro expansion and name
4//! resolution, just before AST lowering. These lints are for purely
5//! syntactical lints.
6
7use rustc_ast::visit::{self as ast_visit, Visitor, walk_list};
8use rustc_ast::{self as ast, AttrVec, HasAttrs};
9use rustc_data_structures::stack::ensure_sufficient_stack;
10use rustc_errors::{BufferedEarlyLint, DecorateDiagCompat, LintBuffer};
11use rustc_feature::Features;
12use rustc_middle::ty::{RegisteredTools, TyCtxt};
13use rustc_session::Session;
14use rustc_session::lint::LintPass;
15use rustc_span::{Ident, Span};
16use tracing::debug;
17
18use crate::context::{EarlyContext, LintContext, LintStore};
19use crate::passes::{EarlyLintPass, EarlyLintPassObject};
20
21pub(super) mod diagnostics;
22
23macro_rules! lint_callback { ($cx:expr, $f:ident, $($args:expr),*) => ({
24    $cx.pass.$f(&$cx.context, $($args),*);
25}) }
26
27/// Implements the AST traversal for early lint passes. `T` provides the
28/// `check_*` methods.
29pub struct EarlyContextAndPass<'ecx, 'tcx, T: EarlyLintPass> {
30    context: EarlyContext<'ecx>,
31    tcx: Option<TyCtxt<'tcx>>,
32    pass: T,
33}
34
35impl<'ecx, 'tcx, T: EarlyLintPass> EarlyContextAndPass<'ecx, 'tcx, T> {
36    fn check_id(&mut self, id: ast::NodeId) {
37        for early_lint in self.context.buffered.take(id) {
38            let BufferedEarlyLint { span, node_id: _, lint_id, diagnostic } = early_lint;
39            self.context.opt_span_lint(lint_id.lint, span, |diag| match diagnostic {
40                DecorateDiagCompat::Builtin(b) => {
41                    diagnostics::decorate_builtin_lint(self.context.sess(), self.tcx, b, diag);
42                }
43                DecorateDiagCompat::Dynamic(d) => d.decorate_lint_box(diag),
44            });
45        }
46    }
47
48    /// Merge the lints specified by any lint attributes into the
49    /// current lint context, call the provided function, then reset the
50    /// lints in effect to their previous state.
51    fn with_lint_attrs<F>(&mut self, id: ast::NodeId, attrs: &'_ [ast::Attribute], f: F)
52    where
53        F: FnOnce(&mut Self),
54    {
55        let is_crate_node = id == ast::CRATE_NODE_ID;
56        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_lint/src/early.rs:56",
                        "rustc_lint::early", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_lint/src/early.rs"),
                        ::tracing_core::__macro_support::Option::Some(56u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_lint::early"),
                        ::tracing_core::field::FieldSet::new(&["id"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&id) as
                                            &dyn Value))])
            });
    } else { ; }
};debug!(?id);
57        let push = self.context.builder.push(attrs, is_crate_node, None);
58
59        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_lint/src/early.rs:59",
                        "rustc_lint::early", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_lint/src/early.rs"),
                        ::tracing_core::__macro_support::Option::Some(59u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_lint::early"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("early context: enter_attrs({0:?})",
                                                    attrs) as &dyn Value))])
            });
    } else { ; }
};debug!("early context: enter_attrs({:?})", attrs);
60        { self.pass.check_attributes(&self.context, attrs); };lint_callback!(self, check_attributes, attrs);
61        ensure_sufficient_stack(|| f(self));
62        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_lint/src/early.rs:62",
                        "rustc_lint::early", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_lint/src/early.rs"),
                        ::tracing_core::__macro_support::Option::Some(62u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_lint::early"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("early context: exit_attrs({0:?})",
                                                    attrs) as &dyn Value))])
            });
    } else { ; }
};debug!("early context: exit_attrs({:?})", attrs);
63        { self.pass.check_attributes_post(&self.context, attrs); };lint_callback!(self, check_attributes_post, attrs);
64        self.context.builder.pop(push);
65    }
66}
67
68impl<'ast, 'ecx, 'tcx, T: EarlyLintPass> ast_visit::Visitor<'ast>
69    for EarlyContextAndPass<'ecx, 'tcx, T>
70{
71    fn visit_id(&mut self, id: rustc_ast::NodeId) {
72        self.check_id(id);
73    }
74
75    fn visit_param(&mut self, param: &'ast ast::Param) {
76        self.with_lint_attrs(param.id, &param.attrs, |cx| {
77            { cx.pass.check_param(&cx.context, param); };lint_callback!(cx, check_param, param);
78            ast_visit::walk_param(cx, param);
79        });
80    }
81
82    fn visit_item(&mut self, it: &'ast ast::Item) {
83        self.with_lint_attrs(it.id, &it.attrs, |cx| {
84            { cx.pass.check_item(&cx.context, it); };lint_callback!(cx, check_item, it);
85            ast_visit::walk_item(cx, it);
86            { cx.pass.check_item_post(&cx.context, it); };lint_callback!(cx, check_item_post, it);
87        })
88    }
89
90    fn visit_foreign_item(&mut self, it: &'ast ast::ForeignItem) {
91        self.with_lint_attrs(it.id, &it.attrs, |cx| {
92            ast_visit::walk_item(cx, it);
93        })
94    }
95
96    fn visit_pat(&mut self, p: &'ast ast::Pat) {
97        { self.pass.check_pat(&self.context, p); };lint_callback!(self, check_pat, p);
98        ast_visit::walk_pat(self, p);
99        { self.pass.check_pat_post(&self.context, p); };lint_callback!(self, check_pat_post, p);
100    }
101
102    fn visit_pat_field(&mut self, field: &'ast ast::PatField) {
103        self.with_lint_attrs(field.id, &field.attrs, |cx| {
104            ast_visit::walk_pat_field(cx, field);
105        });
106    }
107
108    fn visit_expr(&mut self, e: &'ast ast::Expr) {
109        self.with_lint_attrs(e.id, &e.attrs, |cx| {
110            { cx.pass.check_expr(&cx.context, e); };lint_callback!(cx, check_expr, e);
111            ast_visit::walk_expr(cx, e);
112            { cx.pass.check_expr_post(&cx.context, e); };lint_callback!(cx, check_expr_post, e);
113        })
114    }
115
116    fn visit_expr_field(&mut self, f: &'ast ast::ExprField) {
117        self.with_lint_attrs(f.id, &f.attrs, |cx| {
118            ast_visit::walk_expr_field(cx, f);
119        })
120    }
121
122    fn visit_stmt(&mut self, s: &'ast ast::Stmt) {
123        // Add the statement's lint attributes to our
124        // current state when checking the statement itself.
125        // This allows us to handle attributes like
126        // `#[allow(unused_doc_comments)]`, which apply to
127        // sibling attributes on the same target
128        //
129        // Note that statements get their attributes from
130        // the AST struct that they wrap (e.g. an item)
131        self.with_lint_attrs(s.id, s.attrs(), |cx| {
132            { cx.pass.check_stmt(&cx.context, s); };lint_callback!(cx, check_stmt, s);
133            ast_visit::walk_stmt(cx, s);
134        });
135    }
136
137    fn visit_fn(&mut self, fk: ast_visit::FnKind<'ast>, _: &AttrVec, span: Span, id: ast::NodeId) {
138        { self.pass.check_fn(&self.context, fk, span, id); };lint_callback!(self, check_fn, fk, span, id);
139        ast_visit::walk_fn(self, fk);
140    }
141
142    fn visit_field_def(&mut self, s: &'ast ast::FieldDef) {
143        self.with_lint_attrs(s.id, &s.attrs, |cx| {
144            ast_visit::walk_field_def(cx, s);
145        })
146    }
147
148    fn visit_variant(&mut self, v: &'ast ast::Variant) {
149        self.with_lint_attrs(v.id, &v.attrs, |cx| {
150            { cx.pass.check_variant(&cx.context, v); };lint_callback!(cx, check_variant, v);
151            ast_visit::walk_variant(cx, v);
152        })
153    }
154
155    fn visit_ty(&mut self, t: &'ast ast::Ty) {
156        { self.pass.check_ty(&self.context, t); };lint_callback!(self, check_ty, t);
157        ast_visit::walk_ty(self, t);
158    }
159
160    fn visit_ident(&mut self, ident: &Ident) {
161        { self.pass.check_ident(&self.context, ident); };lint_callback!(self, check_ident, ident);
162    }
163
164    fn visit_local(&mut self, l: &'ast ast::Local) {
165        self.with_lint_attrs(l.id, &l.attrs, |cx| {
166            { cx.pass.check_local(&cx.context, l); };lint_callback!(cx, check_local, l);
167            ast_visit::walk_local(cx, l);
168        })
169    }
170
171    fn visit_block(&mut self, b: &'ast ast::Block) {
172        { self.pass.check_block(&self.context, b); };lint_callback!(self, check_block, b);
173        ast_visit::walk_block(self, b);
174    }
175
176    fn visit_arm(&mut self, a: &'ast ast::Arm) {
177        self.with_lint_attrs(a.id, &a.attrs, |cx| {
178            { cx.pass.check_arm(&cx.context, a); };lint_callback!(cx, check_arm, a);
179            ast_visit::walk_arm(cx, a);
180        })
181    }
182
183    fn visit_generic_arg(&mut self, arg: &'ast ast::GenericArg) {
184        { self.pass.check_generic_arg(&self.context, arg); };lint_callback!(self, check_generic_arg, arg);
185        ast_visit::walk_generic_arg(self, arg);
186    }
187
188    fn visit_generic_param(&mut self, param: &'ast ast::GenericParam) {
189        self.with_lint_attrs(param.id, &param.attrs, |cx| {
190            { cx.pass.check_generic_param(&cx.context, param); };lint_callback!(cx, check_generic_param, param);
191            ast_visit::walk_generic_param(cx, param);
192        });
193    }
194
195    fn visit_generics(&mut self, g: &'ast ast::Generics) {
196        { self.pass.check_generics(&self.context, g); };lint_callback!(self, check_generics, g);
197        ast_visit::walk_generics(self, g);
198    }
199
200    fn visit_where_predicate(&mut self, p: &'ast ast::WherePredicate) {
201        { self.pass.enter_where_predicate(&self.context, p); };lint_callback!(self, enter_where_predicate, p);
202        ast_visit::walk_where_predicate(self, p);
203        { self.pass.exit_where_predicate(&self.context, p); };lint_callback!(self, exit_where_predicate, p);
204    }
205
206    fn visit_poly_trait_ref(&mut self, t: &'ast ast::PolyTraitRef) {
207        { self.pass.check_poly_trait_ref(&self.context, t); };lint_callback!(self, check_poly_trait_ref, t);
208        ast_visit::walk_poly_trait_ref(self, t);
209    }
210
211    fn visit_assoc_item(&mut self, item: &'ast ast::AssocItem, ctxt: ast_visit::AssocCtxt) {
212        self.with_lint_attrs(item.id, &item.attrs, |cx| {
213            match ctxt {
214                ast_visit::AssocCtxt::Trait => {
215                    { cx.pass.check_trait_item(&cx.context, item); };lint_callback!(cx, check_trait_item, item);
216                }
217                ast_visit::AssocCtxt::Impl { .. } => {
218                    { cx.pass.check_impl_item(&cx.context, item); };lint_callback!(cx, check_impl_item, item);
219                }
220            }
221            ast_visit::walk_assoc_item(cx, item, ctxt);
222            match ctxt {
223                ast_visit::AssocCtxt::Trait => {
224                    { cx.pass.check_trait_item_post(&cx.context, item); };lint_callback!(cx, check_trait_item_post, item);
225                }
226                ast_visit::AssocCtxt::Impl { .. } => {
227                    { cx.pass.check_impl_item_post(&cx.context, item); };lint_callback!(cx, check_impl_item_post, item);
228                }
229            }
230        });
231    }
232
233    fn visit_attribute(&mut self, attr: &'ast ast::Attribute) {
234        { self.pass.check_attribute(&self.context, attr); };lint_callback!(self, check_attribute, attr);
235        ast_visit::walk_attribute(self, attr);
236    }
237
238    fn visit_macro_def(&mut self, mac: &'ast ast::MacroDef) {
239        { self.pass.check_mac_def(&self.context, mac); };lint_callback!(self, check_mac_def, mac);
240    }
241
242    fn visit_mac_call(&mut self, mac: &'ast ast::MacCall) {
243        { self.pass.check_mac(&self.context, mac); };lint_callback!(self, check_mac, mac);
244        ast_visit::walk_mac(self, mac);
245    }
246}
247
248// Combines multiple lint passes into a single pass, at runtime. Each
249// `check_foo` method in `$methods` within this pass simply calls `check_foo`
250// once per `$pass`. Compare with `declare_combined_early_lint_pass`, which is
251// similar, but combines lint passes at compile time.
252struct RuntimeCombinedEarlyLintPass<'a> {
253    passes: &'a mut [EarlyLintPassObject],
254}
255
256#[allow(rustc::lint_pass_impl_without_macro)]
257impl LintPass for RuntimeCombinedEarlyLintPass<'_> {
258    fn name(&self) -> &'static str {
259        ::core::panicking::panic("explicit panic")panic!()
260    }
261    fn get_lints(&self) -> crate::LintVec {
262        ::core::panicking::panic("explicit panic")panic!()
263    }
264}
265
266macro_rules! impl_early_lint_pass {
267    ([], [$($(#[$attr:meta])* fn $f:ident($($param:ident: $arg:ty),*);)*]) => (
268        impl EarlyLintPass for RuntimeCombinedEarlyLintPass<'_> {
269            $(fn $f(&mut self, context: &EarlyContext<'_>, $($param: $arg),*) {
270                for pass in self.passes.iter_mut() {
271                    pass.$f(context, $($param),*);
272                }
273            })*
274        }
275    )
276}
277
278impl EarlyLintPass for RuntimeCombinedEarlyLintPass<'_> {
    fn check_param(&mut self, context: &EarlyContext<'_>,
        a: &rustc_ast::Param) {
        for pass in self.passes.iter_mut() { pass.check_param(context, a); }
    }
    fn check_ident(&mut self, context: &EarlyContext<'_>,
        a: &rustc_span::Ident) {
        for pass in self.passes.iter_mut() { pass.check_ident(context, a); }
    }
    fn check_crate(&mut self, context: &EarlyContext<'_>,
        a: &rustc_ast::Crate) {
        for pass in self.passes.iter_mut() { pass.check_crate(context, a); }
    }
    fn check_crate_post(&mut self, context: &EarlyContext<'_>,
        a: &rustc_ast::Crate) {
        for pass in self.passes.iter_mut() {
            pass.check_crate_post(context, a);
        }
    }
    fn check_item(&mut self, context: &EarlyContext<'_>,
        a: &rustc_ast::Item) {
        for pass in self.passes.iter_mut() { pass.check_item(context, a); }
    }
    fn check_item_post(&mut self, context: &EarlyContext<'_>,
        a: &rustc_ast::Item) {
        for pass in self.passes.iter_mut() {
            pass.check_item_post(context, a);
        }
    }
    fn check_local(&mut self, context: &EarlyContext<'_>,
        a: &rustc_ast::Local) {
        for pass in self.passes.iter_mut() { pass.check_local(context, a); }
    }
    fn check_block(&mut self, context: &EarlyContext<'_>,
        a: &rustc_ast::Block) {
        for pass in self.passes.iter_mut() { pass.check_block(context, a); }
    }
    fn check_stmt(&mut self, context: &EarlyContext<'_>,
        a: &rustc_ast::Stmt) {
        for pass in self.passes.iter_mut() { pass.check_stmt(context, a); }
    }
    fn check_arm(&mut self, context: &EarlyContext<'_>, a: &rustc_ast::Arm) {
        for pass in self.passes.iter_mut() { pass.check_arm(context, a); }
    }
    fn check_pat(&mut self, context: &EarlyContext<'_>, a: &rustc_ast::Pat) {
        for pass in self.passes.iter_mut() { pass.check_pat(context, a); }
    }
    fn check_pat_post(&mut self, context: &EarlyContext<'_>,
        a: &rustc_ast::Pat) {
        for pass in self.passes.iter_mut() {
            pass.check_pat_post(context, a);
        }
    }
    fn check_expr(&mut self, context: &EarlyContext<'_>,
        a: &rustc_ast::Expr) {
        for pass in self.passes.iter_mut() { pass.check_expr(context, a); }
    }
    fn check_expr_post(&mut self, context: &EarlyContext<'_>,
        a: &rustc_ast::Expr) {
        for pass in self.passes.iter_mut() {
            pass.check_expr_post(context, a);
        }
    }
    fn check_ty(&mut self, context: &EarlyContext<'_>, a: &rustc_ast::Ty) {
        for pass in self.passes.iter_mut() { pass.check_ty(context, a); }
    }
    fn check_generic_arg(&mut self, context: &EarlyContext<'_>,
        a: &rustc_ast::GenericArg) {
        for pass in self.passes.iter_mut() {
            pass.check_generic_arg(context, a);
        }
    }
    fn check_generic_param(&mut self, context: &EarlyContext<'_>,
        a: &rustc_ast::GenericParam) {
        for pass in self.passes.iter_mut() {
            pass.check_generic_param(context, a);
        }
    }
    fn check_generics(&mut self, context: &EarlyContext<'_>,
        a: &rustc_ast::Generics) {
        for pass in self.passes.iter_mut() {
            pass.check_generics(context, a);
        }
    }
    fn check_poly_trait_ref(&mut self, context: &EarlyContext<'_>,
        a: &rustc_ast::PolyTraitRef) {
        for pass in self.passes.iter_mut() {
            pass.check_poly_trait_ref(context, a);
        }
    }
    fn check_fn(&mut self, context: &EarlyContext<'_>,
        a: rustc_ast::visit::FnKind<'_>, c: rustc_span::Span,
        d_: rustc_ast::NodeId) {
        for pass in self.passes.iter_mut() {
            pass.check_fn(context, a, c, d_);
        }
    }
    fn check_trait_item(&mut self, context: &EarlyContext<'_>,
        a: &rustc_ast::AssocItem) {
        for pass in self.passes.iter_mut() {
            pass.check_trait_item(context, a);
        }
    }
    fn check_trait_item_post(&mut self, context: &EarlyContext<'_>,
        a: &rustc_ast::AssocItem) {
        for pass in self.passes.iter_mut() {
            pass.check_trait_item_post(context, a);
        }
    }
    fn check_impl_item(&mut self, context: &EarlyContext<'_>,
        a: &rustc_ast::AssocItem) {
        for pass in self.passes.iter_mut() {
            pass.check_impl_item(context, a);
        }
    }
    fn check_impl_item_post(&mut self, context: &EarlyContext<'_>,
        a: &rustc_ast::AssocItem) {
        for pass in self.passes.iter_mut() {
            pass.check_impl_item_post(context, a);
        }
    }
    fn check_variant(&mut self, context: &EarlyContext<'_>,
        a: &rustc_ast::Variant) {
        for pass in self.passes.iter_mut() { pass.check_variant(context, a); }
    }
    fn check_attribute(&mut self, context: &EarlyContext<'_>,
        a: &rustc_ast::Attribute) {
        for pass in self.passes.iter_mut() {
            pass.check_attribute(context, a);
        }
    }
    fn check_attributes(&mut self, context: &EarlyContext<'_>,
        a: &[rustc_ast::Attribute]) {
        for pass in self.passes.iter_mut() {
            pass.check_attributes(context, a);
        }
    }
    fn check_attributes_post(&mut self, context: &EarlyContext<'_>,
        a: &[rustc_ast::Attribute]) {
        for pass in self.passes.iter_mut() {
            pass.check_attributes_post(context, a);
        }
    }
    fn check_mac_def(&mut self, context: &EarlyContext<'_>,
        a: &rustc_ast::MacroDef) {
        for pass in self.passes.iter_mut() { pass.check_mac_def(context, a); }
    }
    fn check_mac(&mut self, context: &EarlyContext<'_>,
        a: &rustc_ast::MacCall) {
        for pass in self.passes.iter_mut() { pass.check_mac(context, a); }
    }
    fn enter_where_predicate(&mut self, context: &EarlyContext<'_>,
        a: &rustc_ast::WherePredicate) {
        for pass in self.passes.iter_mut() {
            pass.enter_where_predicate(context, a);
        }
    }
    fn exit_where_predicate(&mut self, context: &EarlyContext<'_>,
        a: &rustc_ast::WherePredicate) {
        for pass in self.passes.iter_mut() {
            pass.exit_where_predicate(context, a);
        }
    }
}crate::early_lint_methods!(impl_early_lint_pass, []);
279
280/// Early lints work on different nodes - either on the crate root, or on freshly loaded modules.
281/// This trait generalizes over those nodes.
282pub trait EarlyCheckNode<'a>: Copy {
283    fn id(self) -> ast::NodeId;
284    fn attrs(self) -> &'a [ast::Attribute];
285    fn check<'ecx, 'tcx, T: EarlyLintPass>(self, cx: &mut EarlyContextAndPass<'ecx, 'tcx, T>);
286}
287
288impl<'a> EarlyCheckNode<'a> for (&'a ast::Crate, &'a [ast::Attribute]) {
289    fn id(self) -> ast::NodeId {
290        ast::CRATE_NODE_ID
291    }
292    fn attrs(self) -> &'a [ast::Attribute] {
293        self.1
294    }
295    fn check<'ecx, 'tcx, T: EarlyLintPass>(self, cx: &mut EarlyContextAndPass<'ecx, 'tcx, T>) {
296        { cx.pass.check_crate(&cx.context, self.0); };lint_callback!(cx, check_crate, self.0);
297        ast_visit::walk_crate(cx, self.0);
298        { cx.pass.check_crate_post(&cx.context, self.0); };lint_callback!(cx, check_crate_post, self.0);
299    }
300}
301
302impl<'a> EarlyCheckNode<'a> for (ast::NodeId, &'a [ast::Attribute], &'a [Box<ast::Item>]) {
303    fn id(self) -> ast::NodeId {
304        self.0
305    }
306    fn attrs(self) -> &'a [ast::Attribute] {
307        self.1
308    }
309    fn check<'ecx, 'tcx, T: EarlyLintPass>(self, cx: &mut EarlyContextAndPass<'ecx, 'tcx, T>) {
310        for elem in self.1 {
    match ::rustc_ast_ir::visit::VisitorResult::branch(cx.visit_attribute(elem))
        {
        core::ops::ControlFlow::Continue(()) =>
            (),
            #[allow(unreachable_code)]
            core::ops::ControlFlow::Break(r) => {
            return ::rustc_ast_ir::visit::VisitorResult::from_residual(r);
        }
    };
};walk_list!(cx, visit_attribute, self.1);
311        for elem in self.2 {
    match ::rustc_ast_ir::visit::VisitorResult::branch(cx.visit_item(elem)) {
        core::ops::ControlFlow::Continue(()) =>
            (),
            #[allow(unreachable_code)]
            core::ops::ControlFlow::Break(r) => {
            return ::rustc_ast_ir::visit::VisitorResult::from_residual(r);
        }
    };
};walk_list!(cx, visit_item, self.2);
312    }
313}
314
315pub fn check_ast_node<'a>(
316    sess: &Session,
317    tcx: Option<TyCtxt<'_>>,
318    features: &Features,
319    pre_expansion: bool,
320    lint_store: &LintStore,
321    registered_tools: &RegisteredTools,
322    lint_buffer: Option<LintBuffer>,
323    builtin_lints: impl EarlyLintPass + 'static,
324    check_node: impl EarlyCheckNode<'a>,
325) {
326    let context = EarlyContext::new(
327        sess,
328        features,
329        !pre_expansion,
330        lint_store,
331        registered_tools,
332        lint_buffer.unwrap_or_default(),
333    );
334
335    // Note: `passes` is often empty. In that case, it's faster to run
336    // `builtin_lints` directly rather than bundling it up into the
337    // `RuntimeCombinedEarlyLintPass`.
338    let passes =
339        if pre_expansion { &lint_store.pre_expansion_passes } else { &lint_store.early_passes };
340    if passes.is_empty() {
341        check_ast_node_inner(sess, tcx, check_node, context, builtin_lints);
342    } else {
343        let mut passes: Vec<_> = passes.iter().map(|mk_pass| (mk_pass)()).collect();
344        passes.push(Box::new(builtin_lints));
345        let pass = RuntimeCombinedEarlyLintPass { passes: &mut passes[..] };
346        check_ast_node_inner(sess, tcx, check_node, context, pass);
347    }
348}
349
350fn check_ast_node_inner<'a, T: EarlyLintPass>(
351    sess: &Session,
352    tcx: Option<TyCtxt<'_>>,
353    check_node: impl EarlyCheckNode<'a>,
354    context: EarlyContext<'_>,
355    pass: T,
356) {
357    let mut cx = EarlyContextAndPass { context, tcx, pass };
358
359    cx.with_lint_attrs(check_node.id(), check_node.attrs(), |cx| check_node.check(cx));
360
361    // All of the buffered lints should have been emitted at this point.
362    // If not, that means that we somehow buffered a lint for a node id
363    // that was not lint-checked (perhaps it doesn't exist?). This is a bug.
364    for (id, lints) in cx.context.buffered.map {
365        if !lints.is_empty() {
366            if !sess.dcx().has_errors().is_some() {
    {
        ::core::panicking::panic_fmt(format_args!("failed to process buffered lint here (dummy = {0})",
                id == ast::DUMMY_NODE_ID));
    }
};assert!(
367                sess.dcx().has_errors().is_some(),
368                "failed to process buffered lint here (dummy = {})",
369                id == ast::DUMMY_NODE_ID
370            );
371            break;
372        }
373    }
374}