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