Auto merge of #105416 - nnethercote:more-linting-tweaks, r=cjgillot

More linting tweaks

Squeeze a little more blood from this stone.

r? `@cjgillot`
This commit is contained in:
bors 2022-12-10 19:49:51 +00:00
commit c6fcdb6906
6 changed files with 80 additions and 58 deletions

View file

@ -97,6 +97,7 @@ fn pierce_parens(mut expr: &ast::Expr) -> &ast::Expr {
} }
impl EarlyLintPass for WhileTrue { impl EarlyLintPass for WhileTrue {
#[inline]
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) { fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
if let ast::ExprKind::While(cond, _, label) = &e.kind if let ast::ExprKind::While(cond, _, label) = &e.kind
&& let cond = pierce_parens(cond) && let cond = pierce_parens(cond)
@ -361,6 +362,7 @@ impl EarlyLintPass for UnsafeCode {
} }
} }
#[inline]
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) { fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
if let ast::ExprKind::Block(ref blk, _) = e.kind { if let ast::ExprKind::Block(ref blk, _) = e.kind {
// Don't warn about generated blocks; that'll just pollute the output. // Don't warn about generated blocks; that'll just pollute the output.
@ -583,6 +585,7 @@ impl MissingDoc {
} }
impl<'tcx> LateLintPass<'tcx> for MissingDoc { impl<'tcx> LateLintPass<'tcx> for MissingDoc {
#[inline]
fn enter_lint_attrs(&mut self, _cx: &LateContext<'_>, attrs: &[ast::Attribute]) { fn enter_lint_attrs(&mut self, _cx: &LateContext<'_>, attrs: &[ast::Attribute]) {
let doc_hidden = self.doc_hidden() let doc_hidden = self.doc_hidden()
|| attrs.iter().any(|attr| { || attrs.iter().any(|attr| {

View file

@ -37,7 +37,9 @@ pub struct EarlyContextAndPasses<'a> {
} }
impl<'a> EarlyContextAndPasses<'a> { impl<'a> EarlyContextAndPasses<'a> {
fn check_id(&mut self, id: ast::NodeId) { // This always-inlined function is for the hot call site.
#[inline(always)]
fn inlined_check_id(&mut self, id: ast::NodeId) {
for early_lint in self.context.buffered.take(id) { for early_lint in self.context.buffered.take(id) {
let BufferedEarlyLint { span, msg, node_id: _, lint_id, diagnostic } = early_lint; let BufferedEarlyLint { span, msg, node_id: _, lint_id, diagnostic } = early_lint;
self.context.lookup_with_diagnostics( self.context.lookup_with_diagnostics(
@ -50,6 +52,11 @@ impl<'a> EarlyContextAndPasses<'a> {
} }
} }
// This non-inlined function is for the cold call sites.
fn check_id(&mut self, id: ast::NodeId) {
self.inlined_check_id(id)
}
/// Merge the lints specified by any lint attributes into the /// Merge the lints specified by any lint attributes into the
/// current lint context, call the provided function, then reset the /// current lint context, call the provided function, then reset the
/// lints in effect to their previous state. /// lints in effect to their previous state.
@ -61,7 +68,7 @@ impl<'a> EarlyContextAndPasses<'a> {
debug!(?id); debug!(?id);
let push = self.context.builder.push(attrs, is_crate_node, None); let push = self.context.builder.push(attrs, is_crate_node, None);
self.check_id(id); self.inlined_check_id(id);
debug!("early context: enter_attrs({:?})", attrs); debug!("early context: enter_attrs({:?})", attrs);
run_early_passes!(self, enter_lint_attrs, attrs); run_early_passes!(self, enter_lint_attrs, attrs);
f(self); f(self);

View file

@ -121,6 +121,7 @@ impl EarlyLintPass for HiddenUnicodeCodepoints {
} }
} }
#[inline]
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) { fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
// byte strings are already handled well enough by `EscapeError::NonAsciiCharInByteString` // byte strings are already handled well enough by `EscapeError::NonAsciiCharInByteString`
match &expr.kind { match &expr.kind {

View file

@ -127,6 +127,7 @@ fn lint_mod(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
late::late_lint_mod(tcx, module_def_id, BuiltinCombinedModuleLateLintPass::new()); late::late_lint_mod(tcx, module_def_id, BuiltinCombinedModuleLateLintPass::new());
} }
// See the comment on `BuiltinCombinedEarlyLintPass`, which is similar.
early_lint_methods!( early_lint_methods!(
declare_combined_early_lint_pass, declare_combined_early_lint_pass,
[ [
@ -137,6 +138,9 @@ early_lint_methods!(
] ]
); );
// Declare `BuiltinCombinedEarlyLintPass`, a lint pass that combines multiple
// lint passes into a single pass for maximum speed. Each `check_foo` method
// within this pass simply calls `check_foo` once per listed lint.
early_lint_methods!( early_lint_methods!(
declare_combined_early_lint_pass, declare_combined_early_lint_pass,
[ [
@ -162,7 +166,9 @@ early_lint_methods!(
] ]
); );
// FIXME: Make a separate lint type which do not require typeck tables // FIXME: Make a separate lint type which does not require typeck tables.
// See the comment on `BuiltinCombinedEarlyLintPass`, which is similar.
late_lint_methods!( late_lint_methods!(
declare_combined_late_lint_pass, declare_combined_late_lint_pass,
[ [
@ -179,10 +185,10 @@ late_lint_methods!(
// Keeps a global list of foreign declarations. // Keeps a global list of foreign declarations.
ClashingExternDeclarations: ClashingExternDeclarations::new(), ClashingExternDeclarations: ClashingExternDeclarations::new(),
] ]
], ]
['tcx]
); );
// See the comment on `BuiltinCombinedEarlyLintPass`, which is similar.
late_lint_methods!( late_lint_methods!(
declare_combined_late_lint_pass, declare_combined_late_lint_pass,
[ [
@ -229,8 +235,7 @@ late_lint_methods!(
NamedAsmLabels: NamedAsmLabels, NamedAsmLabels: NamedAsmLabels,
OpaqueHiddenInferredBound: OpaqueHiddenInferredBound, OpaqueHiddenInferredBound: OpaqueHiddenInferredBound,
] ]
], ]
['tcx]
); );
pub fn new_lint_store(internal_lints: bool) -> LintStore { pub fn new_lint_store(internal_lints: bool) -> LintStore {

View file

@ -9,49 +9,49 @@ use rustc_span::Span;
#[macro_export] #[macro_export]
macro_rules! late_lint_methods { macro_rules! late_lint_methods {
($macro:path, $args:tt, [$hir:tt]) => ( ($macro:path, $args:tt) => (
$macro!($args, [$hir], [ $macro!($args, [
fn check_body(a: &$hir hir::Body<$hir>); fn check_body(a: &'tcx hir::Body<'tcx>);
fn check_body_post(a: &$hir hir::Body<$hir>); fn check_body_post(a: &'tcx hir::Body<'tcx>);
fn check_crate(); fn check_crate();
fn check_crate_post(); fn check_crate_post();
fn check_mod(a: &$hir hir::Mod<$hir>, b: hir::HirId); fn check_mod(a: &'tcx hir::Mod<'tcx>, b: hir::HirId);
fn check_foreign_item(a: &$hir hir::ForeignItem<$hir>); fn check_foreign_item(a: &'tcx hir::ForeignItem<'tcx>);
fn check_item(a: &$hir hir::Item<$hir>); fn check_item(a: &'tcx hir::Item<'tcx>);
fn check_item_post(a: &$hir hir::Item<$hir>); fn check_item_post(a: &'tcx hir::Item<'tcx>);
fn check_local(a: &$hir hir::Local<$hir>); fn check_local(a: &'tcx hir::Local<'tcx>);
fn check_block(a: &$hir hir::Block<$hir>); fn check_block(a: &'tcx hir::Block<'tcx>);
fn check_block_post(a: &$hir hir::Block<$hir>); fn check_block_post(a: &'tcx hir::Block<'tcx>);
fn check_stmt(a: &$hir hir::Stmt<$hir>); fn check_stmt(a: &'tcx hir::Stmt<'tcx>);
fn check_arm(a: &$hir hir::Arm<$hir>); fn check_arm(a: &'tcx hir::Arm<'tcx>);
fn check_pat(a: &$hir hir::Pat<$hir>); fn check_pat(a: &'tcx hir::Pat<'tcx>);
fn check_expr(a: &$hir hir::Expr<$hir>); fn check_expr(a: &'tcx hir::Expr<'tcx>);
fn check_expr_post(a: &$hir hir::Expr<$hir>); fn check_expr_post(a: &'tcx hir::Expr<'tcx>);
fn check_ty(a: &$hir hir::Ty<$hir>); fn check_ty(a: &'tcx hir::Ty<'tcx>);
fn check_generic_param(a: &$hir hir::GenericParam<$hir>); fn check_generic_param(a: &'tcx hir::GenericParam<'tcx>);
fn check_generics(a: &$hir hir::Generics<$hir>); fn check_generics(a: &'tcx hir::Generics<'tcx>);
fn check_poly_trait_ref(a: &$hir hir::PolyTraitRef<$hir>); fn check_poly_trait_ref(a: &'tcx hir::PolyTraitRef<'tcx>);
fn check_fn( fn check_fn(
a: rustc_hir::intravisit::FnKind<$hir>, a: rustc_hir::intravisit::FnKind<'tcx>,
b: &$hir hir::FnDecl<$hir>, b: &'tcx hir::FnDecl<'tcx>,
c: &$hir hir::Body<$hir>, c: &'tcx hir::Body<'tcx>,
d: Span, d: Span,
e: hir::HirId); e: hir::HirId);
fn check_trait_item(a: &$hir hir::TraitItem<$hir>); fn check_trait_item(a: &'tcx hir::TraitItem<'tcx>);
fn check_impl_item(a: &$hir hir::ImplItem<$hir>); fn check_impl_item(a: &'tcx hir::ImplItem<'tcx>);
fn check_impl_item_post(a: &$hir hir::ImplItem<$hir>); fn check_impl_item_post(a: &'tcx hir::ImplItem<'tcx>);
fn check_struct_def(a: &$hir hir::VariantData<$hir>); fn check_struct_def(a: &'tcx hir::VariantData<'tcx>);
fn check_field_def(a: &$hir hir::FieldDef<$hir>); fn check_field_def(a: &'tcx hir::FieldDef<'tcx>);
fn check_variant(a: &$hir hir::Variant<$hir>); fn check_variant(a: &'tcx hir::Variant<'tcx>);
fn check_path(a: &hir::Path<$hir>, b: hir::HirId); fn check_path(a: &hir::Path<'tcx>, b: hir::HirId);
fn check_attribute(a: &$hir ast::Attribute); fn check_attribute(a: &'tcx ast::Attribute);
/// Called when entering a syntax node that can have lint attributes such /// Called when entering a syntax node that can have lint attributes such
/// as `#[allow(...)]`. Called with *all* the attributes of that node. /// as `#[allow(...)]`. Called with *all* the attributes of that node.
fn enter_lint_attrs(a: &$hir [ast::Attribute]); fn enter_lint_attrs(a: &'tcx [ast::Attribute]);
/// Counterpart to `enter_lint_attrs`. /// Counterpart to `enter_lint_attrs`.
fn exit_lint_attrs(a: &$hir [ast::Attribute]); fn exit_lint_attrs(a: &'tcx [ast::Attribute]);
]); ]);
) )
} }
@ -66,21 +66,23 @@ macro_rules! late_lint_methods {
// contains a few lint-specific methods with no equivalent in `Visitor`. // contains a few lint-specific methods with no equivalent in `Visitor`.
macro_rules! declare_late_lint_pass { macro_rules! declare_late_lint_pass {
([], [$hir:tt], [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => ( ([], [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
pub trait LateLintPass<$hir>: LintPass { pub trait LateLintPass<'tcx>: LintPass {
$(#[inline(always)] fn $name(&mut self, _: &LateContext<$hir>, $(_: $arg),*) {})* $(#[inline(always)] fn $name(&mut self, _: &LateContext<'tcx>, $(_: $arg),*) {})*
} }
) )
} }
late_lint_methods!(declare_late_lint_pass, [], ['tcx]); // Declare the `LateLintPass` trait, which contains empty default definitions
// for all the `check_*` methods.
late_lint_methods!(declare_late_lint_pass, []);
impl LateLintPass<'_> for HardwiredLints {} impl LateLintPass<'_> for HardwiredLints {}
#[macro_export] #[macro_export]
macro_rules! expand_combined_late_lint_pass_method { macro_rules! expand_combined_late_lint_pass_method {
([$($passes:ident),*], $self: ident, $name: ident, $params:tt) => ({ ([$($pass:ident),*], $self: ident, $name: ident, $params:tt) => ({
$($self.$passes.$name $params;)* $($self.$pass.$name $params;)*
}) })
} }
@ -95,28 +97,28 @@ macro_rules! expand_combined_late_lint_pass_methods {
#[macro_export] #[macro_export]
macro_rules! declare_combined_late_lint_pass { macro_rules! declare_combined_late_lint_pass {
([$v:vis $name:ident, [$($passes:ident: $constructor:expr,)*]], [$hir:tt], $methods:tt) => ( ([$v:vis $name:ident, [$($pass:ident: $constructor:expr,)*]], $methods:tt) => (
#[allow(non_snake_case)] #[allow(non_snake_case)]
$v struct $name { $v struct $name {
$($passes: $passes,)* $($pass: $pass,)*
} }
impl $name { impl $name {
$v fn new() -> Self { $v fn new() -> Self {
Self { Self {
$($passes: $constructor,)* $($pass: $constructor,)*
} }
} }
$v fn get_lints() -> LintArray { $v fn get_lints() -> LintArray {
let mut lints = Vec::new(); let mut lints = Vec::new();
$(lints.extend_from_slice(&$passes::get_lints());)* $(lints.extend_from_slice(&$pass::get_lints());)*
lints lints
} }
} }
impl<'tcx> LateLintPass<'tcx> for $name { impl<'tcx> LateLintPass<'tcx> for $name {
expand_combined_late_lint_pass_methods!([$($passes),*], $methods); expand_combined_late_lint_pass_methods!([$($pass),*], $methods);
} }
#[allow(rustc::lint_pass_impl_without_macro)] #[allow(rustc::lint_pass_impl_without_macro)]
@ -176,12 +178,14 @@ macro_rules! declare_early_lint_pass {
) )
} }
// Declare the `EarlyLintPass` trait, which contains empty default definitions
// for all the `check_*` methods.
early_lint_methods!(declare_early_lint_pass, []); early_lint_methods!(declare_early_lint_pass, []);
#[macro_export] #[macro_export]
macro_rules! expand_combined_early_lint_pass_method { macro_rules! expand_combined_early_lint_pass_method {
([$($passes:ident),*], $self: ident, $name: ident, $params:tt) => ({ ([$($pass:ident),*], $self: ident, $name: ident, $params:tt) => ({
$($self.$passes.$name $params;)* $($self.$pass.$name $params;)*
}) })
} }
@ -196,28 +200,28 @@ macro_rules! expand_combined_early_lint_pass_methods {
#[macro_export] #[macro_export]
macro_rules! declare_combined_early_lint_pass { macro_rules! declare_combined_early_lint_pass {
([$v:vis $name:ident, [$($passes:ident: $constructor:expr,)*]], $methods:tt) => ( ([$v:vis $name:ident, [$($pass:ident: $constructor:expr,)*]], $methods:tt) => (
#[allow(non_snake_case)] #[allow(non_snake_case)]
$v struct $name { $v struct $name {
$($passes: $passes,)* $($pass: $pass,)*
} }
impl $name { impl $name {
$v fn new() -> Self { $v fn new() -> Self {
Self { Self {
$($passes: $constructor,)* $($pass: $constructor,)*
} }
} }
$v fn get_lints() -> LintArray { $v fn get_lints() -> LintArray {
let mut lints = Vec::new(); let mut lints = Vec::new();
$(lints.extend_from_slice(&$passes::get_lints());)* $(lints.extend_from_slice(&$pass::get_lints());)*
lints lints
} }
} }
impl EarlyLintPass for $name { impl EarlyLintPass for $name {
expand_combined_early_lint_pass_methods!([$($passes),*], $methods); expand_combined_early_lint_pass_methods!([$($pass),*], $methods);
} }
#[allow(rustc::lint_pass_impl_without_macro)] #[allow(rustc::lint_pass_impl_without_macro)]

View file

@ -949,6 +949,7 @@ impl UnusedParens {
} }
impl EarlyLintPass for UnusedParens { impl EarlyLintPass for UnusedParens {
#[inline]
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) { fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
match e.kind { match e.kind {
ExprKind::Let(ref pat, _, _) | ExprKind::ForLoop(ref pat, ..) => { ExprKind::Let(ref pat, _, _) | ExprKind::ForLoop(ref pat, ..) => {
@ -1167,6 +1168,7 @@ impl EarlyLintPass for UnusedBraces {
<Self as UnusedDelimLint>::check_stmt(self, cx, s) <Self as UnusedDelimLint>::check_stmt(self, cx, s)
} }
#[inline]
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) { fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
<Self as UnusedDelimLint>::check_expr(self, cx, e); <Self as UnusedDelimLint>::check_expr(self, cx, e);