1 //! Implementation of lint checking.
3 //! The lint checking is mostly consolidated into one pass which runs
4 //! after all other analyses. Throughout compilation, lint warnings
5 //! can be added via the `add_lint` method on the Session structure. This
6 //! requires a span and an ID of the node that the lint is being added to. The
7 //! lint isn't actually emitted at that time because it is unknown what the
8 //! actual lint level at that location is.
10 //! To actually emit lint warnings/errors, a separate pass is used.
11 //! A context keeps track of the current state of all lint levels.
12 //! Upon entering a node of the ast which can modify the lint settings, the
13 //! previous lint state is pushed onto a stack and the ast is then recursed
14 //! upon. As the ast is traversed, this keeps track of the current lint level
15 //! for all lint attributes.
18 use rustc::hir::def_id::{DefId, LOCAL_CRATE};
19 use rustc::hir::intravisit as hir_visit;
20 use rustc::hir::intravisit::Visitor;
21 use rustc::lint::LateContext;
22 use rustc::lint::LintPass;
23 use rustc::lint::{LateLintPass, LateLintPassObject};
24 use rustc::ty::{self, TyCtxt};
25 use rustc::util::common::time;
27 use rustc_data_structures::sync::{join, par_iter, ParallelIterator};
33 use syntax::walk_list;
35 macro_rules! lint_callback { ($cx:expr, $f:ident, $($args:expr),*) => ({
36 $cx.pass.$f(&$cx.context, $($args),*);
39 struct LateContextAndPass<'a, 'tcx, T: LateLintPass<'a, 'tcx>> {
40 context: LateContext<'a, 'tcx>,
44 impl<'a, 'tcx, T: LateLintPass<'a, 'tcx>> LateContextAndPass<'a, 'tcx, T> {
45 /// Merge the lints specified by any lint attributes into the
46 /// current lint context, call the provided function, then reset the
47 /// lints in effect to their previous state.
48 fn with_lint_attrs<F>(&mut self, id: hir::HirId, attrs: &'tcx [ast::Attribute], f: F)
52 let prev = self.context.last_node_with_lint_attrs;
53 self.context.last_node_with_lint_attrs = id;
54 self.enter_attrs(attrs);
56 self.exit_attrs(attrs);
57 self.context.last_node_with_lint_attrs = prev;
60 fn with_param_env<F>(&mut self, id: hir::HirId, f: F)
64 let old_param_env = self.context.param_env;
65 self.context.param_env =
66 self.context.tcx.param_env(self.context.tcx.hir().local_def_id(id));
68 self.context.param_env = old_param_env;
71 fn process_mod(&mut self, m: &'tcx hir::Mod<'tcx>, s: Span, n: hir::HirId) {
72 lint_callback!(self, check_mod, m, s, n);
73 hir_visit::walk_mod(self, m, n);
74 lint_callback!(self, check_mod_post, m, s, n);
77 fn enter_attrs(&mut self, attrs: &'tcx [ast::Attribute]) {
78 debug!("late context: enter_attrs({:?})", attrs);
79 lint_callback!(self, enter_lint_attrs, attrs);
82 fn exit_attrs(&mut self, attrs: &'tcx [ast::Attribute]) {
83 debug!("late context: exit_attrs({:?})", attrs);
84 lint_callback!(self, exit_lint_attrs, attrs);
88 impl<'a, 'tcx, T: LateLintPass<'a, 'tcx>> hir_visit::Visitor<'tcx>
89 for LateContextAndPass<'a, 'tcx, T>
91 /// Because lints are scoped lexically, we want to walk nested
92 /// items in the context of the outer item, so enable
94 fn nested_visit_map<'this>(&'this mut self) -> hir_visit::NestedVisitorMap<'this, 'tcx> {
95 hir_visit::NestedVisitorMap::All(&self.context.tcx.hir())
98 fn visit_nested_body(&mut self, body: hir::BodyId) {
99 let old_tables = self.context.tables;
100 self.context.tables = self.context.tcx.body_tables(body);
101 let body = self.context.tcx.hir().body(body);
102 self.visit_body(body);
103 self.context.tables = old_tables;
106 fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
107 self.with_lint_attrs(param.hir_id, ¶m.attrs, |cx| {
108 lint_callback!(cx, check_param, param);
109 hir_visit::walk_param(cx, param);
113 fn visit_body(&mut self, body: &'tcx hir::Body<'tcx>) {
114 lint_callback!(self, check_body, body);
115 hir_visit::walk_body(self, body);
116 lint_callback!(self, check_body_post, body);
119 fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) {
120 let generics = self.context.generics.take();
121 self.context.generics = it.kind.generics();
122 self.with_lint_attrs(it.hir_id, &it.attrs, |cx| {
123 cx.with_param_env(it.hir_id, |cx| {
124 lint_callback!(cx, check_item, it);
125 hir_visit::walk_item(cx, it);
126 lint_callback!(cx, check_item_post, it);
129 self.context.generics = generics;
132 fn visit_foreign_item(&mut self, it: &'tcx hir::ForeignItem<'tcx>) {
133 self.with_lint_attrs(it.hir_id, &it.attrs, |cx| {
134 cx.with_param_env(it.hir_id, |cx| {
135 lint_callback!(cx, check_foreign_item, it);
136 hir_visit::walk_foreign_item(cx, it);
137 lint_callback!(cx, check_foreign_item_post, it);
142 fn visit_pat(&mut self, p: &'tcx hir::Pat<'tcx>) {
143 lint_callback!(self, check_pat, p);
144 hir_visit::walk_pat(self, p);
147 fn visit_expr(&mut self, e: &'tcx hir::Expr<'tcx>) {
148 self.with_lint_attrs(e.hir_id, &e.attrs, |cx| {
149 lint_callback!(cx, check_expr, e);
150 hir_visit::walk_expr(cx, e);
151 lint_callback!(cx, check_expr_post, e);
155 fn visit_stmt(&mut self, s: &'tcx hir::Stmt<'tcx>) {
156 // statement attributes are actually just attributes on one of
160 // so we keep track of lint levels there
161 lint_callback!(self, check_stmt, s);
162 hir_visit::walk_stmt(self, s);
167 fk: hir_visit::FnKind<'tcx>,
168 decl: &'tcx hir::FnDecl<'tcx>,
169 body_id: hir::BodyId,
173 // Wrap in tables here, not just in visit_nested_body,
174 // in order for `check_fn` to be able to use them.
175 let old_tables = self.context.tables;
176 self.context.tables = self.context.tcx.body_tables(body_id);
177 let body = self.context.tcx.hir().body(body_id);
178 lint_callback!(self, check_fn, fk, decl, body, span, id);
179 hir_visit::walk_fn(self, fk, decl, body_id, span, id);
180 lint_callback!(self, check_fn_post, fk, decl, body, span, id);
181 self.context.tables = old_tables;
184 fn visit_variant_data(
186 s: &'tcx hir::VariantData<'tcx>,
188 _: &'tcx hir::Generics<'tcx>,
192 lint_callback!(self, check_struct_def, s);
193 hir_visit::walk_struct_def(self, s);
194 lint_callback!(self, check_struct_def_post, s);
197 fn visit_struct_field(&mut self, s: &'tcx hir::StructField<'tcx>) {
198 self.with_lint_attrs(s.hir_id, &s.attrs, |cx| {
199 lint_callback!(cx, check_struct_field, s);
200 hir_visit::walk_struct_field(cx, s);
206 v: &'tcx hir::Variant<'tcx>,
207 g: &'tcx hir::Generics<'tcx>,
210 self.with_lint_attrs(v.id, &v.attrs, |cx| {
211 lint_callback!(cx, check_variant, v);
212 hir_visit::walk_variant(cx, v, g, item_id);
213 lint_callback!(cx, check_variant_post, v);
217 fn visit_ty(&mut self, t: &'tcx hir::Ty<'tcx>) {
218 lint_callback!(self, check_ty, t);
219 hir_visit::walk_ty(self, t);
222 fn visit_name(&mut self, sp: Span, name: ast::Name) {
223 lint_callback!(self, check_name, sp, name);
226 fn visit_mod(&mut self, m: &'tcx hir::Mod<'tcx>, s: Span, n: hir::HirId) {
227 if !self.context.only_module {
228 self.process_mod(m, s, n);
232 fn visit_local(&mut self, l: &'tcx hir::Local<'tcx>) {
233 self.with_lint_attrs(l.hir_id, &l.attrs, |cx| {
234 lint_callback!(cx, check_local, l);
235 hir_visit::walk_local(cx, l);
239 fn visit_block(&mut self, b: &'tcx hir::Block<'tcx>) {
240 lint_callback!(self, check_block, b);
241 hir_visit::walk_block(self, b);
242 lint_callback!(self, check_block_post, b);
245 fn visit_arm(&mut self, a: &'tcx hir::Arm<'tcx>) {
246 lint_callback!(self, check_arm, a);
247 hir_visit::walk_arm(self, a);
250 fn visit_generic_param(&mut self, p: &'tcx hir::GenericParam<'tcx>) {
251 lint_callback!(self, check_generic_param, p);
252 hir_visit::walk_generic_param(self, p);
255 fn visit_generics(&mut self, g: &'tcx hir::Generics<'tcx>) {
256 lint_callback!(self, check_generics, g);
257 hir_visit::walk_generics(self, g);
260 fn visit_where_predicate(&mut self, p: &'tcx hir::WherePredicate<'tcx>) {
261 lint_callback!(self, check_where_predicate, p);
262 hir_visit::walk_where_predicate(self, p);
265 fn visit_poly_trait_ref(
267 t: &'tcx hir::PolyTraitRef<'tcx>,
268 m: hir::TraitBoundModifier,
270 lint_callback!(self, check_poly_trait_ref, t, m);
271 hir_visit::walk_poly_trait_ref(self, t, m);
274 fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
275 let generics = self.context.generics.take();
276 self.context.generics = Some(&trait_item.generics);
277 self.with_lint_attrs(trait_item.hir_id, &trait_item.attrs, |cx| {
278 cx.with_param_env(trait_item.hir_id, |cx| {
279 lint_callback!(cx, check_trait_item, trait_item);
280 hir_visit::walk_trait_item(cx, trait_item);
281 lint_callback!(cx, check_trait_item_post, trait_item);
284 self.context.generics = generics;
287 fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
288 let generics = self.context.generics.take();
289 self.context.generics = Some(&impl_item.generics);
290 self.with_lint_attrs(impl_item.hir_id, &impl_item.attrs, |cx| {
291 cx.with_param_env(impl_item.hir_id, |cx| {
292 lint_callback!(cx, check_impl_item, impl_item);
293 hir_visit::walk_impl_item(cx, impl_item);
294 lint_callback!(cx, check_impl_item_post, impl_item);
297 self.context.generics = generics;
300 fn visit_lifetime(&mut self, lt: &'tcx hir::Lifetime) {
301 lint_callback!(self, check_lifetime, lt);
302 hir_visit::walk_lifetime(self, lt);
305 fn visit_path(&mut self, p: &'tcx hir::Path<'tcx>, id: hir::HirId) {
306 lint_callback!(self, check_path, p, id);
307 hir_visit::walk_path(self, p);
310 fn visit_attribute(&mut self, attr: &'tcx ast::Attribute) {
311 lint_callback!(self, check_attribute, attr);
315 struct LateLintPassObjects<'a> {
316 lints: &'a mut [LateLintPassObject],
319 #[allow(rustc::lint_pass_impl_without_macro)]
320 impl LintPass for LateLintPassObjects<'_> {
321 fn name(&self) -> &'static str {
326 macro_rules! expand_late_lint_pass_impl_methods {
327 ([$a:tt, $hir:tt], [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
328 $(fn $name(&mut self, context: &LateContext<$a, $hir>, $($param: $arg),*) {
329 for obj in self.lints.iter_mut() {
330 obj.$name(context, $($param),*);
336 macro_rules! late_lint_pass_impl {
337 ([], [$hir:tt], $methods:tt) => (
338 impl<'a, $hir> LateLintPass<'a, $hir> for LateLintPassObjects<'_> {
339 expand_late_lint_pass_impl_methods!(['a, $hir], $methods);
344 late_lint_methods!(late_lint_pass_impl, [], ['tcx]);
346 fn late_lint_mod_pass<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(
348 module_def_id: DefId,
351 let access_levels = &tcx.privacy_access_levels(LOCAL_CRATE);
353 let context = LateContext {
355 tables: &ty::TypeckTables::empty(None),
356 param_env: ty::ParamEnv::empty(),
358 lint_store: &tcx.lint_store,
359 last_node_with_lint_attrs: tcx.hir().as_local_hir_id(module_def_id).unwrap(),
364 let mut cx = LateContextAndPass { context, pass };
366 let (module, span, hir_id) = tcx.hir().get_module(module_def_id);
367 cx.process_mod(module, span, hir_id);
369 // Visit the crate attributes
370 if hir_id == hir::CRATE_HIR_ID {
371 walk_list!(cx, visit_attribute, tcx.hir().attrs(hir::CRATE_HIR_ID));
375 pub fn late_lint_mod<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(
377 module_def_id: DefId,
380 if tcx.sess.opts.debugging_opts.no_interleave_lints {
381 // These passes runs in late_lint_crate with -Z no_interleave_lints
385 late_lint_mod_pass(tcx, module_def_id, builtin_lints);
387 let mut passes: Vec<_> =
388 tcx.lint_store.late_module_passes.iter().map(|pass| (pass)()).collect();
390 if !passes.is_empty() {
391 late_lint_mod_pass(tcx, module_def_id, LateLintPassObjects { lints: &mut passes[..] });
395 fn late_lint_pass_crate<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(tcx: TyCtxt<'tcx>, pass: T) {
396 let access_levels = &tcx.privacy_access_levels(LOCAL_CRATE);
398 let krate = tcx.hir().krate();
400 let context = LateContext {
402 tables: &ty::TypeckTables::empty(None),
403 param_env: ty::ParamEnv::empty(),
405 lint_store: &tcx.lint_store,
406 last_node_with_lint_attrs: hir::CRATE_HIR_ID,
411 let mut cx = LateContextAndPass { context, pass };
413 // Visit the whole crate.
414 cx.with_lint_attrs(hir::CRATE_HIR_ID, &krate.attrs, |cx| {
415 // since the root module isn't visited as an item (because it isn't an
416 // item), warn for it here.
417 lint_callback!(cx, check_crate, krate);
419 hir_visit::walk_crate(cx, krate);
421 lint_callback!(cx, check_crate_post, krate);
425 fn late_lint_crate<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(tcx: TyCtxt<'tcx>, builtin_lints: T) {
426 let mut passes = tcx.lint_store.late_passes.iter().map(|p| (p)()).collect::<Vec<_>>();
428 if !tcx.sess.opts.debugging_opts.no_interleave_lints {
429 if !passes.is_empty() {
430 late_lint_pass_crate(tcx, LateLintPassObjects { lints: &mut passes[..] });
433 late_lint_pass_crate(tcx, builtin_lints);
435 for pass in &mut passes {
436 time(tcx.sess, &format!("running late lint: {}", pass.name()), || {
437 late_lint_pass_crate(tcx, LateLintPassObjects { lints: slice::from_mut(pass) });
441 let mut passes: Vec<_> =
442 tcx.lint_store.late_module_passes.iter().map(|pass| (pass)()).collect();
444 for pass in &mut passes {
445 time(tcx.sess, &format!("running late module lint: {}", pass.name()), || {
446 late_lint_pass_crate(tcx, LateLintPassObjects { lints: slice::from_mut(pass) });
452 /// Performs lint checking on a crate.
453 pub fn check_crate<'tcx, T: for<'a> LateLintPass<'a, 'tcx>>(
455 builtin_lints: impl FnOnce() -> T + Send,
459 time(tcx.sess, "crate lints", || {
460 // Run whole crate non-incremental lints
461 late_lint_crate(tcx, builtin_lints());
465 time(tcx.sess, "module lints", || {
466 // Run per-module lints
467 par_iter(&tcx.hir().krate().modules).for_each(|(&module, _)| {
468 tcx.ensure().lint_mod(tcx.hir().local_def_id(module));