Skip to main content

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