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;
7use std::cell::Cell;
8
9use rustc_data_structures::stack::ensure_sufficient_stack;
10use rustc_data_structures::sync::par_join;
11use rustc_hir::def_id::{LocalDefId, LocalModId};
12use rustc_hir::{self as hir, AmbigArg, HirId, intravisit as hir_visit};
13use rustc_middle::hir::nested_filter;
14use rustc_middle::ty::{self, TyCtxt};
15use rustc_session::Session;
16use rustc_session::lint::LintPass;
17use rustc_span::Span;
18use tracing::debug;
19
20use crate::passes::LateLintPassObject;
21use crate::{LateContext, LateLintPass, LintStore, is_lint_pass_required};
22
23/// Extract the [`LintStore`] from [`Session`].
24///
25/// This function exists because [`Session::lint_store`] is type-erased.
26pub fn unerased_lint_store(sess: &Session) -> &LintStore {
27    let store: &dyn Any = sess.lint_store.as_deref().unwrap();
28    store.downcast_ref().unwrap()
29}
30
31macro_rules! lint_callback { ($cx:expr, $f:ident, $($args:expr),*) => ({
32    $cx.pass.$f(&$cx.context, $($args),*);
33}) }
34
35/// Implements the AST traversal for late lint passes. `T` provides the
36/// `check_*` methods.
37struct LateContextAndPass<'tcx, T: LateLintPass<'tcx>> {
38    context: LateContext<'tcx>,
39    pass: T,
40}
41
42impl<'tcx, T: LateLintPass<'tcx>> LateContextAndPass<'tcx, T> {
43    /// Merge the lints specified by any lint attributes into the
44    /// current lint context, call the provided function, then reset the
45    /// lints in effect to their previous state.
46    fn with_lint_attrs<F>(&mut self, id: HirId, f: F)
47    where
48        F: FnOnce(&mut Self),
49    {
50        let attrs = self.context.tcx.hir_attrs(id);
51        let prev = self.context.last_node_with_lint_attrs;
52        self.context.last_node_with_lint_attrs = id;
53        {
    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:53",
                        "rustc_lint::late", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_lint/src/late.rs"),
                        ::tracing_core::__macro_support::Option::Some(53u32),
                        ::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);
54        { self.pass.check_attributes(&self.context, attrs); };lint_callback!(self, check_attributes, attrs);
55        for attr in attrs {
56            { self.pass.check_attribute(&self.context, attr); };lint_callback!(self, check_attribute, attr);
57        }
58        f(self);
59        {
    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:59",
                        "rustc_lint::late", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_lint/src/late.rs"),
                        ::tracing_core::__macro_support::Option::Some(59u32),
                        ::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);
60        { self.pass.check_attributes_post(&self.context, attrs); };lint_callback!(self, check_attributes_post, attrs);
61        self.context.last_node_with_lint_attrs = prev;
62    }
63
64    fn with_param_env<F>(&mut self, id: hir::OwnerId, f: F)
65    where
66        F: FnOnce(&mut Self),
67    {
68        let old_param_env = self.context.param_env;
69        self.context.param_env = self.context.tcx.param_env(id);
70        f(self);
71        self.context.param_env = old_param_env;
72    }
73
74    fn process_mod(&mut self, m: &'tcx hir::Mod<'tcx>, n: HirId) {
75        { self.pass.check_mod(&self.context, m, n); };lint_callback!(self, check_mod, m, n);
76        hir_visit::walk_mod(self, m);
77    }
78}
79
80impl<'tcx, T: LateLintPass<'tcx>> hir_visit::Visitor<'tcx> for LateContextAndPass<'tcx, T> {
81    type NestedFilter = nested_filter::All;
82
83    /// Because lints are scoped lexically, we want to walk nested
84    /// items in the context of the outer item, so enable
85    /// deep-walking.
86    fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
87        self.context.tcx
88    }
89
90    fn visit_nested_body(&mut self, body_id: hir::BodyId) {
91        let old_enclosing_body = self.context.enclosing_body.replace(body_id);
92        let old_cached_typeck_results = self.context.cached_typeck_results.get();
93
94        // HACK(eddyb) avoid trashing `cached_typeck_results` when we're
95        // nested in `visit_fn`, which may have already resulted in them
96        // being queried.
97        if old_enclosing_body != Some(body_id) {
98            self.context.cached_typeck_results.set(None);
99        }
100
101        let body = self.context.tcx.hir_body(body_id);
102        self.visit_body(body);
103        self.context.enclosing_body = old_enclosing_body;
104
105        // See HACK comment above.
106        if old_enclosing_body != Some(body_id) {
107            self.context.cached_typeck_results.set(old_cached_typeck_results);
108        }
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_cached_typeck_results = self.context.cached_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.cached_typeck_results.set(old_cached_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_cached_typeck_results = self.context.cached_typeck_results.take();
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.cached_typeck_results.set(old_cached_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        cached_typeck_results: Cell::new(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 { context, pass };
383
384    let (module, _span, hir_id) = tcx.hir_get_module(mod_id);
385
386    cx.with_lint_attrs(hir_id, |cx| {
387        // There is no module lint that will have the crate itself as an item, so check it here.
388        if hir_id == hir::CRATE_HIR_ID {
389            { cx.pass.check_crate(&cx.context); };lint_callback!(cx, check_crate,);
390        }
391
392        cx.process_mod(module, hir_id);
393
394        if hir_id == hir::CRATE_HIR_ID {
395            { cx.pass.check_crate_post(&cx.context); };lint_callback!(cx, check_crate_post,);
396        }
397    });
398}
399
400fn late_lint_crate<'tcx>(tcx: TyCtxt<'tcx>) {
401    let skippable_lints = tcx.skippable_lints(());
402
403    // Note: `passes` is often empty after filtering.
404    let passes: Vec<_> = unerased_lint_store(tcx.sess)
405        .late_lint_passes
406        .iter()
407        .map(|mk_pass| mk_pass(tcx))
408        .filter(|pass| is_lint_pass_required(skippable_lints, &pass.get_lints()))
409        .collect();
410    if passes.is_empty() {
411        return;
412    }
413
414    let context = LateContext {
415        tcx,
416        enclosing_body: None,
417        cached_typeck_results: Cell::new(None),
418        param_env: ty::ParamEnv::empty(),
419        effective_visibilities: tcx.effective_visibilities(()),
420        last_node_with_lint_attrs: hir::CRATE_HIR_ID,
421        generics: None,
422        only_module: false,
423    };
424
425    let pass = RuntimeCombinedLateLintPass { passes };
426    let mut cx = LateContextAndPass { context, pass };
427
428    // Visit the whole crate.
429    cx.with_lint_attrs(hir::CRATE_HIR_ID, |cx| {
430        // Since the root module isn't visited as an item (because it isn't an
431        // item), warn for it here.
432        { cx.pass.check_crate(&cx.context); };lint_callback!(cx, check_crate,);
433        tcx.hir_walk_toplevel_module(cx);
434        { cx.pass.check_crate_post(&cx.context); };lint_callback!(cx, check_crate_post,);
435    })
436}
437
438/// Performs lint checking on a crate.
439pub fn check_crate<'tcx>(tcx: TyCtxt<'tcx>) {
440    par_join(
441        || {
442            tcx.sess.time("crate_lints", || {
443                // Run whole crate non-incremental lints
444                late_lint_crate(tcx);
445            });
446        },
447        || {
448            tcx.sess.time("module_lints", || {
449                // Run per-module lints
450                tcx.par_hir_for_each_module(|module| tcx.ensure_ok().lint_mod(module));
451            });
452        },
453    );
454}