Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More linting tweaks #105416

Merged
merged 5 commits into from
Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ fn pierce_parens(mut expr: &ast::Expr) -> &ast::Expr {
}

impl EarlyLintPass for WhileTrue {
#[inline]
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
if let ast::ExprKind::While(cond, _, label) = &e.kind
&& let cond = pierce_parens(cond)
Expand Down Expand Up @@ -360,6 +361,7 @@ impl EarlyLintPass for UnsafeCode {
}
}

#[inline]
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
if let ast::ExprKind::Block(ref blk, _) = e.kind {
// Don't warn about generated blocks; that'll just pollute the output.
Expand Down Expand Up @@ -582,6 +584,7 @@ impl MissingDoc {
}

impl<'tcx> LateLintPass<'tcx> for MissingDoc {
#[inline]
fn enter_lint_attrs(&mut self, _cx: &LateContext<'_>, attrs: &[ast::Attribute]) {
let doc_hidden = self.doc_hidden()
|| attrs.iter().any(|attr| {
Expand Down
11 changes: 9 additions & 2 deletions compiler/rustc_lint/src/early.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ pub struct 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) {
let BufferedEarlyLint { span, msg, node_id: _, lint_id, diagnostic } = early_lint;
self.context.lookup_with_diagnostics(
Expand All @@ -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
/// current lint context, call the provided function, then reset the
/// lints in effect to their previous state.
Expand All @@ -61,7 +68,7 @@ impl<'a> EarlyContextAndPasses<'a> {
debug!(?id);
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);
run_early_passes!(self, enter_lint_attrs, attrs);
f(self);
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_lint/src/hidden_unicode_codepoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ impl EarlyLintPass for HiddenUnicodeCodepoints {
}
}

#[inline]
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
// byte strings are already handled well enough by `EscapeError::NonAsciiCharInByteString`
match &expr.kind {
Expand Down
15 changes: 10 additions & 5 deletions compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ fn lint_mod(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
late::late_lint_mod(tcx, module_def_id, BuiltinCombinedModuleLateLintPass::new());
}

// See the comment on `BuiltinCombinedEarlyLintPass`, which is similar.
early_lint_methods!(
declare_combined_early_lint_pass,
[
Expand All @@ -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!(
declare_combined_early_lint_pass,
[
Expand All @@ -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!(
declare_combined_late_lint_pass,
[
Expand All @@ -179,10 +185,10 @@ late_lint_methods!(
// Keeps a global list of foreign declarations.
ClashingExternDeclarations: ClashingExternDeclarations::new(),
]
],
['tcx]
]
);

// See the comment on `BuiltinCombinedEarlyLintPass`, which is similar.
late_lint_methods!(
declare_combined_late_lint_pass,
[
Expand Down Expand Up @@ -229,8 +235,7 @@ late_lint_methods!(
NamedAsmLabels: NamedAsmLabels,
OpaqueHiddenInferredBound: OpaqueHiddenInferredBound,
]
],
['tcx]
]
);

pub fn new_lint_store(internal_lints: bool) -> LintStore {
Expand Down
106 changes: 55 additions & 51 deletions compiler/rustc_lint/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,49 @@ use rustc_span::Span;

#[macro_export]
macro_rules! late_lint_methods {
($macro:path, $args:tt, [$hir:tt]) => (
$macro!($args, [$hir], [
fn check_body(a: &$hir hir::Body<$hir>);
fn check_body_post(a: &$hir hir::Body<$hir>);
($macro:path, $args:tt) => (
$macro!($args, [
fn check_body(a: &'tcx hir::Body<'tcx>);
fn check_body_post(a: &'tcx hir::Body<'tcx>);
fn check_crate();
fn check_crate_post();
fn check_mod(a: &$hir hir::Mod<$hir>, b: hir::HirId);
fn check_foreign_item(a: &$hir hir::ForeignItem<$hir>);
fn check_item(a: &$hir hir::Item<$hir>);
fn check_item_post(a: &$hir hir::Item<$hir>);
fn check_local(a: &$hir hir::Local<$hir>);
fn check_block(a: &$hir hir::Block<$hir>);
fn check_block_post(a: &$hir hir::Block<$hir>);
fn check_stmt(a: &$hir hir::Stmt<$hir>);
fn check_arm(a: &$hir hir::Arm<$hir>);
fn check_pat(a: &$hir hir::Pat<$hir>);
fn check_expr(a: &$hir hir::Expr<$hir>);
fn check_expr_post(a: &$hir hir::Expr<$hir>);
fn check_ty(a: &$hir hir::Ty<$hir>);
fn check_generic_param(a: &$hir hir::GenericParam<$hir>);
fn check_generics(a: &$hir hir::Generics<$hir>);
fn check_poly_trait_ref(a: &$hir hir::PolyTraitRef<$hir>);
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>);
fn check_fn(
a: rustc_hir::intravisit::FnKind<$hir>,
b: &$hir hir::FnDecl<$hir>,
c: &$hir hir::Body<$hir>,
a: rustc_hir::intravisit::FnKind<'tcx>,
b: &'tcx hir::FnDecl<'tcx>,
c: &'tcx hir::Body<'tcx>,
d: Span,
e: hir::HirId);
fn check_trait_item(a: &$hir hir::TraitItem<$hir>);
fn check_impl_item(a: &$hir hir::ImplItem<$hir>);
fn check_impl_item_post(a: &$hir hir::ImplItem<$hir>);
fn check_struct_def(a: &$hir hir::VariantData<$hir>);
fn check_field_def(a: &$hir hir::FieldDef<$hir>);
fn check_variant(a: &$hir hir::Variant<$hir>);
fn check_path(a: &hir::Path<$hir>, b: hir::HirId);
fn check_attribute(a: &$hir ast::Attribute);
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);

/// 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: &$hir [ast::Attribute]);
fn enter_lint_attrs(a: &'tcx [ast::Attribute]);

/// Counterpart to `enter_lint_attrs`.
fn exit_lint_attrs(a: &$hir [ast::Attribute]);
fn exit_lint_attrs(a: &'tcx [ast::Attribute]);
]);
)
}
Expand All @@ -66,21 +66,23 @@ macro_rules! late_lint_methods {
// contains a few lint-specific methods with no equivalent in `Visitor`.

macro_rules! declare_late_lint_pass {
([], [$hir:tt], [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
pub trait LateLintPass<$hir>: LintPass {
$(#[inline(always)] fn $name(&mut self, _: &LateContext<$hir>, $(_: $arg),*) {})*
([], [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
pub trait LateLintPass<'tcx>: LintPass {
$(#[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 {}

#[macro_export]
macro_rules! expand_combined_late_lint_pass_method {
([$($passes:ident),*], $self: ident, $name: ident, $params:tt) => ({
$($self.$passes.$name $params;)*
([$($pass:ident),*], $self: ident, $name: ident, $params:tt) => ({
$($self.$pass.$name $params;)*
})
}

Expand All @@ -95,28 +97,28 @@ macro_rules! expand_combined_late_lint_pass_methods {

#[macro_export]
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)]
$v struct $name {
$($passes: $passes,)*
$($pass: $pass,)*
}

impl $name {
$v fn new() -> Self {
Self {
$($passes: $constructor,)*
$($pass: $constructor,)*
}
}

$v fn get_lints() -> LintArray {
let mut lints = Vec::new();
$(lints.extend_from_slice(&$passes::get_lints());)*
$(lints.extend_from_slice(&$pass::get_lints());)*
lints
}
}

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)]
Expand Down Expand Up @@ -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, []);

#[macro_export]
macro_rules! expand_combined_early_lint_pass_method {
([$($passes:ident),*], $self: ident, $name: ident, $params:tt) => ({
$($self.$passes.$name $params;)*
([$($pass:ident),*], $self: ident, $name: ident, $params:tt) => ({
$($self.$pass.$name $params;)*
})
}

Expand All @@ -196,28 +200,28 @@ macro_rules! expand_combined_early_lint_pass_methods {

#[macro_export]
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)]
$v struct $name {
$($passes: $passes,)*
$($pass: $pass,)*
}

impl $name {
$v fn new() -> Self {
Self {
$($passes: $constructor,)*
$($pass: $constructor,)*
}
}

$v fn get_lints() -> LintArray {
let mut lints = Vec::new();
$(lints.extend_from_slice(&$passes::get_lints());)*
$(lints.extend_from_slice(&$pass::get_lints());)*
lints
}
}

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)]
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_lint/src/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,7 @@ impl UnusedParens {
}

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

#[inline]
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
<Self as UnusedDelimLint>::check_expr(self, cx, e);

Expand Down