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