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};
20 use rustc_ast::visit as ast_visit;
21 use rustc_ast::AstLike;
22 use rustc_session::lint::{BufferedEarlyLint, LintBuffer, LintPass};
23 use rustc_session::Session;
24 use rustc_span::symbol::Ident;
30 macro_rules! run_early_pass { ($cx:expr, $f:ident, $($args:expr),*) => ({
31 $cx.pass.$f(&$cx.context, $($args),*);
34 struct EarlyContextAndPass<'a, T: EarlyLintPass> {
35 context: EarlyContext<'a>,
39 impl<'a, T: EarlyLintPass> EarlyContextAndPass<'a, T> {
40 fn check_id(&mut self, id: ast::NodeId) {
41 for early_lint in self.context.buffered.take(id) {
42 let BufferedEarlyLint { span, msg, node_id: _, lint_id, diagnostic } = early_lint;
43 self.context.lookup_with_diagnostics(
46 |lint| lint.build(&msg).emit(),
52 /// Merge the lints specified by any lint attributes into the
53 /// current lint context, call the provided function, then reset the
54 /// lints in effect to their previous state.
55 fn with_lint_attrs<F>(&mut self, id: ast::NodeId, attrs: &'a [ast::Attribute], f: F)
59 let is_crate_node = id == ast::CRATE_NODE_ID;
60 let push = self.context.builder.push(attrs, &self.context.lint_store, is_crate_node);
62 self.enter_attrs(attrs);
64 self.exit_attrs(attrs);
65 self.context.builder.pop(push);
68 fn enter_attrs(&mut self, attrs: &'a [ast::Attribute]) {
69 debug!("early context: enter_attrs({:?})", attrs);
70 run_early_pass!(self, enter_lint_attrs, attrs);
73 fn exit_attrs(&mut self, attrs: &'a [ast::Attribute]) {
74 debug!("early context: exit_attrs({:?})", attrs);
75 run_early_pass!(self, exit_lint_attrs, attrs);
79 impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T> {
80 fn visit_param(&mut self, param: &'a ast::Param) {
81 self.with_lint_attrs(param.id, ¶m.attrs, |cx| {
82 run_early_pass!(cx, check_param, param);
83 ast_visit::walk_param(cx, param);
87 fn visit_item(&mut self, it: &'a ast::Item) {
88 self.with_lint_attrs(it.id, &it.attrs, |cx| {
89 run_early_pass!(cx, check_item, it);
90 ast_visit::walk_item(cx, it);
91 run_early_pass!(cx, check_item_post, it);
95 fn visit_foreign_item(&mut self, it: &'a ast::ForeignItem) {
96 self.with_lint_attrs(it.id, &it.attrs, |cx| {
97 run_early_pass!(cx, check_foreign_item, it);
98 ast_visit::walk_foreign_item(cx, it);
99 run_early_pass!(cx, check_foreign_item_post, it);
103 fn visit_pat(&mut self, p: &'a ast::Pat) {
104 run_early_pass!(self, check_pat, p);
106 ast_visit::walk_pat(self, p);
107 run_early_pass!(self, check_pat_post, p);
110 fn visit_anon_const(&mut self, c: &'a ast::AnonConst) {
111 run_early_pass!(self, check_anon_const, c);
113 ast_visit::walk_anon_const(self, c);
116 fn visit_expr(&mut self, e: &'a ast::Expr) {
117 self.with_lint_attrs(e.id, &e.attrs, |cx| {
118 run_early_pass!(cx, check_expr, e);
119 ast_visit::walk_expr(cx, e);
123 fn visit_stmt(&mut self, s: &'a ast::Stmt) {
124 // Add the statement's lint attributes to our
125 // current state when checking the statement itself.
126 // This allows us to handle attributes like
127 // `#[allow(unused_doc_comments)]`, which apply to
128 // sibling attributes on the same target
130 // Note that statements get their attributes from
131 // the AST struct that they wrap (e.g. an item)
132 self.with_lint_attrs(s.id, s.attrs(), |cx| {
133 run_early_pass!(cx, check_stmt, s);
136 // The visitor for the AST struct wrapped
137 // by the statement (e.g. `Item`) will call
138 // `with_lint_attrs`, so do this walk
139 // outside of the above `with_lint_attrs` call
140 ast_visit::walk_stmt(self, s);
143 fn visit_fn(&mut self, fk: ast_visit::FnKind<'a>, span: Span, id: ast::NodeId) {
144 run_early_pass!(self, check_fn, fk, span, id);
146 ast_visit::walk_fn(self, fk, span);
148 // Explicitly check for lints associated with 'closure_id', since
149 // it does not have a corresponding AST node
150 if let ast_visit::FnKind::Fn(_, _, sig, _, _) = fk {
151 if let ast::Async::Yes { closure_id, .. } = sig.header.asyncness {
152 self.check_id(closure_id);
155 run_early_pass!(self, check_fn_post, fk, span, id);
158 fn visit_variant_data(&mut self, s: &'a ast::VariantData) {
159 run_early_pass!(self, check_struct_def, s);
160 if let Some(ctor_hir_id) = s.ctor_id() {
161 self.check_id(ctor_hir_id);
163 ast_visit::walk_struct_def(self, s);
164 run_early_pass!(self, check_struct_def_post, s);
167 fn visit_field_def(&mut self, s: &'a ast::FieldDef) {
168 self.with_lint_attrs(s.id, &s.attrs, |cx| {
169 run_early_pass!(cx, check_field_def, s);
170 ast_visit::walk_field_def(cx, s);
174 fn visit_variant(&mut self, v: &'a ast::Variant) {
175 self.with_lint_attrs(v.id, &v.attrs, |cx| {
176 run_early_pass!(cx, check_variant, v);
177 ast_visit::walk_variant(cx, v);
178 run_early_pass!(cx, check_variant_post, v);
182 fn visit_ty(&mut self, t: &'a ast::Ty) {
183 run_early_pass!(self, check_ty, t);
185 ast_visit::walk_ty(self, t);
188 fn visit_ident(&mut self, ident: Ident) {
189 run_early_pass!(self, check_ident, ident);
192 fn visit_local(&mut self, l: &'a ast::Local) {
193 self.with_lint_attrs(l.id, &l.attrs, |cx| {
194 run_early_pass!(cx, check_local, l);
195 ast_visit::walk_local(cx, l);
199 fn visit_block(&mut self, b: &'a ast::Block) {
200 run_early_pass!(self, check_block, b);
202 ast_visit::walk_block(self, b);
203 run_early_pass!(self, check_block_post, b);
206 fn visit_arm(&mut self, a: &'a ast::Arm) {
207 run_early_pass!(self, check_arm, a);
208 ast_visit::walk_arm(self, a);
211 fn visit_expr_post(&mut self, e: &'a ast::Expr) {
212 run_early_pass!(self, check_expr_post, e);
214 // Explicitly check for lints associated with 'closure_id', since
215 // it does not have a corresponding AST node
217 ast::ExprKind::Closure(_, ast::Async::Yes { closure_id, .. }, ..)
218 | ast::ExprKind::Async(_, closure_id, ..) => self.check_id(closure_id),
223 fn visit_generic_arg(&mut self, arg: &'a ast::GenericArg) {
224 run_early_pass!(self, check_generic_arg, arg);
225 ast_visit::walk_generic_arg(self, arg);
228 fn visit_generic_param(&mut self, param: &'a ast::GenericParam) {
229 run_early_pass!(self, check_generic_param, param);
230 ast_visit::walk_generic_param(self, param);
233 fn visit_generics(&mut self, g: &'a ast::Generics) {
234 run_early_pass!(self, check_generics, g);
235 ast_visit::walk_generics(self, g);
238 fn visit_where_predicate(&mut self, p: &'a ast::WherePredicate) {
239 run_early_pass!(self, check_where_predicate, p);
240 ast_visit::walk_where_predicate(self, p);
243 fn visit_poly_trait_ref(&mut self, t: &'a ast::PolyTraitRef, m: &'a ast::TraitBoundModifier) {
244 run_early_pass!(self, check_poly_trait_ref, t, m);
245 ast_visit::walk_poly_trait_ref(self, t, m);
248 fn visit_assoc_item(&mut self, item: &'a ast::AssocItem, ctxt: ast_visit::AssocCtxt) {
249 self.with_lint_attrs(item.id, &item.attrs, |cx| match ctxt {
250 ast_visit::AssocCtxt::Trait => {
251 run_early_pass!(cx, check_trait_item, item);
252 ast_visit::walk_assoc_item(cx, item, ctxt);
253 run_early_pass!(cx, check_trait_item_post, item);
255 ast_visit::AssocCtxt::Impl => {
256 run_early_pass!(cx, check_impl_item, item);
257 ast_visit::walk_assoc_item(cx, item, ctxt);
258 run_early_pass!(cx, check_impl_item_post, item);
263 fn visit_lifetime(&mut self, lt: &'a ast::Lifetime) {
264 run_early_pass!(self, check_lifetime, lt);
265 self.check_id(lt.id);
268 fn visit_path(&mut self, p: &'a ast::Path, id: ast::NodeId) {
269 run_early_pass!(self, check_path, p, id);
271 ast_visit::walk_path(self, p);
274 fn visit_attribute(&mut self, attr: &'a ast::Attribute) {
275 run_early_pass!(self, check_attribute, attr);
278 fn visit_mac_def(&mut self, mac: &'a ast::MacroDef, id: ast::NodeId) {
279 run_early_pass!(self, check_mac_def, mac, id);
283 fn visit_mac_call(&mut self, mac: &'a ast::MacCall) {
284 run_early_pass!(self, check_mac, mac);
285 ast_visit::walk_mac(self, mac);
289 struct EarlyLintPassObjects<'a> {
290 lints: &'a mut [EarlyLintPassObject],
293 #[allow(rustc::lint_pass_impl_without_macro)]
294 impl LintPass for EarlyLintPassObjects<'_> {
295 fn name(&self) -> &'static str {
300 macro_rules! expand_early_lint_pass_impl_methods {
301 ([$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
302 $(fn $name(&mut self, context: &EarlyContext<'_>, $($param: $arg),*) {
303 for obj in self.lints.iter_mut() {
304 obj.$name(context, $($param),*);
310 macro_rules! early_lint_pass_impl {
311 ([], [$($methods:tt)*]) => (
312 impl EarlyLintPass for EarlyLintPassObjects<'_> {
313 expand_early_lint_pass_impl_methods!([$($methods)*]);
318 crate::early_lint_methods!(early_lint_pass_impl, []);
320 fn early_lint_crate<T: EarlyLintPass>(
322 lint_store: &LintStore,
325 buffered: LintBuffer,
326 warn_about_weird_lints: bool,
328 let mut cx = EarlyContextAndPass {
329 context: EarlyContext::new(sess, lint_store, krate, buffered, warn_about_weird_lints),
333 // Visit the whole crate.
334 cx.with_lint_attrs(ast::CRATE_NODE_ID, &krate.attrs, |cx| {
335 // since the root module isn't visited as an item (because it isn't an
336 // item), warn for it here.
337 run_early_pass!(cx, check_crate, krate);
339 ast_visit::walk_crate(cx, krate);
341 run_early_pass!(cx, check_crate_post, krate);
346 pub fn check_ast_crate<T: EarlyLintPass>(
348 lint_store: &LintStore,
351 lint_buffer: Option<LintBuffer>,
355 if pre_expansion { &lint_store.pre_expansion_passes } else { &lint_store.early_passes };
356 let mut passes: Vec<_> = passes.iter().map(|p| (p)()).collect();
357 let mut buffered = lint_buffer.unwrap_or_default();
359 if !sess.opts.debugging_opts.no_interleave_lints {
361 early_lint_crate(sess, lint_store, krate, builtin_lints, buffered, pre_expansion);
363 if !passes.is_empty() {
364 buffered = early_lint_crate(
368 EarlyLintPassObjects { lints: &mut passes[..] },
374 for (i, pass) in passes.iter_mut().enumerate() {
376 sess.prof.extra_verbose_generic_activity("run_lint", pass.name()).run(|| {
381 EarlyLintPassObjects { lints: slice::from_mut(pass) },
383 pre_expansion && i == 0,
389 // All of the buffered lints should have been emitted at this point.
390 // If not, that means that we somehow buffered a lint for a node id
391 // that was not lint-checked (perhaps it doesn't exist?). This is a bug.
392 for (_id, lints) in buffered.map {
393 for early_lint in lints {
394 sess.delay_span_bug(early_lint.span, "failed to process buffered lint here");