Skip to main content

rustc_lint/
late.rs

1//! Implementation of the late lint pass.
2//!
3//! The late lint pass Works on HIR nodes, towards the end of analysis (after
4//! borrow checking, etc.). These lints have full type information available.
5
6use std::any::Any;
7
8use rustc_data_structures::sync::par_join;
9use rustc_hir::def_id::{LocalDefId, LocalModId};
10use rustc_hir::{self as hir, AmbigArg, HirId, intravisit as hir_visit};
11use rustc_middle::hir::nested_filter;
12use rustc_middle::ty::{self, TyCtxt};
13use rustc_session::Session;
14use rustc_session::lint::LintPass;
15use rustc_span::Span;
16use tracing::debug;
17
18use crate::passes::LateLintPassObject;
19use crate::{LateContext, LateLintPass, LintStore, is_lint_pass_required};
20
21/// Extract the [`LintStore`] from [`Session`].
22///
23/// This function exists because [`Session::lint_store`] is type-erased.
24pub fn unerased_lint_store(sess: &Session) -> &LintStore {
25    let store: &dyn Any = sess.lint_store.as_deref().unwrap();
26    store.downcast_ref().unwrap()
27}
28
29macro_rules! lint_callback { ($cx:expr, $f:ident, $($args:expr),*) => ({
30    $cx.pass.$f(&$cx.context, $($args),*);
31}) }
32
33/// Implements the AST traversal for late lint passes. `T` provides the
34/// `check_*` methods.
35struct LateContextAndPass<'tcx, T: LateLintPass<'tcx>> {
36    context: LateContext<'tcx>,
37    pass: T,
38
39    /// Rustdoc parses functions which are behind failing `#[cfg]` checks and may not pass
40    /// type checking. See: <https://github.com/rust-lang/rust/pull/73566>
41    /// Inlined from the session variables to avoid the indirection and cache pressure in the
42    /// lint visitor.
43    actually_rustdoc: bool,
44}
45
46impl<'tcx, T: LateLintPass<'tcx>> LateContextAndPass<'tcx, T> {
47    /// Merge the lints specified by any lint attributes into the
48    /// current lint context, call the provided function, then reset the
49    /// lints in effect to their previous state.
50    fn with_lint_attrs<F>(&mut self, id: HirId, f: F)
51    where
52        F: FnOnce(&mut Self),
53    {
54        let attrs = self.context.tcx.hir_attrs(id);
55        let prev = self.context.last_node_with_lint_attrs;
56        self.context.last_node_with_lint_attrs = id;
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/late.rs:57",
                        "rustc_lint::late", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_lint/src/late.rs"),
                        ::tracing_core::__macro_support::Option::Some(57u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_lint::late"),
                        ::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!("late context: enter_attrs({0:?})",
                                                    attrs) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("late context: enter_attrs({:?})", attrs);
58        { self.pass.check_attributes(&self.context, attrs); };lint_callback!(self, check_attributes, attrs);
59        for attr in attrs {
60            { self.pass.check_attribute(&self.context, attr); };lint_callback!(self, check_attribute, attr);
61        }
62        f(self);
63        {
    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/late.rs:63",
                        "rustc_lint::late", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_lint/src/late.rs"),
                        ::tracing_core::__macro_support::Option::Some(63u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_lint::late"),
                        ::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!("late context: exit_attrs({0:?})",
                                                    attrs) as &dyn ::tracing::field::Value))])
            });
    } else { ; }
};debug!("late context: exit_attrs({:?})", attrs);
64        { self.pass.check_attributes_post(&self.context, attrs); };lint_callback!(self, check_attributes_post, attrs);
65        self.context.last_node_with_lint_attrs = prev;
66    }
67
68    fn with_param_env<F>(&mut self, id: hir::OwnerId, f: F)
69    where
70        F: FnOnce(&mut Self),
71    {
72        let old_param_env = self.context.param_env;
73        self.context.param_env = self.context.tcx.param_env(id);
74        f(self);
75        self.context.param_env = old_param_env;
76    }
77
78    fn process_mod(&mut self, m: &'tcx hir::Mod<'tcx>, n: HirId) {
79        { self.pass.check_mod(&self.context, m, n); };lint_callback!(self, check_mod, m, n);
80        hir_visit::walk_mod(self, m);
81    }
82}
83
84impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPass<'tcx, T> {
85    type NestedFilter = nested_filter::All;
86
87    /// Because lints are scoped lexically, we want to walk nested
88    /// items in the context of the outer item, so enable
89    /// deep-walking.
90    fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
91        self.context.tcx
92    }
93
94    fn visit_nested_body(&mut self, body_id: hir::BodyId) {
95        let old_enclosing_body = self.context.enclosing_body.replace(body_id);
96        let old_typeck_results = self.context.typeck_results;
97
98        // The body and typeck results are also set in `visit_fn`.
99        // Only fetch the results if this is for a new body.
100        if old_enclosing_body != Some(body_id) && !self.actually_rustdoc {
101            self.context.typeck_results = Some(self.context.tcx.typeck_body(body_id));
102        }
103
104        let body = self.context.tcx.hir_body(body_id);
105        self.visit_body(body);
106        self.context.enclosing_body = old_enclosing_body;
107        self.context.typeck_results = old_typeck_results;
108    }
109
110    fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
111        self.with_lint_attrs(param.hir_id, |cx| {
112            hir_visit::walk_param(cx, param);
113        });
114    }
115
116    fn visit_body(&mut self, body: &hir::Body<'tcx>) {
117        { self.pass.check_body(&self.context, body); };lint_callback!(self, check_body, body);
118        hir_visit::walk_body(self, body);
119        { self.pass.check_body_post(&self.context, body); };lint_callback!(self, check_body_post, body);
120    }
121
122    fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) {
123        let generics = self.context.generics.take();
124        self.context.generics = it.kind.generics();
125        let old_typeck_results = self.context.typeck_results.take();
126        let old_enclosing_body = self.context.enclosing_body.take();
127        self.with_lint_attrs(it.hir_id(), |cx| {
128            cx.with_param_env(it.owner_id, |cx| {
129                { cx.pass.check_item(&cx.context, it); };lint_callback!(cx, check_item, it);
130                hir_visit::walk_item(cx, it);
131                { cx.pass.check_item_post(&cx.context, it); };lint_callback!(cx, check_item_post, it);
132            });
133        });
134        self.context.enclosing_body = old_enclosing_body;
135        self.context.typeck_results = old_typeck_results;
136        self.context.generics = generics;
137    }
138
139    fn visit_foreign_item(&mut self, it: &'tcx hir::ForeignItem<'tcx>) {
140        self.with_lint_attrs(it.hir_id(), |cx| {
141            cx.with_param_env(it.owner_id, |cx| {
142                { cx.pass.check_foreign_item(&cx.context, it); };lint_callback!(cx, check_foreign_item, it);
143                hir_visit::walk_foreign_item(cx, it);
144            });
145        })
146    }
147
148    fn visit_pat(&mut self, p: &'tcx hir::Pat<'tcx>) {
149        { self.pass.check_pat(&self.context, p); };lint_callback!(self, check_pat, p);
150        hir_visit::walk_pat(self, p);
151    }
152
153    fn visit_lit(&mut self, hir_id: HirId, lit: hir::Lit, is_negated_pat: bool) {
154        { self.pass.check_lit(&self.context, hir_id, lit, is_negated_pat); };lint_callback!(self, check_lit, hir_id, lit, is_negated_pat);
155    }
156
157    fn visit_expr_field(&mut self, field: &'tcx hir::ExprField<'tcx>) {
158        self.with_lint_attrs(field.hir_id, |cx| hir_visit::walk_expr_field(cx, field))
159    }
160
161    fn visit_expr(&mut self, e: &'tcx hir::Expr<'tcx>) {
162        self.with_lint_attrs(e.hir_id, |cx| {
163            { cx.pass.check_expr(&cx.context, e); };lint_callback!(cx, check_expr, e);
164            hir_visit::walk_expr(cx, e);
165            { cx.pass.check_expr_post(&cx.context, e); };lint_callback!(cx, check_expr_post, e);
166        })
167    }
168
169    fn visit_stmt(&mut self, s: &'tcx hir::Stmt<'tcx>) {
170        // See `EarlyContextAndPass::visit_stmt` for an explanation
171        // of why we call `walk_stmt` outside of `with_lint_attrs`
172        self.with_lint_attrs(s.hir_id, |cx| {
173            { cx.pass.check_stmt(&cx.context, s); };lint_callback!(cx, check_stmt, s);
174        });
175        hir_visit::walk_stmt(self, s);
176    }
177
178    fn visit_fn(
179        &mut self,
180        fk: hir_visit::FnKind<'tcx>,
181        decl: &'tcx hir::FnDecl<'tcx>,
182        body_id: hir::BodyId,
183        span: Span,
184        id: LocalDefId,
185    ) {
186        // Wrap in typeck results here, not just in visit_nested_body,
187        // in order for `check_fn` to be able to use them.
188        let old_enclosing_body = self.context.enclosing_body.replace(body_id);
189        let old_typeck_results = self.context.typeck_results;
190        if !self.actually_rustdoc {
191            self.context.typeck_results = Some(self.context.tcx.typeck_body(body_id));
192        }
193        let body = self.context.tcx.hir_body(body_id);
194        { self.pass.check_fn(&self.context, fk, decl, body, span, id); };lint_callback!(self, check_fn, fk, decl, body, span, id);
195        hir_visit::walk_fn(self, fk, decl, body_id, id);
196        self.context.enclosing_body = old_enclosing_body;
197        self.context.typeck_results = old_typeck_results;
198    }
199
200    fn visit_variant_data(&mut self, s: &'tcx hir::VariantData<'tcx>) {
201        hir_visit::walk_struct_def(self, s);
202    }
203
204    fn visit_field_def(&mut self, s: &'tcx hir::FieldDef<'tcx>) {
205        self.with_lint_attrs(s.hir_id, |cx| {
206            { cx.pass.check_field_def(&cx.context, s); };lint_callback!(cx, check_field_def, s);
207            hir_visit::walk_field_def(cx, s);
208        })
209    }
210
211    fn visit_variant(&mut self, v: &'tcx hir::Variant<'tcx>) {
212        self.with_lint_attrs(v.hir_id, |cx| {
213            { cx.pass.check_variant(&cx.context, v); };lint_callback!(cx, check_variant, v);
214            hir_visit::walk_variant(cx, v);
215        })
216    }
217
218    fn visit_ty(&mut self, t: &'tcx hir::Ty<'tcx, AmbigArg>) {
219        { self.pass.check_ty(&self.context, t); };lint_callback!(self, check_ty, t);
220        hir_visit::walk_ty(self, t);
221    }
222
223    fn visit_mod(&mut self, m: &'tcx hir::Mod<'tcx>, _: Span, n: HirId) {
224        if !self.context.only_module {
225            self.process_mod(m, n);
226        }
227    }
228
229    fn visit_local(&mut self, l: &'tcx hir::LetStmt<'tcx>) {
230        self.with_lint_attrs(l.hir_id, |cx| {
231            { cx.pass.check_local(&cx.context, l); };lint_callback!(cx, check_local, l);
232            hir_visit::walk_local(cx, l);
233        })
234    }
235
236    fn visit_block(&mut self, b: &'tcx hir::Block<'tcx>) {
237        { self.pass.check_block(&self.context, b); };lint_callback!(self, check_block, b);
238        hir_visit::walk_block(self, b);
239        { self.pass.check_block_post(&self.context, b); };lint_callback!(self, check_block_post, b);
240    }
241
242    fn visit_arm(&mut self, a: &'tcx hir::Arm<'tcx>) {
243        self.with_lint_attrs(a.hir_id, |cx| {
244            { cx.pass.check_arm(&cx.context, a); };lint_callback!(cx, check_arm, a);
245            hir_visit::walk_arm(cx, a);
246        })
247    }
248
249    fn visit_generic_param(&mut self, p: &'tcx hir::GenericParam<'tcx>) {
250        { self.pass.check_generic_param(&self.context, p); };lint_callback!(self, check_generic_param, p);
251        hir_visit::walk_generic_param(self, p);
252    }
253
254    fn visit_generics(&mut self, g: &'tcx hir::Generics<'tcx>) {
255        { self.pass.check_generics(&self.context, g); };lint_callback!(self, check_generics, g);
256        hir_visit::walk_generics(self, g);
257    }
258
259    fn visit_where_predicate(&mut self, p: &'tcx hir::WherePredicate<'tcx>) {
260        hir_visit::walk_where_predicate(self, p);
261    }
262
263    fn visit_poly_trait_ref(&mut self, t: &'tcx hir::PolyTraitRef<'tcx>) {
264        { self.pass.check_poly_trait_ref(&self.context, t); };lint_callback!(self, check_poly_trait_ref, t);
265        hir_visit::walk_poly_trait_ref(self, t);
266    }
267
268    fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
269        let generics = self.context.generics.take();
270        self.context.generics = Some(trait_item.generics);
271        self.with_lint_attrs(trait_item.hir_id(), |cx| {
272            cx.with_param_env(trait_item.owner_id, |cx| {
273                { cx.pass.check_trait_item(&cx.context, trait_item); };lint_callback!(cx, check_trait_item, trait_item);
274                hir_visit::walk_trait_item(cx, trait_item);
275            });
276        });
277        self.context.generics = generics;
278    }
279
280    fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
281        let generics = self.context.generics.take();
282        self.context.generics = Some(impl_item.generics);
283        self.with_lint_attrs(impl_item.hir_id(), |cx| {
284            cx.with_param_env(impl_item.owner_id, |cx| {
285                { cx.pass.check_impl_item(&cx.context, impl_item); };lint_callback!(cx, check_impl_item, impl_item);
286                hir_visit::walk_impl_item(cx, impl_item);
287                { cx.pass.check_impl_item_post(&cx.context, impl_item); };lint_callback!(cx, check_impl_item_post, impl_item);
288            });
289        });
290        self.context.generics = generics;
291    }
292
293    fn visit_lifetime(&mut self, lt: &'tcx hir::Lifetime) {
294        hir_visit::walk_lifetime(self, lt);
295    }
296
297    fn visit_path(&mut self, p: &hir::Path<'tcx>, id: HirId) {
298        { self.pass.check_path(&self.context, p, id); };lint_callback!(self, check_path, p, id);
299        hir_visit::walk_path(self, p);
300    }
301}
302
303// Combines multiple lint passes into a single pass, at runtime. Each
304// `check_foo` method in `$methods` within this pass simply calls `check_foo`
305// once per `$pass`. Compare with `declare_combined_late_lint_pass`, which is
306// similar, but combines lint passes at compile time.
307struct RuntimeCombinedLateLintPass<'tcx> {
308    passes: Vec<LateLintPassObject<'tcx>>,
309}
310
311#[allow(rustc::lint_pass_impl_without_macro)]
312impl LintPass for RuntimeCombinedLateLintPass<'_> {
313    fn name(&self) -> &'static str {
314        ::core::panicking::panic("explicit panic")panic!()
315    }
316    fn get_lints(&self) -> crate::LintVec {
317        ::core::panicking::panic("explicit panic")panic!()
318    }
319}
320
321macro_rules! impl_late_lint_pass {
322    ([], [$($(#[$attr:meta])* fn $f:ident($($param:ident: $arg:ty),*);)*]) => {
323        impl<'tcx> LateLintPass<'tcx> for RuntimeCombinedLateLintPass<'tcx> {
324            $(fn $f(&mut self, context: &LateContext<'tcx>, $($param: $arg),*) {
325                for pass in self.passes.iter_mut() {
326                    pass.$f(context, $($param),*);
327                }
328            })*
329        }
330    };
331}
332
333impl<'tcx> LateLintPass<'tcx> for RuntimeCombinedLateLintPass<'tcx> {
    fn check_body(&mut self, context: &LateContext<'tcx>,
        a: &rustc_hir::Body<'tcx>) {
        for pass in self.passes.iter_mut() { pass.check_body(context, a); }
    }
    fn check_body_post(&mut self, context: &LateContext<'tcx>,
        a: &rustc_hir::Body<'tcx>) {
        for pass in self.passes.iter_mut() {
            pass.check_body_post(context, a);
        }
    }
    fn check_crate(&mut self, context: &LateContext<'tcx>) {
        for pass in self.passes.iter_mut() { pass.check_crate(context); }
    }
    fn check_crate_post(&mut self, context: &LateContext<'tcx>) {
        for pass in self.passes.iter_mut() { pass.check_crate_post(context); }
    }
    fn check_mod(&mut self, context: &LateContext<'tcx>,
        a: &'tcx rustc_hir::Mod<'tcx>, b: rustc_hir::HirId) {
        for pass in self.passes.iter_mut() { pass.check_mod(context, a, b); }
    }
    fn check_foreign_item(&mut self, context: &LateContext<'tcx>,
        a: &'tcx rustc_hir::ForeignItem<'tcx>) {
        for pass in self.passes.iter_mut() {
            pass.check_foreign_item(context, a);
        }
    }
    fn check_item(&mut self, context: &LateContext<'tcx>,
        a: &'tcx rustc_hir::Item<'tcx>) {
        for pass in self.passes.iter_mut() { pass.check_item(context, a); }
    }
    fn check_item_post(&mut self, context: &LateContext<'tcx>,
        a: &'tcx rustc_hir::Item<'tcx>) {
        for pass in self.passes.iter_mut() {
            pass.check_item_post(context, a);
        }
    }
    fn check_local(&mut self, context: &LateContext<'tcx>,
        a: &'tcx rustc_hir::LetStmt<'tcx>) {
        for pass in self.passes.iter_mut() { pass.check_local(context, a); }
    }
    fn check_block(&mut self, context: &LateContext<'tcx>,
        a: &'tcx rustc_hir::Block<'tcx>) {
        for pass in self.passes.iter_mut() { pass.check_block(context, a); }
    }
    fn check_block_post(&mut self, context: &LateContext<'tcx>,
        a: &'tcx rustc_hir::Block<'tcx>) {
        for pass in self.passes.iter_mut() {
            pass.check_block_post(context, a);
        }
    }
    fn check_stmt(&mut self, context: &LateContext<'tcx>,
        a: &'tcx rustc_hir::Stmt<'tcx>) {
        for pass in self.passes.iter_mut() { pass.check_stmt(context, a); }
    }
    fn check_arm(&mut self, context: &LateContext<'tcx>,
        a: &'tcx rustc_hir::Arm<'tcx>) {
        for pass in self.passes.iter_mut() { pass.check_arm(context, a); }
    }
    fn check_pat(&mut self, context: &LateContext<'tcx>,
        a: &'tcx rustc_hir::Pat<'tcx>) {
        for pass in self.passes.iter_mut() { pass.check_pat(context, a); }
    }
    fn check_lit(&mut self, context: &LateContext<'tcx>,
        hir_id: rustc_hir::HirId, a: rustc_hir::Lit, is_negated_pat: bool) {
        for pass in self.passes.iter_mut() {
            pass.check_lit(context, hir_id, a, is_negated_pat);
        }
    }
    fn check_expr(&mut self, context: &LateContext<'tcx>,
        a: &'tcx rustc_hir::Expr<'tcx>) {
        for pass in self.passes.iter_mut() { pass.check_expr(context, a); }
    }
    fn check_expr_post(&mut self, context: &LateContext<'tcx>,
        a: &'tcx rustc_hir::Expr<'tcx>) {
        for pass in self.passes.iter_mut() {
            pass.check_expr_post(context, a);
        }
    }
    fn check_ty(&mut self, context: &LateContext<'tcx>,
        a: &'tcx rustc_hir::Ty<'tcx, rustc_hir::AmbigArg>) {
        for pass in self.passes.iter_mut() { pass.check_ty(context, a); }
    }
    fn check_generic_param(&mut self, context: &LateContext<'tcx>,
        a: &'tcx rustc_hir::GenericParam<'tcx>) {
        for pass in self.passes.iter_mut() {
            pass.check_generic_param(context, a);
        }
    }
    fn check_generics(&mut self, context: &LateContext<'tcx>,
        a: &'tcx rustc_hir::Generics<'tcx>) {
        for pass in self.passes.iter_mut() {
            pass.check_generics(context, a);
        }
    }
    fn check_poly_trait_ref(&mut self, context: &LateContext<'tcx>,
        a: &'tcx rustc_hir::PolyTraitRef<'tcx>) {
        for pass in self.passes.iter_mut() {
            pass.check_poly_trait_ref(context, a);
        }
    }
    fn check_fn(&mut self, context: &LateContext<'tcx>,
        a: rustc_hir::intravisit::FnKind<'tcx>,
        b: &'tcx rustc_hir::FnDecl<'tcx>, c: &'tcx rustc_hir::Body<'tcx>,
        d: rustc_span::Span, e: rustc_span::def_id::LocalDefId) {
        for pass in self.passes.iter_mut() {
            pass.check_fn(context, a, b, c, d, e);
        }
    }
    fn check_trait_item(&mut self, context: &LateContext<'tcx>,
        a: &'tcx rustc_hir::TraitItem<'tcx>) {
        for pass in self.passes.iter_mut() {
            pass.check_trait_item(context, a);
        }
    }
    fn check_impl_item(&mut self, context: &LateContext<'tcx>,
        a: &'tcx rustc_hir::ImplItem<'tcx>) {
        for pass in self.passes.iter_mut() {
            pass.check_impl_item(context, a);
        }
    }
    fn check_impl_item_post(&mut self, context: &LateContext<'tcx>,
        a: &'tcx rustc_hir::ImplItem<'tcx>) {
        for pass in self.passes.iter_mut() {
            pass.check_impl_item_post(context, a);
        }
    }
    fn check_field_def(&mut self, context: &LateContext<'tcx>,
        a: &'tcx rustc_hir::FieldDef<'tcx>) {
        for pass in self.passes.iter_mut() {
            pass.check_field_def(context, a);
        }
    }
    fn check_variant(&mut self, context: &LateContext<'tcx>,
        a: &'tcx rustc_hir::Variant<'tcx>) {
        for pass in self.passes.iter_mut() { pass.check_variant(context, a); }
    }
    fn check_path(&mut self, context: &LateContext<'tcx>,
        a: &rustc_hir::Path<'tcx>, b: rustc_hir::HirId) {
        for pass in self.passes.iter_mut() { pass.check_path(context, a, b); }
    }
    fn check_attribute(&mut self, context: &LateContext<'tcx>,
        a: &'tcx rustc_hir::Attribute) {
        for pass in self.passes.iter_mut() {
            pass.check_attribute(context, a);
        }
    }
    fn check_attributes(&mut self, context: &LateContext<'tcx>,
        a: &'tcx [rustc_hir::Attribute]) {
        for pass in self.passes.iter_mut() {
            pass.check_attributes(context, a);
        }
    }
    fn check_attributes_post(&mut self, context: &LateContext<'tcx>,
        a: &'tcx [rustc_hir::Attribute]) {
        for pass in self.passes.iter_mut() {
            pass.check_attributes_post(context, a);
        }
    }
}crate::late_lint_methods!(impl_late_lint_pass, []);
334
335pub fn late_lint_mod<'tcx, T: LateLintPass<'tcx> + 'tcx>(
336    tcx: TyCtxt<'tcx>,
337    mod_id: LocalModId,
338    builtin_lints: T,
339) {
340    let context = LateContext {
341        tcx,
342        enclosing_body: None,
343        typeck_results: None,
344        param_env: ty::ParamEnv::empty(),
345        effective_visibilities: tcx.effective_visibilities(()),
346        last_node_with_lint_attrs: tcx.local_def_id_to_hir_id(mod_id),
347        generics: None,
348        only_module: true,
349    };
350
351    let skippable_lints = tcx.skippable_lints(());
352
353    // Note: `passes` is often empty. In that case, it's faster to run
354    // `builtin_lints` directly rather than bundling it up into the
355    // `RuntimeCombinedLateLintPass`.
356    let mut passes: Vec<_> = unerased_lint_store(tcx.sess)
357        .late_lint_mod_passes
358        .iter()
359        .map(|mk_pass| mk_pass(tcx))
360        .filter(|pass| is_lint_pass_required(skippable_lints, &pass.get_lints()))
361        .collect();
362    let builtin_lints_must_run = is_lint_pass_required(skippable_lints, &builtin_lints.get_lints());
363    if passes.is_empty() {
364        if builtin_lints_must_run {
365            late_lint_mod_inner(tcx, mod_id, context, builtin_lints);
366        }
367    } else {
368        if builtin_lints_must_run {
369            passes.push(Box::new(builtin_lints) as Box<dyn LateLintPass<'tcx>>);
370        }
371        let pass = RuntimeCombinedLateLintPass { passes };
372        late_lint_mod_inner(tcx, mod_id, context, pass);
373    }
374}
375
376fn late_lint_mod_inner<'tcx, T: LateLintPass<'tcx>>(
377    tcx: TyCtxt<'tcx>,
378    mod_id: LocalModId,
379    context: LateContext<'tcx>,
380    pass: T,
381) {
382    let mut cx = LateContextAndPass::<'tcx, T> {
383        context,
384        pass,
385        actually_rustdoc: tcx.sess.opts.actually_rustdoc,
386    };
387
388    let (module, _span, hir_id) = tcx.hir_get_module(mod_id);
389
390    cx.with_lint_attrs(hir_id, |cx| {
391        // There is no module lint that will have the crate itself as an item, so check it here.
392        if hir_id == hir::CRATE_HIR_ID {
393            { cx.pass.check_crate(&cx.context); };lint_callback!(cx, check_crate,);
394        }
395
396        cx.process_mod(module, hir_id);
397
398        if hir_id == hir::CRATE_HIR_ID {
399            { cx.pass.check_crate_post(&cx.context); };lint_callback!(cx, check_crate_post,);
400        }
401    });
402}
403
404fn late_lint_crate<'tcx>(tcx: TyCtxt<'tcx>) {
405    let skippable_lints = tcx.skippable_lints(());
406
407    // Note: `passes` is often empty after filtering.
408    let passes: Vec<_> = unerased_lint_store(tcx.sess)
409        .late_lint_passes
410        .iter()
411        .map(|mk_pass| mk_pass(tcx))
412        .filter(|pass| is_lint_pass_required(skippable_lints, &pass.get_lints()))
413        .collect();
414    if passes.is_empty() {
415        return;
416    }
417
418    let context = LateContext {
419        tcx,
420        enclosing_body: None,
421        typeck_results: None,
422        param_env: ty::ParamEnv::empty(),
423        effective_visibilities: tcx.effective_visibilities(()),
424        last_node_with_lint_attrs: hir::CRATE_HIR_ID,
425        generics: None,
426        only_module: false,
427    };
428
429    let pass = RuntimeCombinedLateLintPass { passes };
430    let mut cx =
431        LateContextAndPass { context, pass, actually_rustdoc: tcx.sess.opts.actually_rustdoc };
432
433    // Visit the whole crate.
434    cx.with_lint_attrs(hir::CRATE_HIR_ID, |cx| {
435        // Since the root module isn't visited as an item (because it isn't an
436        // item), warn for it here.
437        { cx.pass.check_crate(&cx.context); };lint_callback!(cx, check_crate,);
438        tcx.hir_walk_toplevel_module(cx);
439        { cx.pass.check_crate_post(&cx.context); };lint_callback!(cx, check_crate_post,);
440    })
441}
442
443/// Performs lint checking on a crate.
444pub fn check_crate<'tcx>(tcx: TyCtxt<'tcx>) {
445    par_join(
446        || {
447            tcx.sess.time("crate_lints", || {
448                // Run whole crate non-incremental lints
449                late_lint_crate(tcx);
450            });
451        },
452        || {
453            tcx.sess.time("module_lints", || {
454                // Run per-module lints
455                tcx.par_hir_for_each_module(|module| tcx.ensure_ok().lint_mod(module));
456            });
457        },
458    );
459}