2020-01-09 06:52:01 +00:00
|
|
|
use crate::context::{EarlyContext, LateContext};
|
2014-11-06 08:05:53 +00:00
|
|
|
|
2020-04-27 17:56:11 +00:00
|
|
|
use rustc_ast as ast;
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir as hir;
|
2020-01-05 09:47:20 +00:00
|
|
|
use rustc_session::lint::builtin::HardwiredLints;
|
2020-01-09 06:52:01 +00:00
|
|
|
use rustc_session::lint::LintPass;
|
2022-07-29 01:28:51 +00:00
|
|
|
use rustc_span::symbol::Ident;
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::Span;
|
2014-06-06 22:49:48 +00:00
|
|
|
|
2018-06-21 07:04:50 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! late_lint_methods {
|
2022-12-07 02:27:04 +00:00
|
|
|
($macro:path, $args:tt) => (
|
|
|
|
$macro!($args, [
|
|
|
|
fn check_body(a: &'tcx hir::Body<'tcx>);
|
|
|
|
fn check_body_post(a: &'tcx hir::Body<'tcx>);
|
2021-09-12 09:58:27 +00:00
|
|
|
fn check_crate();
|
|
|
|
fn check_crate_post();
|
2022-12-07 02:27:04 +00:00
|
|
|
fn check_mod(a: &'tcx hir::Mod<'tcx>, b: hir::HirId);
|
|
|
|
fn check_foreign_item(a: &'tcx hir::ForeignItem<'tcx>);
|
|
|
|
fn check_item(a: &'tcx hir::Item<'tcx>);
|
|
|
|
fn check_item_post(a: &'tcx hir::Item<'tcx>);
|
|
|
|
fn check_local(a: &'tcx hir::Local<'tcx>);
|
|
|
|
fn check_block(a: &'tcx hir::Block<'tcx>);
|
|
|
|
fn check_block_post(a: &'tcx hir::Block<'tcx>);
|
|
|
|
fn check_stmt(a: &'tcx hir::Stmt<'tcx>);
|
|
|
|
fn check_arm(a: &'tcx hir::Arm<'tcx>);
|
|
|
|
fn check_pat(a: &'tcx hir::Pat<'tcx>);
|
|
|
|
fn check_expr(a: &'tcx hir::Expr<'tcx>);
|
|
|
|
fn check_expr_post(a: &'tcx hir::Expr<'tcx>);
|
|
|
|
fn check_ty(a: &'tcx hir::Ty<'tcx>);
|
|
|
|
fn check_generic_param(a: &'tcx hir::GenericParam<'tcx>);
|
|
|
|
fn check_generics(a: &'tcx hir::Generics<'tcx>);
|
|
|
|
fn check_poly_trait_ref(a: &'tcx hir::PolyTraitRef<'tcx>);
|
2018-06-21 07:04:50 +00:00
|
|
|
fn check_fn(
|
2022-12-07 02:27:04 +00:00
|
|
|
a: rustc_hir::intravisit::FnKind<'tcx>,
|
|
|
|
b: &'tcx hir::FnDecl<'tcx>,
|
|
|
|
c: &'tcx hir::Body<'tcx>,
|
2018-06-21 07:04:50 +00:00
|
|
|
d: Span,
|
2019-02-06 13:16:11 +00:00
|
|
|
e: hir::HirId);
|
2022-12-07 02:27:04 +00:00
|
|
|
fn check_trait_item(a: &'tcx hir::TraitItem<'tcx>);
|
|
|
|
fn check_impl_item(a: &'tcx hir::ImplItem<'tcx>);
|
|
|
|
fn check_impl_item_post(a: &'tcx hir::ImplItem<'tcx>);
|
|
|
|
fn check_struct_def(a: &'tcx hir::VariantData<'tcx>);
|
|
|
|
fn check_field_def(a: &'tcx hir::FieldDef<'tcx>);
|
|
|
|
fn check_variant(a: &'tcx hir::Variant<'tcx>);
|
|
|
|
fn check_path(a: &hir::Path<'tcx>, b: hir::HirId);
|
|
|
|
fn check_attribute(a: &'tcx ast::Attribute);
|
2018-06-21 07:04:50 +00:00
|
|
|
|
|
|
|
/// Called when entering a syntax node that can have lint attributes such
|
|
|
|
/// as `#[allow(...)]`. Called with *all* the attributes of that node.
|
2022-12-07 02:27:04 +00:00
|
|
|
fn enter_lint_attrs(a: &'tcx [ast::Attribute]);
|
2018-06-21 07:04:50 +00:00
|
|
|
|
|
|
|
/// Counterpart to `enter_lint_attrs`.
|
2022-12-07 02:27:04 +00:00
|
|
|
fn exit_lint_attrs(a: &'tcx [ast::Attribute]);
|
2018-06-21 07:04:50 +00:00
|
|
|
]);
|
|
|
|
)
|
|
|
|
}
|
2015-09-14 23:35:25 +00:00
|
|
|
|
|
|
|
/// Trait for types providing lint checks.
|
|
|
|
///
|
|
|
|
/// Each `check` method checks a single syntax node, and should not
|
|
|
|
/// invoke methods recursively (unlike `Visitor`). By default they
|
|
|
|
/// do nothing.
|
|
|
|
//
|
|
|
|
// FIXME: eliminate the duplication with `Visitor`. But this also
|
|
|
|
// contains a few lint-specific methods with no equivalent in `Visitor`.
|
2018-06-21 07:04:50 +00:00
|
|
|
|
|
|
|
macro_rules! declare_late_lint_pass {
|
2022-12-07 02:27:04 +00:00
|
|
|
([], [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
|
|
|
|
pub trait LateLintPass<'tcx>: LintPass {
|
|
|
|
$(#[inline(always)] fn $name(&mut self, _: &LateContext<'tcx>, $(_: $arg),*) {})*
|
2018-06-21 07:04:50 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-12-07 03:18:19 +00:00
|
|
|
// Declare the `LateLintPass` trait, which contains empty default definitions
|
|
|
|
// for all the `check_*` methods.
|
2022-12-07 02:27:04 +00:00
|
|
|
late_lint_methods!(declare_late_lint_pass, []);
|
2018-06-21 07:04:50 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl LateLintPass<'_> for HardwiredLints {}
|
2020-01-05 09:07:26 +00:00
|
|
|
|
2018-06-21 07:04:50 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! expand_combined_late_lint_pass_method {
|
|
|
|
([$($passes:ident),*], $self: ident, $name: ident, $params:tt) => ({
|
|
|
|
$($self.$passes.$name $params;)*
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! expand_combined_late_lint_pass_methods {
|
|
|
|
($passes:tt, [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
|
2020-06-25 20:41:36 +00:00
|
|
|
$(fn $name(&mut self, context: &LateContext<'tcx>, $($param: $arg),*) {
|
2018-06-21 07:04:50 +00:00
|
|
|
expand_combined_late_lint_pass_method!($passes, self, $name, (context, $($param),*));
|
|
|
|
})*
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! declare_combined_late_lint_pass {
|
2022-12-07 02:27:04 +00:00
|
|
|
([$v:vis $name:ident, [$($passes:ident: $constructor:expr,)*]], $methods:tt) => (
|
2018-06-21 07:04:50 +00:00
|
|
|
#[allow(non_snake_case)]
|
2019-01-31 00:36:11 +00:00
|
|
|
$v struct $name {
|
2018-06-21 07:04:50 +00:00
|
|
|
$($passes: $passes,)*
|
|
|
|
}
|
|
|
|
|
|
|
|
impl $name {
|
2019-01-31 00:36:11 +00:00
|
|
|
$v fn new() -> Self {
|
2018-06-21 07:04:50 +00:00
|
|
|
Self {
|
|
|
|
$($passes: $constructor,)*
|
|
|
|
}
|
|
|
|
}
|
2019-10-07 22:04:05 +00:00
|
|
|
|
|
|
|
$v fn get_lints() -> LintArray {
|
|
|
|
let mut lints = Vec::new();
|
|
|
|
$(lints.extend_from_slice(&$passes::get_lints());)*
|
|
|
|
lints
|
|
|
|
}
|
2018-06-21 07:04:50 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for $name {
|
2018-06-21 07:04:50 +00:00
|
|
|
expand_combined_late_lint_pass_methods!([$($passes),*], $methods);
|
|
|
|
}
|
|
|
|
|
2020-01-09 06:52:01 +00:00
|
|
|
#[allow(rustc::lint_pass_impl_without_macro)]
|
2018-06-21 07:04:50 +00:00
|
|
|
impl LintPass for $name {
|
2019-01-18 06:40:55 +00:00
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
panic!()
|
|
|
|
}
|
2018-06-21 07:04:50 +00:00
|
|
|
}
|
|
|
|
)
|
2015-09-14 23:35:25 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 06:40:55 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! early_lint_methods {
|
|
|
|
($macro:path, $args:tt) => (
|
|
|
|
$macro!($args, [
|
2019-08-27 11:24:32 +00:00
|
|
|
fn check_param(a: &ast::Param);
|
2020-04-19 11:00:18 +00:00
|
|
|
fn check_ident(a: Ident);
|
2019-01-18 06:40:55 +00:00
|
|
|
fn check_crate(a: &ast::Crate);
|
|
|
|
fn check_crate_post(a: &ast::Crate);
|
|
|
|
fn check_item(a: &ast::Item);
|
|
|
|
fn check_item_post(a: &ast::Item);
|
|
|
|
fn check_local(a: &ast::Local);
|
|
|
|
fn check_block(a: &ast::Block);
|
|
|
|
fn check_stmt(a: &ast::Stmt);
|
|
|
|
fn check_arm(a: &ast::Arm);
|
2019-04-21 14:09:30 +00:00
|
|
|
fn check_pat(a: &ast::Pat);
|
|
|
|
fn check_pat_post(a: &ast::Pat);
|
2019-01-18 06:40:55 +00:00
|
|
|
fn check_expr(a: &ast::Expr);
|
|
|
|
fn check_ty(a: &ast::Ty);
|
2020-10-09 22:20:06 +00:00
|
|
|
fn check_generic_arg(a: &ast::GenericArg);
|
2019-01-18 06:40:55 +00:00
|
|
|
fn check_generic_param(a: &ast::GenericParam);
|
|
|
|
fn check_generics(a: &ast::Generics);
|
2022-08-11 01:05:26 +00:00
|
|
|
fn check_poly_trait_ref(a: &ast::PolyTraitRef);
|
2020-02-29 17:37:32 +00:00
|
|
|
fn check_fn(a: rustc_ast::visit::FnKind<'_>, c: Span, d_: ast::NodeId);
|
2019-12-07 23:08:09 +00:00
|
|
|
fn check_trait_item(a: &ast::AssocItem);
|
|
|
|
fn check_impl_item(a: &ast::AssocItem);
|
2019-08-24 16:54:40 +00:00
|
|
|
fn check_variant(a: &ast::Variant);
|
2019-01-18 06:40:55 +00:00
|
|
|
fn check_attribute(a: &ast::Attribute);
|
2022-09-12 03:50:18 +00:00
|
|
|
fn check_mac_def(a: &ast::MacroDef);
|
2020-02-29 16:32:20 +00:00
|
|
|
fn check_mac(a: &ast::MacCall);
|
2019-01-18 06:40:55 +00:00
|
|
|
|
|
|
|
/// Called when entering a syntax node that can have lint attributes such
|
|
|
|
/// as `#[allow(...)]`. Called with *all* the attributes of that node.
|
|
|
|
fn enter_lint_attrs(a: &[ast::Attribute]);
|
|
|
|
|
|
|
|
/// Counterpart to `enter_lint_attrs`.
|
|
|
|
fn exit_lint_attrs(a: &[ast::Attribute]);
|
|
|
|
]);
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! declare_early_lint_pass {
|
2022-11-25 02:42:01 +00:00
|
|
|
([], [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
|
2019-01-18 06:40:55 +00:00
|
|
|
pub trait EarlyLintPass: LintPass {
|
2022-11-25 02:42:01 +00:00
|
|
|
$(#[inline(always)] fn $name(&mut self, _: &EarlyContext<'_>, $(_: $arg),*) {})*
|
2019-01-18 06:40:55 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-12-07 03:18:19 +00:00
|
|
|
// Declare the `EarlyLintPass` trait, which contains empty default definitions
|
|
|
|
// for all the `check_*` methods.
|
2019-01-18 06:40:55 +00:00
|
|
|
early_lint_methods!(declare_early_lint_pass, []);
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! expand_combined_early_lint_pass_method {
|
|
|
|
([$($passes:ident),*], $self: ident, $name: ident, $params:tt) => ({
|
|
|
|
$($self.$passes.$name $params;)*
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! expand_combined_early_lint_pass_methods {
|
|
|
|
($passes:tt, [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
|
|
|
|
$(fn $name(&mut self, context: &EarlyContext<'_>, $($param: $arg),*) {
|
|
|
|
expand_combined_early_lint_pass_method!($passes, self, $name, (context, $($param),*));
|
|
|
|
})*
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! declare_combined_early_lint_pass {
|
|
|
|
([$v:vis $name:ident, [$($passes:ident: $constructor:expr,)*]], $methods:tt) => (
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
$v struct $name {
|
|
|
|
$($passes: $passes,)*
|
|
|
|
}
|
|
|
|
|
|
|
|
impl $name {
|
|
|
|
$v fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
$($passes: $constructor,)*
|
|
|
|
}
|
|
|
|
}
|
2019-10-07 22:04:05 +00:00
|
|
|
|
|
|
|
$v fn get_lints() -> LintArray {
|
|
|
|
let mut lints = Vec::new();
|
|
|
|
$(lints.extend_from_slice(&$passes::get_lints());)*
|
|
|
|
lints
|
|
|
|
}
|
2019-01-18 06:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EarlyLintPass for $name {
|
|
|
|
expand_combined_early_lint_pass_methods!([$($passes),*], $methods);
|
|
|
|
}
|
|
|
|
|
2020-01-09 06:52:01 +00:00
|
|
|
#[allow(rustc::lint_pass_impl_without_macro)]
|
2019-01-18 06:40:55 +00:00
|
|
|
impl LintPass for $name {
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
panic!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-06-02 22:27:15 +00:00
|
|
|
}
|
|
|
|
|
2014-06-06 22:49:48 +00:00
|
|
|
/// A lint pass boxed up as a trait object.
|
2022-12-01 04:42:31 +00:00
|
|
|
pub type EarlyLintPassObject = Box<dyn EarlyLintPass + 'static>;
|
|
|
|
pub type LateLintPassObject<'tcx> = Box<dyn LateLintPass<'tcx> + 'tcx>;
|