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, LocalModDefId};
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, LintId, LintStore};
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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("late context: enter_attrs({0:?})",
                                                    attrs) as &dyn 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};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("late context: exit_attrs({0:?})",
                                                    attrs) as &dyn 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, negated: bool) {
155        { self.pass.check_lit(&self.context, hir_id, lit, negated); };lint_callback!(self, check_lit, hir_id, lit, negated);
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        { self.pass.check_struct_def(&self.context, s); };lint_callback!(self, check_struct_def, s);
202        hir_visit::walk_struct_def(self, s);
203    }
204
205    fn visit_field_def(&mut self, s: &'tcx hir::FieldDef<'tcx>) {
206        self.with_lint_attrs(s.hir_id, |cx| {
207            { cx.pass.check_field_def(&cx.context, s); };lint_callback!(cx, check_field_def, s);
208            hir_visit::walk_field_def(cx, s);
209        })
210    }
211
212    fn visit_variant(&mut self, v: &'tcx hir::Variant<'tcx>) {
213        self.with_lint_attrs(v.hir_id, |cx| {
214            { cx.pass.check_variant(&cx.context, v); };lint_callback!(cx, check_variant, v);
215            hir_visit::walk_variant(cx, v);
216        })
217    }
218
219    fn visit_ty(&mut self, t: &'tcx hir::Ty<'tcx, AmbigArg>) {
220        { self.pass.check_ty(&self.context, t); };lint_callback!(self, check_ty, t);
221        hir_visit::walk_ty(self, t);
222    }
223
224    fn visit_mod(&mut self, m: &'tcx hir::Mod<'tcx>, _: Span, n: HirId) {
225        if !self.context.only_module {
226            self.process_mod(m, n);
227        }
228    }
229
230    fn visit_local(&mut self, l: &'tcx hir::LetStmt<'tcx>) {
231        self.with_lint_attrs(l.hir_id, |cx| {
232            { cx.pass.check_local(&cx.context, l); };lint_callback!(cx, check_local, l);
233            hir_visit::walk_local(cx, l);
234        })
235    }
236
237    fn visit_block(&mut self, b: &'tcx hir::Block<'tcx>) {
238        { self.pass.check_block(&self.context, b); };lint_callback!(self, check_block, b);
239        hir_visit::walk_block(self, b);
240        { self.pass.check_block_post(&self.context, b); };lint_callback!(self, check_block_post, b);
241    }
242
243    fn visit_arm(&mut self, a: &'tcx hir::Arm<'tcx>) {
244        self.with_lint_attrs(a.hir_id, |cx| {
245            { cx.pass.check_arm(&cx.context, a); };lint_callback!(cx, check_arm, a);
246            hir_visit::walk_arm(cx, a);
247        })
248    }
249
250    fn visit_generic_param(&mut self, p: &'tcx hir::GenericParam<'tcx>) {
251        { self.pass.check_generic_param(&self.context, p); };lint_callback!(self, check_generic_param, p);
252        hir_visit::walk_generic_param(self, p);
253    }
254
255    fn visit_generics(&mut self, g: &'tcx hir::Generics<'tcx>) {
256        { self.pass.check_generics(&self.context, g); };lint_callback!(self, check_generics, g);
257        hir_visit::walk_generics(self, g);
258    }
259
260    fn visit_where_predicate(&mut self, p: &'tcx hir::WherePredicate<'tcx>) {
261        hir_visit::walk_where_predicate(self, p);
262    }
263
264    fn visit_poly_trait_ref(&mut self, t: &'tcx hir::PolyTraitRef<'tcx>) {
265        { self.pass.check_poly_trait_ref(&self.context, t); };lint_callback!(self, check_poly_trait_ref, t);
266        hir_visit::walk_poly_trait_ref(self, t);
267    }
268
269    fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
270        let generics = self.context.generics.take();
271        self.context.generics = Some(trait_item.generics);
272        self.with_lint_attrs(trait_item.hir_id(), |cx| {
273            cx.with_param_env(trait_item.owner_id, |cx| {
274                { cx.pass.check_trait_item(&cx.context, trait_item); };lint_callback!(cx, check_trait_item, trait_item);
275                hir_visit::walk_trait_item(cx, trait_item);
276            });
277        });
278        self.context.generics = generics;
279    }
280
281    fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
282        let generics = self.context.generics.take();
283        self.context.generics = Some(impl_item.generics);
284        self.with_lint_attrs(impl_item.hir_id(), |cx| {
285            cx.with_param_env(impl_item.owner_id, |cx| {
286                { cx.pass.check_impl_item(&cx.context, impl_item); };lint_callback!(cx, check_impl_item, impl_item);
287                hir_visit::walk_impl_item(cx, impl_item);
288                { cx.pass.check_impl_item_post(&cx.context, impl_item); };lint_callback!(cx, check_impl_item_post, impl_item);
289            });
290        });
291        self.context.generics = generics;
292    }
293
294    fn visit_lifetime(&mut self, lt: &'tcx hir::Lifetime) {
295        hir_visit::walk_lifetime(self, lt);
296    }
297
298    fn visit_path(&mut self, p: &hir::Path<'tcx>, id: HirId) {
299        { self.pass.check_path(&self.context, p, id); };lint_callback!(self, check_path, p, id);
300        hir_visit::walk_path(self, p);
301    }
302}
303
304// Combines multiple lint passes into a single pass, at runtime. Each
305// `check_foo` method in `$methods` within this pass simply calls `check_foo`
306// once per `$pass`. Compare with `declare_combined_late_lint_pass`, which is
307// similar, but combines lint passes at compile time.
308struct RuntimeCombinedLateLintPass<'tcx> {
309    passes: Vec<LateLintPassObject<'tcx>>,
310}
311
312#[allow(rustc::lint_pass_impl_without_macro)]
313impl LintPass for RuntimeCombinedLateLintPass<'_> {
314    fn name(&self) -> &'static str {
315        ::core::panicking::panic("explicit panic")panic!()
316    }
317    fn get_lints(&self) -> crate::LintVec {
318        ::core::panicking::panic("explicit panic")panic!()
319    }
320}
321
322macro_rules! impl_late_lint_pass {
323    ([], [$($(#[$attr:meta])* fn $f:ident($($param:ident: $arg:ty),*);)*]) => {
324        impl<'tcx> LateLintPass<'tcx> for RuntimeCombinedLateLintPass<'tcx> {
325            $(fn $f(&mut self, context: &LateContext<'tcx>, $($param: $arg),*) {
326                for pass in self.passes.iter_mut() {
327                    pass.$f(context, $($param),*);
328                }
329            })*
330        }
331    };
332}
333
334impl<'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, negated: bool) {
        for pass in self.passes.iter_mut() {
            pass.check_lit(context, hir_id, a, negated);
        }
    }
    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_struct_def(&mut self, context: &LateContext<'tcx>,
        a: &'tcx rustc_hir::VariantData<'tcx>) {
        for pass in self.passes.iter_mut() {
            pass.check_struct_def(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, []);
335
336pub fn late_lint_mod<'tcx, T: LateLintPass<'tcx> + 'tcx>(
337    tcx: TyCtxt<'tcx>,
338    module_def_id: LocalModDefId,
339    builtin_lints: T,
340) {
341    let context = LateContext {
342        tcx,
343        enclosing_body: None,
344        cached_typeck_results: Cell::new(None),
345        param_env: ty::ParamEnv::empty(),
346        effective_visibilities: tcx.effective_visibilities(()),
347        last_node_with_lint_attrs: tcx.local_def_id_to_hir_id(module_def_id),
348        generics: None,
349        only_module: true,
350    };
351
352    // Note: `passes` is often empty. In that case, it's faster to run
353    // `builtin_lints` directly rather than bundling it up into the
354    // `RuntimeCombinedLateLintPass`.
355    let store = unerased_lint_store(tcx.sess);
356
357    if store.late_module_passes.is_empty() {
358        // If all builtin lints can be skipped, there is no point in running `late_lint_mod_inner`
359        // at all. This happens often for dependencies built with `--cap-lints=allow`.
360        let dont_need_to_run = tcx.lints_that_dont_need_to_run(());
361        let can_skip_lints = builtin_lints
362            .get_lints()
363            .iter()
364            .all(|lint| dont_need_to_run.contains(&LintId::of(lint)));
365        if !can_skip_lints {
366            late_lint_mod_inner(tcx, module_def_id, context, builtin_lints);
367        }
368    } else {
369        let builtin_lints = Box::new(builtin_lints) as Box<dyn LateLintPass<'tcx>>;
370        let passes = store
371            .late_module_passes
372            .iter()
373            .map(|mk_pass| (mk_pass)(tcx))
374            .chain(std::iter::once(builtin_lints))
375            .collect::<Vec<_>>();
376
377        let pass = RuntimeCombinedLateLintPass { passes };
378        late_lint_mod_inner(tcx, module_def_id, context, pass);
379    }
380}
381
382fn late_lint_mod_inner<'tcx, T: LateLintPass<'tcx>>(
383    tcx: TyCtxt<'tcx>,
384    module_def_id: LocalModDefId,
385    context: LateContext<'tcx>,
386    pass: T,
387) {
388    let mut cx = LateContextAndPass { context, pass };
389
390    let (module, _span, hir_id) = tcx.hir_get_module(module_def_id);
391
392    cx.with_lint_attrs(hir_id, |cx| {
393        // There is no module lint that will have the crate itself as an item, so check it here.
394        if hir_id == hir::CRATE_HIR_ID {
395            { cx.pass.check_crate(&cx.context); };lint_callback!(cx, check_crate,);
396        }
397
398        cx.process_mod(module, hir_id);
399
400        if hir_id == hir::CRATE_HIR_ID {
401            { cx.pass.check_crate_post(&cx.context); };lint_callback!(cx, check_crate_post,);
402        }
403    });
404}
405
406fn late_lint_crate<'tcx>(tcx: TyCtxt<'tcx>) {
407    let lints_that_dont_need_to_run = tcx.lints_that_dont_need_to_run(());
408
409    // Note: `passes` is often empty after filtering.
410    let mut passes: Vec<_> =
411        unerased_lint_store(tcx.sess).late_passes.iter().map(|mk_pass| (mk_pass)(tcx)).collect();
412    passes.retain(|pass| {
413        let lints = pass.get_lints();
414        // Lintless passes are always in
415        lints.is_empty() ||
416            // If the pass doesn't have a single needed lint, omit it
417            !lints.iter().all(|lint| lints_that_dont_need_to_run.contains(&LintId::of(lint)))
418    });
419    if passes.is_empty() {
420        return;
421    }
422
423    let context = LateContext {
424        tcx,
425        enclosing_body: None,
426        cached_typeck_results: Cell::new(None),
427        param_env: ty::ParamEnv::empty(),
428        effective_visibilities: tcx.effective_visibilities(()),
429        last_node_with_lint_attrs: hir::CRATE_HIR_ID,
430        generics: None,
431        only_module: false,
432    };
433
434    let pass = RuntimeCombinedLateLintPass { passes };
435    let mut cx = LateContextAndPass { context, pass };
436
437    // Visit the whole crate.
438    cx.with_lint_attrs(hir::CRATE_HIR_ID, |cx| {
439        // Since the root module isn't visited as an item (because it isn't an
440        // item), warn for it here.
441        { cx.pass.check_crate(&cx.context); };lint_callback!(cx, check_crate,);
442        tcx.hir_walk_toplevel_module(cx);
443        { cx.pass.check_crate_post(&cx.context); };lint_callback!(cx, check_crate_post,);
444    })
445}
446
447/// Performs lint checking on a crate.
448pub fn check_crate<'tcx>(tcx: TyCtxt<'tcx>) {
449    par_join(
450        || {
451            tcx.sess.time("crate_lints", || {
452                // Run whole crate non-incremental lints
453                late_lint_crate(tcx);
454            });
455        },
456        || {
457            tcx.sess.time("module_lints", || {
458                // Run per-module lints
459                tcx.par_hir_for_each_module(|module| tcx.ensure_ok().lint_mod(module));
460            });
461        },
462    );
463}