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.
17 use crate::context::{EarlyContext, LintContext, LintStore};
18 use crate::passes::{EarlyLintPass, EarlyLintPassObject};
19 use rustc_session::lint::{LintBuffer, LintPass};
20 use rustc_session::Session;
23 use syntax::visit as ast_visit;
28 macro_rules! run_early_pass { ($cx:expr, $f:ident, $($args:expr),*) => ({
29 $cx.pass.$f(&$cx.context, $($args),*);
32 struct EarlyContextAndPass<'a, T: EarlyLintPass> {
33 context: EarlyContext<'a>,
37 impl<'a, T: EarlyLintPass> EarlyContextAndPass<'a, T> {
38 fn check_id(&mut self, id: ast::NodeId) {
39 for early_lint in self.context.buffered.take(id) {
40 self.context.lookup_and_emit_with_diagnostics(
41 early_lint.lint_id.lint,
42 Some(early_lint.span.clone()),
44 early_lint.diagnostic,
49 /// Merge the lints specified by any lint attributes into the
50 /// current lint context, call the provided function, then reset the
51 /// lints in effect to their previous state.
52 fn with_lint_attrs<F>(&mut self, id: ast::NodeId, attrs: &'a [ast::Attribute], f: F)
56 let push = self.context.builder.push(attrs, &self.context.lint_store);
58 self.enter_attrs(attrs);
60 self.exit_attrs(attrs);
61 self.context.builder.pop(push);
64 fn enter_attrs(&mut self, attrs: &'a [ast::Attribute]) {
65 debug!("early context: enter_attrs({:?})", attrs);
66 run_early_pass!(self, enter_lint_attrs, attrs);
69 fn exit_attrs(&mut self, attrs: &'a [ast::Attribute]) {
70 debug!("early context: exit_attrs({:?})", attrs);
71 run_early_pass!(self, exit_lint_attrs, attrs);
75 impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T> {
76 fn visit_param(&mut self, param: &'a ast::Param) {
77 self.with_lint_attrs(param.id, ¶m.attrs, |cx| {
78 run_early_pass!(cx, check_param, param);
79 ast_visit::walk_param(cx, param);
83 fn visit_item(&mut self, it: &'a ast::Item) {
84 self.with_lint_attrs(it.id, &it.attrs, |cx| {
85 run_early_pass!(cx, check_item, it);
86 ast_visit::walk_item(cx, it);
87 run_early_pass!(cx, check_item_post, it);
91 fn visit_foreign_item(&mut self, it: &'a ast::ForeignItem) {
92 self.with_lint_attrs(it.id, &it.attrs, |cx| {
93 run_early_pass!(cx, check_foreign_item, it);
94 ast_visit::walk_foreign_item(cx, it);
95 run_early_pass!(cx, check_foreign_item_post, it);
99 fn visit_pat(&mut self, p: &'a ast::Pat) {
100 run_early_pass!(self, check_pat, p);
102 ast_visit::walk_pat(self, p);
103 run_early_pass!(self, check_pat_post, p);
106 fn visit_expr(&mut self, e: &'a ast::Expr) {
107 self.with_lint_attrs(e.id, &e.attrs, |cx| {
108 run_early_pass!(cx, check_expr, e);
109 ast_visit::walk_expr(cx, e);
113 fn visit_stmt(&mut self, s: &'a ast::Stmt) {
114 run_early_pass!(self, check_stmt, s);
116 ast_visit::walk_stmt(self, s);
119 fn visit_fn(&mut self, fk: ast_visit::FnKind<'a>, span: Span, id: ast::NodeId) {
120 run_early_pass!(self, check_fn, fk, span, id);
122 ast_visit::walk_fn(self, fk, span);
123 run_early_pass!(self, check_fn_post, fk, span, id);
126 fn visit_variant_data(&mut self, s: &'a ast::VariantData) {
127 run_early_pass!(self, check_struct_def, s);
128 if let Some(ctor_hir_id) = s.ctor_id() {
129 self.check_id(ctor_hir_id);
131 ast_visit::walk_struct_def(self, s);
132 run_early_pass!(self, check_struct_def_post, s);
135 fn visit_struct_field(&mut self, s: &'a ast::StructField) {
136 self.with_lint_attrs(s.id, &s.attrs, |cx| {
137 run_early_pass!(cx, check_struct_field, s);
138 ast_visit::walk_struct_field(cx, s);
142 fn visit_variant(&mut self, v: &'a ast::Variant) {
143 self.with_lint_attrs(v.id, &v.attrs, |cx| {
144 run_early_pass!(cx, check_variant, v);
145 ast_visit::walk_variant(cx, v);
146 run_early_pass!(cx, check_variant_post, v);
150 fn visit_ty(&mut self, t: &'a ast::Ty) {
151 run_early_pass!(self, check_ty, t);
153 ast_visit::walk_ty(self, t);
156 fn visit_ident(&mut self, ident: ast::Ident) {
157 run_early_pass!(self, check_ident, ident);
160 fn visit_mod(&mut self, m: &'a ast::Mod, s: Span, _a: &[ast::Attribute], n: ast::NodeId) {
161 run_early_pass!(self, check_mod, m, s, n);
163 ast_visit::walk_mod(self, m);
164 run_early_pass!(self, check_mod_post, m, s, n);
167 fn visit_local(&mut self, l: &'a ast::Local) {
168 self.with_lint_attrs(l.id, &l.attrs, |cx| {
169 run_early_pass!(cx, check_local, l);
170 ast_visit::walk_local(cx, l);
174 fn visit_block(&mut self, b: &'a ast::Block) {
175 run_early_pass!(self, check_block, b);
177 ast_visit::walk_block(self, b);
178 run_early_pass!(self, check_block_post, b);
181 fn visit_arm(&mut self, a: &'a ast::Arm) {
182 run_early_pass!(self, check_arm, a);
183 ast_visit::walk_arm(self, a);
186 fn visit_expr_post(&mut self, e: &'a ast::Expr) {
187 run_early_pass!(self, check_expr_post, e);
190 fn visit_generic_param(&mut self, param: &'a ast::GenericParam) {
191 run_early_pass!(self, check_generic_param, param);
192 ast_visit::walk_generic_param(self, param);
195 fn visit_generics(&mut self, g: &'a ast::Generics) {
196 run_early_pass!(self, check_generics, g);
197 ast_visit::walk_generics(self, g);
200 fn visit_where_predicate(&mut self, p: &'a ast::WherePredicate) {
201 run_early_pass!(self, check_where_predicate, p);
202 ast_visit::walk_where_predicate(self, p);
205 fn visit_poly_trait_ref(&mut self, t: &'a ast::PolyTraitRef, m: &'a ast::TraitBoundModifier) {
206 run_early_pass!(self, check_poly_trait_ref, t, m);
207 ast_visit::walk_poly_trait_ref(self, t, m);
210 fn visit_assoc_item(&mut self, item: &'a ast::AssocItem, ctxt: ast_visit::AssocCtxt) {
211 self.with_lint_attrs(item.id, &item.attrs, |cx| match ctxt {
212 ast_visit::AssocCtxt::Trait => {
213 run_early_pass!(cx, check_trait_item, item);
214 ast_visit::walk_assoc_item(cx, item, ctxt);
215 run_early_pass!(cx, check_trait_item_post, item);
217 ast_visit::AssocCtxt::Impl => {
218 run_early_pass!(cx, check_impl_item, item);
219 ast_visit::walk_assoc_item(cx, item, ctxt);
220 run_early_pass!(cx, check_impl_item_post, item);
225 fn visit_lifetime(&mut self, lt: &'a ast::Lifetime) {
226 run_early_pass!(self, check_lifetime, lt);
227 self.check_id(lt.id);
230 fn visit_path(&mut self, p: &'a ast::Path, id: ast::NodeId) {
231 run_early_pass!(self, check_path, p, id);
233 ast_visit::walk_path(self, p);
236 fn visit_attribute(&mut self, attr: &'a ast::Attribute) {
237 run_early_pass!(self, check_attribute, attr);
240 fn visit_mac_def(&mut self, mac: &'a ast::MacroDef, id: ast::NodeId) {
241 run_early_pass!(self, check_mac_def, mac, id);
245 fn visit_mac(&mut self, mac: &'a ast::Mac) {
246 // FIXME(#54110): So, this setup isn't really right. I think
247 // that (a) the libsyntax visitor ought to be doing this as
248 // part of `walk_mac`, and (b) we should be calling
249 // `visit_path`, *but* that would require a `NodeId`, and I
250 // want to get #53686 fixed quickly. -nmatsakis
251 ast_visit::walk_path(self, &mac.path);
253 run_early_pass!(self, check_mac, mac);
257 struct EarlyLintPassObjects<'a> {
258 lints: &'a mut [EarlyLintPassObject],
261 #[allow(rustc::lint_pass_impl_without_macro)]
262 impl LintPass for EarlyLintPassObjects<'_> {
263 fn name(&self) -> &'static str {
268 macro_rules! expand_early_lint_pass_impl_methods {
269 ([$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
270 $(fn $name(&mut self, context: &EarlyContext<'_>, $($param: $arg),*) {
271 for obj in self.lints.iter_mut() {
272 obj.$name(context, $($param),*);
278 macro_rules! early_lint_pass_impl {
279 ([], [$($methods:tt)*]) => (
280 impl EarlyLintPass for EarlyLintPassObjects<'_> {
281 expand_early_lint_pass_impl_methods!([$($methods)*]);
286 crate::early_lint_methods!(early_lint_pass_impl, []);
288 fn early_lint_crate<T: EarlyLintPass>(
290 lint_store: &LintStore,
293 buffered: LintBuffer,
294 warn_about_weird_lints: bool,
296 let mut cx = EarlyContextAndPass {
297 context: EarlyContext::new(sess, lint_store, krate, buffered, warn_about_weird_lints),
301 // Visit the whole crate.
302 cx.with_lint_attrs(ast::CRATE_NODE_ID, &krate.attrs, |cx| {
303 // since the root module isn't visited as an item (because it isn't an
304 // item), warn for it here.
305 run_early_pass!(cx, check_crate, krate);
307 ast_visit::walk_crate(cx, krate);
309 run_early_pass!(cx, check_crate_post, krate);
314 pub fn check_ast_crate<T: EarlyLintPass>(
316 lint_store: &LintStore,
319 lint_buffer: Option<LintBuffer>,
322 let mut passes: Vec<_> = if pre_expansion {
323 lint_store.pre_expansion_passes.iter().map(|p| (p)()).collect()
325 lint_store.early_passes.iter().map(|p| (p)()).collect()
327 let mut buffered = lint_buffer.unwrap_or_default();
329 if !sess.opts.debugging_opts.no_interleave_lints {
331 early_lint_crate(sess, lint_store, krate, builtin_lints, buffered, pre_expansion);
333 if !passes.is_empty() {
334 buffered = early_lint_crate(
338 EarlyLintPassObjects { lints: &mut passes[..] },
344 for pass in &mut passes {
346 sess.prof.extra_verbose_generic_activity("run_lint", pass.name()).run(|| {
351 EarlyLintPassObjects { lints: slice::from_mut(pass) },
359 // All of the buffered lints should have been emitted at this point.
360 // If not, that means that we somehow buffered a lint for a node id
361 // that was not lint-checked (perhaps it doesn't exist?). This is a bug.
363 // Rustdoc runs everybody-loops before the early lints and removes
364 // function bodies, so it's totally possible for linted
365 // node ids to not exist (e.g., macros defined within functions for the
366 // unused_macro lint) anymore. So we only run this check
367 // when we're not in rustdoc mode. (see issue #47639)
368 if !sess.opts.actually_rustdoc {
369 for (_id, lints) in buffered.map {
370 for early_lint in lints {
371 sess.delay_span_bug(early_lint.span, "failed to process buffered lint here");