2019-12-30 13:11:49 +00:00
|
|
|
//! Implementation of lint checking.
|
|
|
|
//!
|
|
|
|
//! The lint checking is mostly consolidated into one pass which runs
|
|
|
|
//! after all other analyses. Throughout compilation, lint warnings
|
|
|
|
//! can be added via the `add_lint` method on the Session structure. This
|
|
|
|
//! requires a span and an ID of the node that the lint is being added to. The
|
|
|
|
//! lint isn't actually emitted at that time because it is unknown what the
|
|
|
|
//! actual lint level at that location is.
|
|
|
|
//!
|
|
|
|
//! To actually emit lint warnings/errors, a separate pass is used.
|
|
|
|
//! A context keeps track of the current state of all lint levels.
|
|
|
|
//! Upon entering a node of the ast which can modify the lint settings, the
|
|
|
|
//! previous lint state is pushed onto a stack and the ast is then recursed
|
|
|
|
//! upon. As the ast is traversed, this keeps track of the current lint level
|
|
|
|
//! for all lint attributes.
|
|
|
|
|
2020-01-09 06:52:01 +00:00
|
|
|
use crate::context::{EarlyContext, LintContext, LintStore};
|
|
|
|
use crate::passes::{EarlyLintPass, EarlyLintPassObject};
|
2021-12-07 10:28:12 +00:00
|
|
|
use rustc_ast::ptr::P;
|
|
|
|
use rustc_ast::visit::{self as ast_visit, Visitor};
|
2022-05-01 17:58:24 +00:00
|
|
|
use rustc_ast::{self as ast, walk_list, HasAttrs};
|
2022-12-19 20:24:09 +00:00
|
|
|
use rustc_data_structures::stack::ensure_sufficient_stack;
|
2023-08-09 12:28:00 +00:00
|
|
|
use rustc_feature::Features;
|
2021-09-28 22:17:54 +00:00
|
|
|
use rustc_middle::ty::RegisteredTools;
|
2022-12-09 01:27:43 +00:00
|
|
|
use rustc_session::lint::{BufferedEarlyLint, LintBuffer, LintPass};
|
2020-01-05 08:40:16 +00:00
|
|
|
use rustc_session::Session;
|
2020-04-19 11:00:18 +00:00
|
|
|
use rustc_span::symbol::Ident;
|
2019-12-30 13:11:49 +00:00
|
|
|
use rustc_span::Span;
|
|
|
|
|
2022-12-09 00:40:39 +00:00
|
|
|
macro_rules! lint_callback { ($cx:expr, $f:ident, $($args:expr),*) => ({
|
2022-12-09 01:27:43 +00:00
|
|
|
$cx.pass.$f(&$cx.context, $($args),*);
|
2019-12-30 13:11:49 +00:00
|
|
|
}) }
|
|
|
|
|
2023-01-03 07:48:16 +00:00
|
|
|
/// Implements the AST traversal for early lint passes. `T` provides the
|
2022-12-09 01:27:43 +00:00
|
|
|
/// `check_*` methods.
|
|
|
|
pub struct EarlyContextAndPass<'a, T: EarlyLintPass> {
|
2019-12-30 13:11:49 +00:00
|
|
|
context: EarlyContext<'a>,
|
2022-12-09 01:27:43 +00:00
|
|
|
pass: T,
|
2019-12-30 13:11:49 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 01:27:43 +00:00
|
|
|
impl<'a, T: EarlyLintPass> EarlyContextAndPass<'a, T> {
|
2022-12-07 03:58:48 +00:00
|
|
|
// This always-inlined function is for the hot call site.
|
|
|
|
#[inline(always)]
|
2022-09-18 14:31:35 +00:00
|
|
|
#[allow(rustc::diagnostic_outside_of_impl)]
|
2022-12-07 03:58:48 +00:00
|
|
|
fn inlined_check_id(&mut self, id: ast::NodeId) {
|
2019-12-30 13:11:49 +00:00
|
|
|
for early_lint in self.context.buffered.take(id) {
|
2020-03-15 23:43:37 +00:00
|
|
|
let BufferedEarlyLint { span, msg, node_id: _, lint_id, diagnostic } = early_lint;
|
2020-02-02 09:41:14 +00:00
|
|
|
self.context.lookup_with_diagnostics(
|
2020-02-05 15:28:23 +00:00
|
|
|
lint_id.lint,
|
2020-02-02 09:41:14 +00:00
|
|
|
Some(span),
|
2022-09-16 07:01:02 +00:00
|
|
|
msg,
|
|
|
|
|lint| lint,
|
2020-02-02 09:41:14 +00:00
|
|
|
diagnostic,
|
2019-12-30 13:11:49 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-07 03:58:48 +00:00
|
|
|
// This non-inlined function is for the cold call sites.
|
|
|
|
fn check_id(&mut self, id: ast::NodeId) {
|
|
|
|
self.inlined_check_id(id)
|
|
|
|
}
|
|
|
|
|
2019-12-30 13:11:49 +00:00
|
|
|
/// Merge the lints specified by any lint attributes into the
|
|
|
|
/// current lint context, call the provided function, then reset the
|
|
|
|
/// lints in effect to their previous state.
|
|
|
|
fn with_lint_attrs<F>(&mut self, id: ast::NodeId, attrs: &'a [ast::Attribute], f: F)
|
|
|
|
where
|
|
|
|
F: FnOnce(&mut Self),
|
|
|
|
{
|
2020-06-13 01:58:24 +00:00
|
|
|
let is_crate_node = id == ast::CRATE_NODE_ID;
|
2022-07-22 16:48:36 +00:00
|
|
|
debug!(?id);
|
2021-11-20 19:45:27 +00:00
|
|
|
let push = self.context.builder.push(attrs, is_crate_node, None);
|
|
|
|
|
2022-12-07 03:58:48 +00:00
|
|
|
self.inlined_check_id(id);
|
2019-12-30 13:11:49 +00:00
|
|
|
debug!("early context: enter_attrs({:?})", attrs);
|
2022-12-09 00:40:39 +00:00
|
|
|
lint_callback!(self, enter_lint_attrs, attrs);
|
2022-12-19 20:24:09 +00:00
|
|
|
ensure_sufficient_stack(|| f(self));
|
2019-12-30 13:11:49 +00:00
|
|
|
debug!("early context: exit_attrs({:?})", attrs);
|
2022-12-09 00:40:39 +00:00
|
|
|
lint_callback!(self, exit_lint_attrs, attrs);
|
2022-06-15 23:47:07 +00:00
|
|
|
self.context.builder.pop(push);
|
2019-12-30 13:11:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-09 01:27:43 +00:00
|
|
|
impl<'a, T: EarlyLintPass> ast_visit::Visitor<'a> for EarlyContextAndPass<'a, T> {
|
2019-12-30 13:11:49 +00:00
|
|
|
fn visit_param(&mut self, param: &'a ast::Param) {
|
|
|
|
self.with_lint_attrs(param.id, ¶m.attrs, |cx| {
|
2022-12-09 00:40:39 +00:00
|
|
|
lint_callback!(cx, check_param, param);
|
2019-12-30 13:11:49 +00:00
|
|
|
ast_visit::walk_param(cx, param);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_item(&mut self, it: &'a ast::Item) {
|
|
|
|
self.with_lint_attrs(it.id, &it.attrs, |cx| {
|
2022-12-09 00:40:39 +00:00
|
|
|
lint_callback!(cx, check_item, it);
|
2019-12-30 13:11:49 +00:00
|
|
|
ast_visit::walk_item(cx, it);
|
2022-12-09 00:40:39 +00:00
|
|
|
lint_callback!(cx, check_item_post, it);
|
2019-12-30 13:11:49 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_foreign_item(&mut self, it: &'a ast::ForeignItem) {
|
|
|
|
self.with_lint_attrs(it.id, &it.attrs, |cx| {
|
|
|
|
ast_visit::walk_foreign_item(cx, it);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_pat(&mut self, p: &'a ast::Pat) {
|
2022-12-09 00:40:39 +00:00
|
|
|
lint_callback!(self, check_pat, p);
|
2019-12-30 13:11:49 +00:00
|
|
|
self.check_id(p.id);
|
|
|
|
ast_visit::walk_pat(self, p);
|
2022-12-09 00:40:39 +00:00
|
|
|
lint_callback!(self, check_pat_post, p);
|
2019-12-30 13:11:49 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 01:27:21 +00:00
|
|
|
fn visit_pat_field(&mut self, field: &'a ast::PatField) {
|
|
|
|
self.with_lint_attrs(field.id, &field.attrs, |cx| {
|
|
|
|
ast_visit::walk_pat_field(cx, field);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-27 20:53:07 +00:00
|
|
|
fn visit_anon_const(&mut self, c: &'a ast::AnonConst) {
|
2021-04-15 15:11:44 +00:00
|
|
|
self.check_id(c.id);
|
2020-03-27 20:53:07 +00:00
|
|
|
ast_visit::walk_anon_const(self, c);
|
|
|
|
}
|
|
|
|
|
2019-12-30 13:11:49 +00:00
|
|
|
fn visit_expr(&mut self, e: &'a ast::Expr) {
|
|
|
|
self.with_lint_attrs(e.id, &e.attrs, |cx| {
|
2022-12-09 00:40:39 +00:00
|
|
|
lint_callback!(cx, check_expr, e);
|
2019-12-30 13:11:49 +00:00
|
|
|
ast_visit::walk_expr(cx, e);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-07-15 01:07:56 +00:00
|
|
|
fn visit_expr_field(&mut self, f: &'a ast::ExprField) {
|
|
|
|
self.with_lint_attrs(f.id, &f.attrs, |cx| {
|
|
|
|
ast_visit::walk_expr_field(cx, f);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-12-30 13:11:49 +00:00
|
|
|
fn visit_stmt(&mut self, s: &'a ast::Stmt) {
|
Fix inconsistencies in handling of inert attributes on statements
When the 'early' and 'late' visitors visit an attribute target, they
activate any lint attributes (e.g. `#[allow]`) that apply to it.
This can affect warnings emitted on sibiling attributes. For example,
the following code does not produce an `unused_attributes` for
`#[inline]`, since the sibiling `#[allow(unused_attributes)]` suppressed
the warning.
```rust
trait Foo {
#[allow(unused_attributes)] #[inline] fn first();
#[inline] #[allow(unused_attributes)] fn second();
}
```
However, we do not do this for statements - instead, the lint attributes
only become active when we visit the struct nested inside `StmtKind`
(e.g. `Item`).
Currently, this is difficult to observe due to another issue - the
`HasAttrs` impl for `StmtKind` ignores attributes for `StmtKind::Item`.
As a result, the `unused_doc_comments` lint will never see attributes on
item statements.
This commit makes two interrelated fixes to the handling of inert
(non-proc-macro) attributes on statements:
* The `HasAttr` impl for `StmtKind` now returns attributes for
`StmtKind::Item`, treating it just like every other `StmtKind`
variant. The only place relying on the old behavior was macro
which has been updated to explicitly ignore attributes on item
statements. This allows the `unused_doc_comments` lint to fire for
item statements.
* The `early` and `late` lint visitors now activate lint attributes when
invoking the callback for `Stmt`. This ensures that a lint
attribute (e.g. `#[allow(unused_doc_comments)]`) can be applied to
sibiling attributes on an item statement.
For now, the `unused_doc_comments` lint is explicitly disabled on item
statements, which preserves the current behavior. The exact locatiosn
where this lint should fire are being discussed in PR #78306
2020-10-23 22:17:00 +00:00
|
|
|
// Add the statement's lint attributes to our
|
|
|
|
// current state when checking the statement itself.
|
|
|
|
// This allows us to handle attributes like
|
|
|
|
// `#[allow(unused_doc_comments)]`, which apply to
|
|
|
|
// sibling attributes on the same target
|
|
|
|
//
|
|
|
|
// Note that statements get their attributes from
|
|
|
|
// the AST struct that they wrap (e.g. an item)
|
|
|
|
self.with_lint_attrs(s.id, s.attrs(), |cx| {
|
2022-12-09 00:40:39 +00:00
|
|
|
lint_callback!(cx, check_stmt, s);
|
Fix inconsistencies in handling of inert attributes on statements
When the 'early' and 'late' visitors visit an attribute target, they
activate any lint attributes (e.g. `#[allow]`) that apply to it.
This can affect warnings emitted on sibiling attributes. For example,
the following code does not produce an `unused_attributes` for
`#[inline]`, since the sibiling `#[allow(unused_attributes)]` suppressed
the warning.
```rust
trait Foo {
#[allow(unused_attributes)] #[inline] fn first();
#[inline] #[allow(unused_attributes)] fn second();
}
```
However, we do not do this for statements - instead, the lint attributes
only become active when we visit the struct nested inside `StmtKind`
(e.g. `Item`).
Currently, this is difficult to observe due to another issue - the
`HasAttrs` impl for `StmtKind` ignores attributes for `StmtKind::Item`.
As a result, the `unused_doc_comments` lint will never see attributes on
item statements.
This commit makes two interrelated fixes to the handling of inert
(non-proc-macro) attributes on statements:
* The `HasAttr` impl for `StmtKind` now returns attributes for
`StmtKind::Item`, treating it just like every other `StmtKind`
variant. The only place relying on the old behavior was macro
which has been updated to explicitly ignore attributes on item
statements. This allows the `unused_doc_comments` lint to fire for
item statements.
* The `early` and `late` lint visitors now activate lint attributes when
invoking the callback for `Stmt`. This ensures that a lint
attribute (e.g. `#[allow(unused_doc_comments)]`) can be applied to
sibiling attributes on an item statement.
For now, the `unused_doc_comments` lint is explicitly disabled on item
statements, which preserves the current behavior. The exact locatiosn
where this lint should fire are being discussed in PR #78306
2020-10-23 22:17:00 +00:00
|
|
|
cx.check_id(s.id);
|
|
|
|
});
|
|
|
|
// The visitor for the AST struct wrapped
|
|
|
|
// by the statement (e.g. `Item`) will call
|
|
|
|
// `with_lint_attrs`, so do this walk
|
|
|
|
// outside of the above `with_lint_attrs` call
|
2019-12-30 13:11:49 +00:00
|
|
|
ast_visit::walk_stmt(self, s);
|
|
|
|
}
|
|
|
|
|
2020-01-29 23:18:54 +00:00
|
|
|
fn visit_fn(&mut self, fk: ast_visit::FnKind<'a>, span: Span, id: ast::NodeId) {
|
2022-12-09 00:40:39 +00:00
|
|
|
lint_callback!(self, check_fn, fk, span, id);
|
2019-12-30 13:11:49 +00:00
|
|
|
self.check_id(id);
|
2022-09-12 03:13:22 +00:00
|
|
|
ast_visit::walk_fn(self, fk);
|
2021-01-30 00:03:20 +00:00
|
|
|
|
|
|
|
// Explicitly check for lints associated with 'closure_id', since
|
|
|
|
// it does not have a corresponding AST node
|
2021-11-19 21:03:43 +00:00
|
|
|
if let ast_visit::FnKind::Fn(_, _, sig, _, _, _) = fk {
|
2021-01-30 00:03:20 +00:00
|
|
|
if let ast::Async::Yes { closure_id, .. } = sig.header.asyncness {
|
|
|
|
self.check_id(closure_id);
|
|
|
|
}
|
|
|
|
}
|
2019-12-30 13:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_variant_data(&mut self, s: &'a ast::VariantData) {
|
2022-10-25 16:15:15 +00:00
|
|
|
if let Some(ctor_node_id) = s.ctor_node_id() {
|
|
|
|
self.check_id(ctor_node_id);
|
2019-12-30 13:11:49 +00:00
|
|
|
}
|
|
|
|
ast_visit::walk_struct_def(self, s);
|
|
|
|
}
|
|
|
|
|
2021-03-15 21:36:07 +00:00
|
|
|
fn visit_field_def(&mut self, s: &'a ast::FieldDef) {
|
2019-12-30 13:11:49 +00:00
|
|
|
self.with_lint_attrs(s.id, &s.attrs, |cx| {
|
2021-03-15 21:36:07 +00:00
|
|
|
ast_visit::walk_field_def(cx, s);
|
2019-12-30 13:11:49 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_variant(&mut self, v: &'a ast::Variant) {
|
|
|
|
self.with_lint_attrs(v.id, &v.attrs, |cx| {
|
2022-12-09 00:40:39 +00:00
|
|
|
lint_callback!(cx, check_variant, v);
|
2019-12-30 13:11:49 +00:00
|
|
|
ast_visit::walk_variant(cx, v);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_ty(&mut self, t: &'a ast::Ty) {
|
2022-12-09 00:40:39 +00:00
|
|
|
lint_callback!(self, check_ty, t);
|
2019-12-30 13:11:49 +00:00
|
|
|
self.check_id(t.id);
|
|
|
|
ast_visit::walk_ty(self, t);
|
|
|
|
}
|
|
|
|
|
2020-04-19 11:00:18 +00:00
|
|
|
fn visit_ident(&mut self, ident: Ident) {
|
2022-12-09 00:40:39 +00:00
|
|
|
lint_callback!(self, check_ident, ident);
|
2019-12-30 13:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_local(&mut self, l: &'a ast::Local) {
|
|
|
|
self.with_lint_attrs(l.id, &l.attrs, |cx| {
|
2022-12-09 00:40:39 +00:00
|
|
|
lint_callback!(cx, check_local, l);
|
2019-12-30 13:11:49 +00:00
|
|
|
ast_visit::walk_local(cx, l);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_block(&mut self, b: &'a ast::Block) {
|
2022-12-09 00:40:39 +00:00
|
|
|
lint_callback!(self, check_block, b);
|
2019-12-30 13:11:49 +00:00
|
|
|
self.check_id(b.id);
|
|
|
|
ast_visit::walk_block(self, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_arm(&mut self, a: &'a ast::Arm) {
|
2021-07-15 19:26:27 +00:00
|
|
|
self.with_lint_attrs(a.id, &a.attrs, |cx| {
|
2022-12-09 00:40:39 +00:00
|
|
|
lint_callback!(cx, check_arm, a);
|
2021-07-15 19:26:27 +00:00
|
|
|
ast_visit::walk_arm(cx, a);
|
|
|
|
})
|
2019-12-30 13:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_expr_post(&mut self, e: &'a ast::Expr) {
|
2021-01-30 00:03:20 +00:00
|
|
|
// Explicitly check for lints associated with 'closure_id', since
|
|
|
|
// it does not have a corresponding AST node
|
2021-02-03 08:40:59 +00:00
|
|
|
match e.kind {
|
2022-09-08 00:52:51 +00:00
|
|
|
ast::ExprKind::Closure(box ast::Closure {
|
|
|
|
asyncness: ast::Async::Yes { closure_id, .. },
|
|
|
|
..
|
2023-01-31 21:13:25 +00:00
|
|
|
}) => self.check_id(closure_id),
|
2021-02-03 08:40:59 +00:00
|
|
|
_ => {}
|
2021-01-30 00:03:20 +00:00
|
|
|
}
|
2023-09-01 02:11:58 +00:00
|
|
|
lint_callback!(self, check_expr_post, e);
|
2019-12-30 13:11:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-09 22:20:06 +00:00
|
|
|
fn visit_generic_arg(&mut self, arg: &'a ast::GenericArg) {
|
2022-12-09 00:40:39 +00:00
|
|
|
lint_callback!(self, check_generic_arg, arg);
|
2020-10-09 22:20:06 +00:00
|
|
|
ast_visit::walk_generic_arg(self, arg);
|
|
|
|
}
|
|
|
|
|
2019-12-30 13:11:49 +00:00
|
|
|
fn visit_generic_param(&mut self, param: &'a ast::GenericParam) {
|
2022-05-05 20:44:12 +00:00
|
|
|
self.with_lint_attrs(param.id, ¶m.attrs, |cx| {
|
2022-12-09 00:40:39 +00:00
|
|
|
lint_callback!(cx, check_generic_param, param);
|
2022-05-05 20:44:12 +00:00
|
|
|
ast_visit::walk_generic_param(cx, param);
|
|
|
|
});
|
2019-12-30 13:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_generics(&mut self, g: &'a ast::Generics) {
|
2022-12-09 00:40:39 +00:00
|
|
|
lint_callback!(self, check_generics, g);
|
2019-12-30 13:11:49 +00:00
|
|
|
ast_visit::walk_generics(self, g);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_where_predicate(&mut self, p: &'a ast::WherePredicate) {
|
2023-01-14 08:52:46 +00:00
|
|
|
lint_callback!(self, enter_where_predicate, p);
|
2019-12-30 13:11:49 +00:00
|
|
|
ast_visit::walk_where_predicate(self, p);
|
2023-01-14 08:52:46 +00:00
|
|
|
lint_callback!(self, exit_where_predicate, p);
|
2019-12-30 13:11:49 +00:00
|
|
|
}
|
|
|
|
|
2022-08-11 01:05:26 +00:00
|
|
|
fn visit_poly_trait_ref(&mut self, t: &'a ast::PolyTraitRef) {
|
2022-12-09 00:40:39 +00:00
|
|
|
lint_callback!(self, check_poly_trait_ref, t);
|
2022-08-11 01:05:26 +00:00
|
|
|
ast_visit::walk_poly_trait_ref(self, t);
|
2019-12-30 13:11:49 +00:00
|
|
|
}
|
|
|
|
|
2020-01-29 23:18:54 +00:00
|
|
|
fn visit_assoc_item(&mut self, item: &'a ast::AssocItem, ctxt: ast_visit::AssocCtxt) {
|
|
|
|
self.with_lint_attrs(item.id, &item.attrs, |cx| match ctxt {
|
|
|
|
ast_visit::AssocCtxt::Trait => {
|
2022-12-09 00:40:39 +00:00
|
|
|
lint_callback!(cx, check_trait_item, item);
|
2020-01-29 23:18:54 +00:00
|
|
|
ast_visit::walk_assoc_item(cx, item, ctxt);
|
|
|
|
}
|
|
|
|
ast_visit::AssocCtxt::Impl => {
|
2022-12-09 00:40:39 +00:00
|
|
|
lint_callback!(cx, check_impl_item, item);
|
2020-01-29 23:18:54 +00:00
|
|
|
ast_visit::walk_assoc_item(cx, item, ctxt);
|
|
|
|
}
|
2019-12-30 13:11:49 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-05-10 17:56:46 +00:00
|
|
|
fn visit_lifetime(&mut self, lt: &'a ast::Lifetime, _: ast_visit::LifetimeCtxt) {
|
2019-12-30 13:11:49 +00:00
|
|
|
self.check_id(lt.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_path(&mut self, p: &'a ast::Path, id: ast::NodeId) {
|
|
|
|
self.check_id(id);
|
|
|
|
ast_visit::walk_path(self, p);
|
|
|
|
}
|
|
|
|
|
2022-09-12 00:43:34 +00:00
|
|
|
fn visit_path_segment(&mut self, s: &'a ast::PathSegment) {
|
2022-03-10 22:12:35 +00:00
|
|
|
self.check_id(s.id);
|
2022-09-12 00:43:34 +00:00
|
|
|
ast_visit::walk_path_segment(self, s);
|
2022-03-10 22:12:35 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 13:11:49 +00:00
|
|
|
fn visit_attribute(&mut self, attr: &'a ast::Attribute) {
|
2022-12-09 00:40:39 +00:00
|
|
|
lint_callback!(self, check_attribute, attr);
|
2019-12-30 13:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_mac_def(&mut self, mac: &'a ast::MacroDef, id: ast::NodeId) {
|
2022-12-09 00:40:39 +00:00
|
|
|
lint_callback!(self, check_mac_def, mac);
|
2019-12-30 13:11:49 +00:00
|
|
|
self.check_id(id);
|
|
|
|
}
|
|
|
|
|
2020-11-03 17:34:57 +00:00
|
|
|
fn visit_mac_call(&mut self, mac: &'a ast::MacCall) {
|
2022-12-09 00:40:39 +00:00
|
|
|
lint_callback!(self, check_mac, mac);
|
2020-11-03 17:26:17 +00:00
|
|
|
ast_visit::walk_mac(self, mac);
|
2019-12-30 13:11:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-09 01:27:43 +00:00
|
|
|
// Combines multiple lint passes into a single pass, at runtime. Each
|
|
|
|
// `check_foo` method in `$methods` within this pass simply calls `check_foo`
|
|
|
|
// once per `$pass`. Compare with `declare_combined_early_lint_pass`, which is
|
|
|
|
// similar, but combines lint passes at compile time.
|
|
|
|
struct RuntimeCombinedEarlyLintPass<'a> {
|
|
|
|
passes: &'a mut [EarlyLintPassObject],
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(rustc::lint_pass_impl_without_macro)]
|
|
|
|
impl LintPass for RuntimeCombinedEarlyLintPass<'_> {
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
panic!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! impl_early_lint_pass {
|
|
|
|
([], [$($(#[$attr:meta])* fn $f:ident($($param:ident: $arg:ty),*);)*]) => (
|
|
|
|
impl EarlyLintPass for RuntimeCombinedEarlyLintPass<'_> {
|
|
|
|
$(fn $f(&mut self, context: &EarlyContext<'_>, $($param: $arg),*) {
|
|
|
|
for pass in self.passes.iter_mut() {
|
|
|
|
pass.$f(context, $($param),*);
|
|
|
|
}
|
|
|
|
})*
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
crate::early_lint_methods!(impl_early_lint_pass, []);
|
|
|
|
|
2021-12-07 10:28:12 +00:00
|
|
|
/// Early lints work on different nodes - either on the crate root, or on freshly loaded modules.
|
|
|
|
/// This trait generalizes over those nodes.
|
|
|
|
pub trait EarlyCheckNode<'a>: Copy {
|
|
|
|
fn id(self) -> ast::NodeId;
|
|
|
|
fn attrs<'b>(self) -> &'b [ast::Attribute]
|
|
|
|
where
|
|
|
|
'a: 'b;
|
2022-12-09 01:27:43 +00:00
|
|
|
fn check<'b, T: EarlyLintPass>(self, cx: &mut EarlyContextAndPass<'b, T>)
|
2021-12-07 10:28:12 +00:00
|
|
|
where
|
|
|
|
'a: 'b;
|
|
|
|
}
|
|
|
|
|
2023-03-14 12:53:04 +00:00
|
|
|
impl<'a> EarlyCheckNode<'a> for (&'a ast::Crate, &'a [ast::Attribute]) {
|
2021-12-07 10:28:12 +00:00
|
|
|
fn id(self) -> ast::NodeId {
|
|
|
|
ast::CRATE_NODE_ID
|
|
|
|
}
|
|
|
|
fn attrs<'b>(self) -> &'b [ast::Attribute]
|
|
|
|
where
|
|
|
|
'a: 'b,
|
|
|
|
{
|
2023-03-14 12:53:04 +00:00
|
|
|
&self.1
|
2021-12-07 10:28:12 +00:00
|
|
|
}
|
2022-12-09 01:27:43 +00:00
|
|
|
fn check<'b, T: EarlyLintPass>(self, cx: &mut EarlyContextAndPass<'b, T>)
|
2021-12-07 10:28:12 +00:00
|
|
|
where
|
|
|
|
'a: 'b,
|
|
|
|
{
|
2023-03-14 12:53:04 +00:00
|
|
|
lint_callback!(cx, check_crate, self.0);
|
|
|
|
ast_visit::walk_crate(cx, self.0);
|
|
|
|
lint_callback!(cx, check_crate_post, self.0);
|
2021-12-07 10:28:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> EarlyCheckNode<'a> for (ast::NodeId, &'a [ast::Attribute], &'a [P<ast::Item>]) {
|
|
|
|
fn id(self) -> ast::NodeId {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
fn attrs<'b>(self) -> &'b [ast::Attribute]
|
|
|
|
where
|
|
|
|
'a: 'b,
|
|
|
|
{
|
|
|
|
self.1
|
|
|
|
}
|
2022-12-09 01:27:43 +00:00
|
|
|
fn check<'b, T: EarlyLintPass>(self, cx: &mut EarlyContextAndPass<'b, T>)
|
2021-12-07 10:28:12 +00:00
|
|
|
where
|
|
|
|
'a: 'b,
|
|
|
|
{
|
|
|
|
walk_list!(cx, visit_attribute, self.1);
|
|
|
|
walk_list!(cx, visit_item, self.2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn check_ast_node<'a>(
|
2019-12-30 13:11:49 +00:00
|
|
|
sess: &Session,
|
2023-08-09 12:28:00 +00:00
|
|
|
features: &Features,
|
2021-09-27 21:28:49 +00:00
|
|
|
pre_expansion: bool,
|
2019-12-30 13:11:49 +00:00
|
|
|
lint_store: &LintStore,
|
2021-09-28 22:17:54 +00:00
|
|
|
registered_tools: &RegisteredTools,
|
2019-12-30 13:11:49 +00:00
|
|
|
lint_buffer: Option<LintBuffer>,
|
2022-12-01 02:22:08 +00:00
|
|
|
builtin_lints: impl EarlyLintPass + 'static,
|
2021-12-07 10:28:12 +00:00
|
|
|
check_node: impl EarlyCheckNode<'a>,
|
2019-12-30 13:11:49 +00:00
|
|
|
) {
|
2022-12-09 01:27:43 +00:00
|
|
|
let context = EarlyContext::new(
|
|
|
|
sess,
|
2023-08-09 12:28:00 +00:00
|
|
|
features,
|
2022-12-09 01:27:43 +00:00
|
|
|
!pre_expansion,
|
|
|
|
lint_store,
|
|
|
|
registered_tools,
|
|
|
|
lint_buffer.unwrap_or_default(),
|
|
|
|
);
|
|
|
|
|
2022-12-09 03:15:26 +00:00
|
|
|
// Note: `passes` is often empty. In that case, it's faster to run
|
|
|
|
// `builtin_lints` directly rather than bundling it up into the
|
|
|
|
// `RuntimeCombinedEarlyLintPass`.
|
2020-03-15 23:43:37 +00:00
|
|
|
let passes =
|
|
|
|
if pre_expansion { &lint_store.pre_expansion_passes } else { &lint_store.early_passes };
|
2022-12-09 03:15:26 +00:00
|
|
|
if passes.is_empty() {
|
|
|
|
check_ast_node_inner(sess, check_node, context, builtin_lints);
|
|
|
|
} else {
|
|
|
|
let mut passes: Vec<_> = passes.iter().map(|mk_pass| (mk_pass)()).collect();
|
|
|
|
passes.push(Box::new(builtin_lints));
|
|
|
|
let pass = RuntimeCombinedEarlyLintPass { passes: &mut passes[..] };
|
|
|
|
check_ast_node_inner(sess, check_node, context, pass);
|
|
|
|
}
|
|
|
|
}
|
2022-12-09 01:27:43 +00:00
|
|
|
|
2022-12-09 03:15:26 +00:00
|
|
|
pub fn check_ast_node_inner<'a, T: EarlyLintPass>(
|
|
|
|
sess: &Session,
|
|
|
|
check_node: impl EarlyCheckNode<'a>,
|
|
|
|
context: EarlyContext<'_>,
|
|
|
|
pass: T,
|
|
|
|
) {
|
2022-12-09 01:27:43 +00:00
|
|
|
let mut cx = EarlyContextAndPass { context, pass };
|
2019-12-30 13:11:49 +00:00
|
|
|
|
2022-12-01 02:31:48 +00:00
|
|
|
cx.with_lint_attrs(check_node.id(), check_node.attrs(), |cx| check_node.check(cx));
|
2022-11-25 03:42:43 +00:00
|
|
|
|
2019-12-30 13:11:49 +00:00
|
|
|
// All of the buffered lints should have been emitted at this point.
|
|
|
|
// If not, that means that we somehow buffered a lint for a node id
|
|
|
|
// that was not lint-checked (perhaps it doesn't exist?). This is a bug.
|
2022-12-01 02:31:48 +00:00
|
|
|
for (id, lints) in cx.context.buffered.map {
|
2021-01-22 19:50:21 +00:00
|
|
|
for early_lint in lints {
|
2021-07-15 01:07:56 +00:00
|
|
|
sess.delay_span_bug(
|
|
|
|
early_lint.span,
|
Restrict `From<S>` for `{D,Subd}iagnosticMessage`.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.
This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.
As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
2023-04-20 03:26:58 +00:00
|
|
|
format!(
|
2021-07-15 01:07:56 +00:00
|
|
|
"failed to process buffered lint here (dummy = {})",
|
|
|
|
id == ast::DUMMY_NODE_ID
|
|
|
|
),
|
|
|
|
);
|
2019-12-30 13:11:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|