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::stack::ensure_sufficient_stack;
9use rustc_data_structures::sync::par_join;
10use rustc_hir::def_id::{LocalDefId, LocalModId};
11use rustc_hir::{self as hir, AmbigArg, HirId, intravisit as hir_visit};
12use rustc_middle::hir::nested_filter;
13use rustc_middle::ty::{self, TyCtxt};
14use rustc_session::Session;
15use rustc_session::lint::LintPass;
16use rustc_span::Span;
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 compiler/rustc_lint/src/late.rs:58",
                        "rustc_lint::late", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("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 compiler/rustc_lint/src/late.rs:64",
                        "rustc_lint::late", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("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, m: &'tcx hir::Mod<'tcx>, n: HirId) {
80        { self.pass.check_mod(&self.context, m, n); };lint_callback!(self, check_mod, m, n);
81        hir_visit::walk_mod(self, m);
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            self.context.typeck_results = Some(self.context.tcx.typeck_body(body_id));
103        }
104
105        let body = self.context.tcx.hir_body(body_id);
106        self.visit_body(body);
107        self.context.enclosing_body = old_enclosing_body;
108        self.context.typeck_results = old_typeck_results;
109    }
110
111    fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
112        self.with_lint_attrs(param.hir_id, |cx| {
113            hir_visit::walk_param(cx, param);
114        });
115    }
116
117    fn visit_body(&mut self, body: &hir::Body<'tcx>) {
118        { self.pass.check_body(&self.context, body); };lint_callback!(self, check_body, body);
119        hir_visit::walk_body(self, body);
120        { self.pass.check_body_post(&self.context, body); };lint_callback!(self, check_body_post, body);
121    }
122
123    fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) {
124        let generics = self.context.generics.take();
125        self.context.generics = it.kind.generics();
126        let old_typeck_results = self.context.typeck_results.take();
127        let old_enclosing_body = self.context.enclosing_body.take();
128        self.with_lint_attrs(it.hir_id(), |cx| {
129            cx.with_param_env(it.owner_id, |cx| {
130                { cx.pass.check_item(&cx.context, it); };lint_callback!(cx, check_item, it);
131                hir_visit::walk_item(cx, it);
132                { cx.pass.check_item_post(&cx.context, it); };lint_callback!(cx, check_item_post, it);
133            });
134        });
135        self.context.enclosing_body = old_enclosing_body;
136        self.context.typeck_results = old_typeck_results;
137        self.context.generics = generics;
138    }
139
140    fn visit_foreign_item(&mut self, it: &'tcx hir::ForeignItem<'tcx>) {
141        self.with_lint_attrs(it.hir_id(), |cx| {
142            cx.with_param_env(it.owner_id, |cx| {
143                { cx.pass.check_foreign_item(&cx.context, it); };lint_callback!(cx, check_foreign_item, it);
144                hir_visit::walk_foreign_item(cx, it);
145            });
146        })
147    }
148
149    fn visit_pat(&mut self, p: &'tcx hir::Pat<'tcx>) {
150        { self.pass.check_pat(&self.context, p); };lint_callback!(self, check_pat, p);
151        hir_visit::walk_pat(self, p);
152    }
153
154    fn visit_lit(&mut self, hir_id: HirId, lit: hir::Lit, is_negated_pat: bool) {
155        { self.pass.check_lit(&self.context, hir_id, lit, is_negated_pat); };lint_callback!(self, check_lit, hir_id, lit, is_negated_pat);
156    }
157
158    fn visit_expr_field(&mut self, field: &'tcx hir::ExprField<'tcx>) {
159        self.with_lint_attrs(field.hir_id, |cx| hir_visit::walk_expr_field(cx, field))
160    }
161
162    fn visit_expr(&mut self, e: &'tcx hir::Expr<'tcx>) {
163        ensure_sufficient_stack(|| {
164            self.with_lint_attrs(e.hir_id, |cx| {
165                { cx.pass.check_expr(&cx.context, e); };lint_callback!(cx, check_expr, e);
166                hir_visit::walk_expr(cx, e);
167                { cx.pass.check_expr_post(&cx.context, e); };lint_callback!(cx, check_expr_post, e);
168            })
169        })
170    }
171
172    fn visit_stmt(&mut self, s: &'tcx hir::Stmt<'tcx>) {
173        // See `EarlyContextAndPass::visit_stmt` for an explanation
174        // of why we call `walk_stmt` outside of `with_lint_attrs`
175        self.with_lint_attrs(s.hir_id, |cx| {
176            { cx.pass.check_stmt(&cx.context, s); };lint_callback!(cx, check_stmt, s);
177        });
178        hir_visit::walk_stmt(self, s);
179    }
180
181    fn visit_fn(
182        &mut self,
183        fk: hir_visit::FnKind<'tcx>,
184        decl: &'tcx hir::FnDecl<'tcx>,
185        body_id: hir::BodyId,
186        span: Span,
187        id: LocalDefId,
188    ) {
189        // Wrap in typeck results here, not just in visit_nested_body,
190        // in order for `check_fn` to be able to use them.
191        let old_enclosing_body = self.context.enclosing_body.replace(body_id);
192        let old_typeck_results = self.context.typeck_results;
193        if !self.actually_rustdoc {
194            self.context.typeck_results = Some(self.context.tcx.typeck_body(body_id));
195        }
196        let body = self.context.tcx.hir_body(body_id);
197        { self.pass.check_fn(&self.context, fk, decl, body, span, id); };lint_callback!(self, check_fn, fk, decl, body, span, id);
198        hir_visit::walk_fn(self, fk, decl, body_id, id);
199        self.context.enclosing_body = old_enclosing_body;
200        self.context.typeck_results = old_typeck_results;
201    }
202
203    fn visit_variant_data(&mut self, s: &'tcx hir::VariantData<'tcx>) {
204        hir_visit::walk_struct_def(self, s);
205    }
206
207    fn visit_field_def(&mut self, s: &'tcx hir::FieldDef<'tcx>) {
208        self.with_lint_attrs(s.hir_id, |cx| {
209            { cx.pass.check_field_def(&cx.context, s); };lint_callback!(cx, check_field_def, s);
210            hir_visit::walk_field_def(cx, s);
211        })
212    }
213
214    fn visit_variant(&mut self, v: &'tcx hir::Variant<'tcx>) {
215        self.with_lint_attrs(v.hir_id, |cx| {
216            { cx.pass.check_variant(&cx.context, v); };lint_callback!(cx, check_variant, v);
217            hir_visit::walk_variant(cx, v);
218        })
219    }
220
221    fn visit_ty(&mut self, t: &'tcx hir::Ty<'tcx, AmbigArg>) {
222        { self.pass.check_ty(&self.context, t); };lint_callback!(self, check_ty, t);
223        hir_visit::walk_ty(self, t);
224    }
225
226    fn visit_mod(&mut self, m: &'tcx hir::Mod<'tcx>, _: Span, n: HirId) {
227        if !self.context.only_module {
228            self.process_mod(m, n);
229        }
230    }
231
232    fn visit_local(&mut self, l: &'tcx hir::LetStmt<'tcx>) {
233        self.with_lint_attrs(l.hir_id, |cx| {
234            { cx.pass.check_local(&cx.context, l); };lint_callback!(cx, check_local, l);
235            hir_visit::walk_local(cx, l);
236        })
237    }
238
239    fn visit_block(&mut self, b: &'tcx hir::Block<'tcx>) {
240        { self.pass.check_block(&self.context, b); };lint_callback!(self, check_block, b);
241        hir_visit::walk_block(self, b);
242        { self.pass.check_block_post(&self.context, b); };lint_callback!(self, check_block_post, b);
243    }
244
245    fn visit_arm(&mut self, a: &'tcx hir::Arm<'tcx>) {
246        self.with_lint_attrs(a.hir_id, |cx| {
247            { cx.pass.check_arm(&cx.context, a); };lint_callback!(cx, check_arm, a);
248            hir_visit::walk_arm(cx, a);
249        })
250    }
251
252    fn visit_generic_param(&mut self, p: &'tcx hir::GenericParam<'tcx>) {
253        { self.pass.check_generic_param(&self.context, p); };lint_callback!(self, check_generic_param, p);
254        hir_visit::walk_generic_param(self, p);
255    }
256
257    fn visit_generics(&mut self, g: &'tcx hir::Generics<'tcx>) {
258        { self.pass.check_generics(&self.context, g); };lint_callback!(self, check_generics, g);
259        hir_visit::walk_generics(self, g);
260    }
261
262    fn visit_where_predicate(&mut self, p: &'tcx hir::WherePredicate<'tcx>) {
263        hir_visit::walk_where_predicate(self, p);
264    }
265
266    fn visit_poly_trait_ref(&mut self, t: &'tcx hir::PolyTraitRef<'tcx>) {
267        { self.pass.check_poly_trait_ref(&self.context, t); };lint_callback!(self, check_poly_trait_ref, t);
268        hir_visit::walk_poly_trait_ref(self, t);
269    }
270
271    fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
272        let generics = self.context.generics.take();
273        self.context.generics = Some(trait_item.generics);
274        self.with_lint_attrs(trait_item.hir_id(), |cx| {
275            cx.with_param_env(trait_item.owner_id, |cx| {
276                { cx.pass.check_trait_item(&cx.context, trait_item); };lint_callback!(cx, check_trait_item, trait_item);
277                hir_visit::walk_trait_item(cx, trait_item);
278            });
279        });
280        self.context.generics = generics;
281    }
282
283    fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
284        let generics = self.context.generics.take();
285        self.context.generics = Some(impl_item.generics);
286        self.with_lint_attrs(impl_item.hir_id(), |cx| {
287            cx.with_param_env(impl_item.owner_id, |cx| {
288                { cx.pass.check_impl_item(&cx.context, impl_item); };lint_callback!(cx, check_impl_item, impl_item);
289                hir_visit::walk_impl_item(cx, impl_item);
290                { cx.pass.check_impl_item_post(&cx.context, impl_item); };lint_callback!(cx, check_impl_item_post, impl_item);
291            });
292        });
293        self.context.generics = generics;
294    }
295
296    fn visit_lifetime(&mut self, lt: &'tcx hir::Lifetime) {
297        hir_visit::walk_lifetime(self, lt);
298    }
299
300    fn visit_path(&mut self, p: &hir::Path<'tcx>, id: HirId) {
301        { self.pass.check_path(&self.context, p, id); };lint_callback!(self, check_path, p, id);
302        hir_visit::walk_path(self, p);
303    }
304}
305
306// Combines multiple lint passes into a single pass, at runtime. Each
307// `check_foo` method in `$methods` within this pass simply calls `check_foo`
308// once per `$pass`. Compare with `declare_combined_late_lint_pass`, which is
309// similar, but combines lint passes at compile time.
310struct RuntimeCombinedLateLintPass<'tcx> {
311    passes: Vec<LateLintPassObject<'tcx>>,
312}
313
314#[allow(rustc::lint_pass_impl_without_macro)]
315impl LintPass for RuntimeCombinedLateLintPass<'_> {
316    fn name(&self) -> &'static str {
317        ::core::panicking::panic("explicit panic")panic!()
318    }
319    fn get_lints(&self) -> crate::LintVec {
320        ::core::panicking::panic("explicit panic")panic!()
321    }
322}
323
324macro_rules! impl_late_lint_pass {
325    ([], [$($(#[$attr:meta])* fn $f:ident($($param:ident: $arg:ty),*);)*]) => {
326        impl<'tcx> LateLintPass<'tcx> for RuntimeCombinedLateLintPass<'tcx> {
327            $(fn $f(&mut self, context: &LateContext<'tcx>, $($param: $arg),*) {
328                for pass in self.passes.iter_mut() {
329                    pass.$f(context, $($param),*);
330                }
331            })*
332        }
333    };
334}
335
336impl<'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, []);
337
338pub fn late_lint_mod<'tcx, T: LateLintPass<'tcx> + 'tcx>(
339    tcx: TyCtxt<'tcx>,
340    mod_id: LocalModId,
341    builtin_lints: T,
342) {
343    let context = LateContext {
344        tcx,
345        enclosing_body: None,
346        typeck_results: None,
347        param_env: ty::ParamEnv::empty(),
348        effective_visibilities: tcx.effective_visibilities(()),
349        last_node_with_lint_attrs: tcx.local_def_id_to_hir_id(mod_id),
350        generics: None,
351        only_module: true,
352    };
353
354    let skippable_lints = tcx.skippable_lints(());
355
356    // Note: `passes` is often empty. In that case, it's faster to run
357    // `builtin_lints` directly rather than bundling it up into the
358    // `RuntimeCombinedLateLintPass`.
359    let mut passes: Vec<_> = unerased_lint_store(tcx.sess)
360        .late_lint_mod_passes
361        .iter()
362        .map(|mk_pass| mk_pass(tcx))
363        .filter(|pass| is_lint_pass_required(skippable_lints, &pass.get_lints()))
364        .collect();
365    let builtin_lints_must_run = is_lint_pass_required(skippable_lints, &builtin_lints.get_lints());
366    if passes.is_empty() {
367        if builtin_lints_must_run {
368            late_lint_mod_inner(tcx, mod_id, context, builtin_lints);
369        }
370    } else {
371        if builtin_lints_must_run {
372            passes.push(Box::new(builtin_lints) as Box<dyn LateLintPass<'tcx>>);
373        }
374        let pass = RuntimeCombinedLateLintPass { passes };
375        late_lint_mod_inner(tcx, mod_id, context, pass);
376    }
377}
378
379fn late_lint_mod_inner<'tcx, T: LateLintPass<'tcx>>(
380    tcx: TyCtxt<'tcx>,
381    mod_id: LocalModId,
382    context: LateContext<'tcx>,
383    pass: T,
384) {
385    let mut cx = LateContextAndPass::<'tcx, T> {
386        context,
387        pass,
388        actually_rustdoc: tcx.sess.opts.actually_rustdoc,
389    };
390
391    let (module, _span, hir_id) = tcx.hir_get_module(mod_id);
392
393    cx.with_lint_attrs(hir_id, |cx| {
394        // There is no module lint that will have the crate itself as an item, so check it here.
395        if hir_id == hir::CRATE_HIR_ID {
396            { cx.pass.check_crate(&cx.context); };lint_callback!(cx, check_crate,);
397        }
398
399        cx.process_mod(module, hir_id);
400
401        if hir_id == hir::CRATE_HIR_ID {
402            { cx.pass.check_crate_post(&cx.context); };lint_callback!(cx, check_crate_post,);
403        }
404    });
405}
406
407fn late_lint_crate<'tcx>(tcx: TyCtxt<'tcx>) {
408    let skippable_lints = tcx.skippable_lints(());
409
410    // Note: `passes` is often empty after filtering.
411    let passes: Vec<_> = unerased_lint_store(tcx.sess)
412        .late_lint_passes
413        .iter()
414        .map(|mk_pass| mk_pass(tcx))
415        .filter(|pass| is_lint_pass_required(skippable_lints, &pass.get_lints()))
416        .collect();
417    if passes.is_empty() {
418        return;
419    }
420
421    let context = LateContext {
422        tcx,
423        enclosing_body: None,
424        typeck_results: None,
425        param_env: ty::ParamEnv::empty(),
426        effective_visibilities: tcx.effective_visibilities(()),
427        last_node_with_lint_attrs: hir::CRATE_HIR_ID,
428        generics: None,
429        only_module: false,
430    };
431
432    let pass = RuntimeCombinedLateLintPass { passes };
433    let mut cx =
434        LateContextAndPass { context, pass, actually_rustdoc: tcx.sess.opts.actually_rustdoc };
435
436    // Visit the whole crate.
437    cx.with_lint_attrs(hir::CRATE_HIR_ID, |cx| {
438        // Since the root module isn't visited as an item (because it isn't an
439        // item), warn for it here.
440        { cx.pass.check_crate(&cx.context); };lint_callback!(cx, check_crate,);
441        tcx.hir_walk_toplevel_module(cx);
442        { cx.pass.check_crate_post(&cx.context); };lint_callback!(cx, check_crate_post,);
443    })
444}
445
446/// Performs lint checking on a crate.
447pub fn check_crate<'tcx>(tcx: TyCtxt<'tcx>) {
448    par_join(
449        || {
450            tcx.sess.time("crate_lints", || {
451                // Run whole crate non-incremental lints
452                late_lint_crate(tcx);
453            });
454        },
455        || {
456            tcx.sess.time("module_lints", || {
457                // Run per-module lints
458                tcx.par_hir_for_each_module(|module| tcx.ensure_ok().lint_mod(module));
459            });
460        },
461    );
462}