Skip to content

Commit c6fcdb6

Browse files
committed
Auto merge of #105416 - nnethercote:more-linting-tweaks, r=cjgillot
More linting tweaks Squeeze a little more blood from this stone. r? `@cjgillot`
2 parents 32da230 + d049be3 commit c6fcdb6

File tree

6 files changed

+80
-58
lines changed

6 files changed

+80
-58
lines changed

compiler/rustc_lint/src/builtin.rs

+3
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ fn pierce_parens(mut expr: &ast::Expr) -> &ast::Expr {
9797
}
9898

9999
impl EarlyLintPass for WhileTrue {
100+
#[inline]
100101
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
101102
if let ast::ExprKind::While(cond, _, label) = &e.kind
102103
&& let cond = pierce_parens(cond)
@@ -361,6 +362,7 @@ impl EarlyLintPass for UnsafeCode {
361362
}
362363
}
363364

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

585587
impl<'tcx> LateLintPass<'tcx> for MissingDoc {
588+
#[inline]
586589
fn enter_lint_attrs(&mut self, _cx: &LateContext<'_>, attrs: &[ast::Attribute]) {
587590
let doc_hidden = self.doc_hidden()
588591
|| attrs.iter().any(|attr| {

compiler/rustc_lint/src/early.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ pub struct EarlyContextAndPasses<'a> {
3737
}
3838

3939
impl<'a> EarlyContextAndPasses<'a> {
40-
fn check_id(&mut self, id: ast::NodeId) {
40+
// This always-inlined function is for the hot call site.
41+
#[inline(always)]
42+
fn inlined_check_id(&mut self, id: ast::NodeId) {
4143
for early_lint in self.context.buffered.take(id) {
4244
let BufferedEarlyLint { span, msg, node_id: _, lint_id, diagnostic } = early_lint;
4345
self.context.lookup_with_diagnostics(
@@ -50,6 +52,11 @@ impl<'a> EarlyContextAndPasses<'a> {
5052
}
5153
}
5254

55+
// This non-inlined function is for the cold call sites.
56+
fn check_id(&mut self, id: ast::NodeId) {
57+
self.inlined_check_id(id)
58+
}
59+
5360
/// Merge the lints specified by any lint attributes into the
5461
/// current lint context, call the provided function, then reset the
5562
/// lints in effect to their previous state.
@@ -61,7 +68,7 @@ impl<'a> EarlyContextAndPasses<'a> {
6168
debug!(?id);
6269
let push = self.context.builder.push(attrs, is_crate_node, None);
6370

64-
self.check_id(id);
71+
self.inlined_check_id(id);
6572
debug!("early context: enter_attrs({:?})", attrs);
6673
run_early_passes!(self, enter_lint_attrs, attrs);
6774
f(self);

compiler/rustc_lint/src/hidden_unicode_codepoints.rs

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ impl EarlyLintPass for HiddenUnicodeCodepoints {
121121
}
122122
}
123123

124+
#[inline]
124125
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
125126
// byte strings are already handled well enough by `EscapeError::NonAsciiCharInByteString`
126127
match &expr.kind {

compiler/rustc_lint/src/lib.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ fn lint_mod(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
127127
late::late_lint_mod(tcx, module_def_id, BuiltinCombinedModuleLateLintPass::new());
128128
}
129129

130+
// See the comment on `BuiltinCombinedEarlyLintPass`, which is similar.
130131
early_lint_methods!(
131132
declare_combined_early_lint_pass,
132133
[
@@ -137,6 +138,9 @@ early_lint_methods!(
137138
]
138139
);
139140

141+
// Declare `BuiltinCombinedEarlyLintPass`, a lint pass that combines multiple
142+
// lint passes into a single pass for maximum speed. Each `check_foo` method
143+
// within this pass simply calls `check_foo` once per listed lint.
140144
early_lint_methods!(
141145
declare_combined_early_lint_pass,
142146
[
@@ -162,7 +166,9 @@ early_lint_methods!(
162166
]
163167
);
164168

165-
// FIXME: Make a separate lint type which do not require typeck tables
169+
// FIXME: Make a separate lint type which does not require typeck tables.
170+
171+
// See the comment on `BuiltinCombinedEarlyLintPass`, which is similar.
166172
late_lint_methods!(
167173
declare_combined_late_lint_pass,
168174
[
@@ -179,10 +185,10 @@ late_lint_methods!(
179185
// Keeps a global list of foreign declarations.
180186
ClashingExternDeclarations: ClashingExternDeclarations::new(),
181187
]
182-
],
183-
['tcx]
188+
]
184189
);
185190

191+
// See the comment on `BuiltinCombinedEarlyLintPass`, which is similar.
186192
late_lint_methods!(
187193
declare_combined_late_lint_pass,
188194
[
@@ -229,8 +235,7 @@ late_lint_methods!(
229235
NamedAsmLabels: NamedAsmLabels,
230236
OpaqueHiddenInferredBound: OpaqueHiddenInferredBound,
231237
]
232-
],
233-
['tcx]
238+
]
234239
);
235240

236241
pub fn new_lint_store(internal_lints: bool) -> LintStore {

compiler/rustc_lint/src/passes.rs

+55-51
Original file line numberDiff line numberDiff line change
@@ -9,49 +9,49 @@ use rustc_span::Span;
99

1010
#[macro_export]
1111
macro_rules! late_lint_methods {
12-
($macro:path, $args:tt, [$hir:tt]) => (
13-
$macro!($args, [$hir], [
14-
fn check_body(a: &$hir hir::Body<$hir>);
15-
fn check_body_post(a: &$hir hir::Body<$hir>);
12+
($macro:path, $args:tt) => (
13+
$macro!($args, [
14+
fn check_body(a: &'tcx hir::Body<'tcx>);
15+
fn check_body_post(a: &'tcx hir::Body<'tcx>);
1616
fn check_crate();
1717
fn check_crate_post();
18-
fn check_mod(a: &$hir hir::Mod<$hir>, b: hir::HirId);
19-
fn check_foreign_item(a: &$hir hir::ForeignItem<$hir>);
20-
fn check_item(a: &$hir hir::Item<$hir>);
21-
fn check_item_post(a: &$hir hir::Item<$hir>);
22-
fn check_local(a: &$hir hir::Local<$hir>);
23-
fn check_block(a: &$hir hir::Block<$hir>);
24-
fn check_block_post(a: &$hir hir::Block<$hir>);
25-
fn check_stmt(a: &$hir hir::Stmt<$hir>);
26-
fn check_arm(a: &$hir hir::Arm<$hir>);
27-
fn check_pat(a: &$hir hir::Pat<$hir>);
28-
fn check_expr(a: &$hir hir::Expr<$hir>);
29-
fn check_expr_post(a: &$hir hir::Expr<$hir>);
30-
fn check_ty(a: &$hir hir::Ty<$hir>);
31-
fn check_generic_param(a: &$hir hir::GenericParam<$hir>);
32-
fn check_generics(a: &$hir hir::Generics<$hir>);
33-
fn check_poly_trait_ref(a: &$hir hir::PolyTraitRef<$hir>);
18+
fn check_mod(a: &'tcx hir::Mod<'tcx>, b: hir::HirId);
19+
fn check_foreign_item(a: &'tcx hir::ForeignItem<'tcx>);
20+
fn check_item(a: &'tcx hir::Item<'tcx>);
21+
fn check_item_post(a: &'tcx hir::Item<'tcx>);
22+
fn check_local(a: &'tcx hir::Local<'tcx>);
23+
fn check_block(a: &'tcx hir::Block<'tcx>);
24+
fn check_block_post(a: &'tcx hir::Block<'tcx>);
25+
fn check_stmt(a: &'tcx hir::Stmt<'tcx>);
26+
fn check_arm(a: &'tcx hir::Arm<'tcx>);
27+
fn check_pat(a: &'tcx hir::Pat<'tcx>);
28+
fn check_expr(a: &'tcx hir::Expr<'tcx>);
29+
fn check_expr_post(a: &'tcx hir::Expr<'tcx>);
30+
fn check_ty(a: &'tcx hir::Ty<'tcx>);
31+
fn check_generic_param(a: &'tcx hir::GenericParam<'tcx>);
32+
fn check_generics(a: &'tcx hir::Generics<'tcx>);
33+
fn check_poly_trait_ref(a: &'tcx hir::PolyTraitRef<'tcx>);
3434
fn check_fn(
35-
a: rustc_hir::intravisit::FnKind<$hir>,
36-
b: &$hir hir::FnDecl<$hir>,
37-
c: &$hir hir::Body<$hir>,
35+
a: rustc_hir::intravisit::FnKind<'tcx>,
36+
b: &'tcx hir::FnDecl<'tcx>,
37+
c: &'tcx hir::Body<'tcx>,
3838
d: Span,
3939
e: hir::HirId);
40-
fn check_trait_item(a: &$hir hir::TraitItem<$hir>);
41-
fn check_impl_item(a: &$hir hir::ImplItem<$hir>);
42-
fn check_impl_item_post(a: &$hir hir::ImplItem<$hir>);
43-
fn check_struct_def(a: &$hir hir::VariantData<$hir>);
44-
fn check_field_def(a: &$hir hir::FieldDef<$hir>);
45-
fn check_variant(a: &$hir hir::Variant<$hir>);
46-
fn check_path(a: &hir::Path<$hir>, b: hir::HirId);
47-
fn check_attribute(a: &$hir ast::Attribute);
40+
fn check_trait_item(a: &'tcx hir::TraitItem<'tcx>);
41+
fn check_impl_item(a: &'tcx hir::ImplItem<'tcx>);
42+
fn check_impl_item_post(a: &'tcx hir::ImplItem<'tcx>);
43+
fn check_struct_def(a: &'tcx hir::VariantData<'tcx>);
44+
fn check_field_def(a: &'tcx hir::FieldDef<'tcx>);
45+
fn check_variant(a: &'tcx hir::Variant<'tcx>);
46+
fn check_path(a: &hir::Path<'tcx>, b: hir::HirId);
47+
fn check_attribute(a: &'tcx ast::Attribute);
4848

4949
/// Called when entering a syntax node that can have lint attributes such
5050
/// as `#[allow(...)]`. Called with *all* the attributes of that node.
51-
fn enter_lint_attrs(a: &$hir [ast::Attribute]);
51+
fn enter_lint_attrs(a: &'tcx [ast::Attribute]);
5252

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

6868
macro_rules! declare_late_lint_pass {
69-
([], [$hir:tt], [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
70-
pub trait LateLintPass<$hir>: LintPass {
71-
$(#[inline(always)] fn $name(&mut self, _: &LateContext<$hir>, $(_: $arg),*) {})*
69+
([], [$($(#[$attr:meta])* fn $name:ident($($param:ident: $arg:ty),*);)*]) => (
70+
pub trait LateLintPass<'tcx>: LintPass {
71+
$(#[inline(always)] fn $name(&mut self, _: &LateContext<'tcx>, $(_: $arg),*) {})*
7272
}
7373
)
7474
}
7575

76-
late_lint_methods!(declare_late_lint_pass, [], ['tcx]);
76+
// Declare the `LateLintPass` trait, which contains empty default definitions
77+
// for all the `check_*` methods.
78+
late_lint_methods!(declare_late_lint_pass, []);
7779

7880
impl LateLintPass<'_> for HardwiredLints {}
7981

8082
#[macro_export]
8183
macro_rules! expand_combined_late_lint_pass_method {
82-
([$($passes:ident),*], $self: ident, $name: ident, $params:tt) => ({
83-
$($self.$passes.$name $params;)*
84+
([$($pass:ident),*], $self: ident, $name: ident, $params:tt) => ({
85+
$($self.$pass.$name $params;)*
8486
})
8587
}
8688

@@ -95,28 +97,28 @@ macro_rules! expand_combined_late_lint_pass_methods {
9597

9698
#[macro_export]
9799
macro_rules! declare_combined_late_lint_pass {
98-
([$v:vis $name:ident, [$($passes:ident: $constructor:expr,)*]], [$hir:tt], $methods:tt) => (
100+
([$v:vis $name:ident, [$($pass:ident: $constructor:expr,)*]], $methods:tt) => (
99101
#[allow(non_snake_case)]
100102
$v struct $name {
101-
$($passes: $passes,)*
103+
$($pass: $pass,)*
102104
}
103105

104106
impl $name {
105107
$v fn new() -> Self {
106108
Self {
107-
$($passes: $constructor,)*
109+
$($pass: $constructor,)*
108110
}
109111
}
110112

111113
$v fn get_lints() -> LintArray {
112114
let mut lints = Vec::new();
113-
$(lints.extend_from_slice(&$passes::get_lints());)*
115+
$(lints.extend_from_slice(&$pass::get_lints());)*
114116
lints
115117
}
116118
}
117119

118120
impl<'tcx> LateLintPass<'tcx> for $name {
119-
expand_combined_late_lint_pass_methods!([$($passes),*], $methods);
121+
expand_combined_late_lint_pass_methods!([$($pass),*], $methods);
120122
}
121123

122124
#[allow(rustc::lint_pass_impl_without_macro)]
@@ -176,12 +178,14 @@ macro_rules! declare_early_lint_pass {
176178
)
177179
}
178180

181+
// Declare the `EarlyLintPass` trait, which contains empty default definitions
182+
// for all the `check_*` methods.
179183
early_lint_methods!(declare_early_lint_pass, []);
180184

181185
#[macro_export]
182186
macro_rules! expand_combined_early_lint_pass_method {
183-
([$($passes:ident),*], $self: ident, $name: ident, $params:tt) => ({
184-
$($self.$passes.$name $params;)*
187+
([$($pass:ident),*], $self: ident, $name: ident, $params:tt) => ({
188+
$($self.$pass.$name $params;)*
185189
})
186190
}
187191

@@ -196,28 +200,28 @@ macro_rules! expand_combined_early_lint_pass_methods {
196200

197201
#[macro_export]
198202
macro_rules! declare_combined_early_lint_pass {
199-
([$v:vis $name:ident, [$($passes:ident: $constructor:expr,)*]], $methods:tt) => (
203+
([$v:vis $name:ident, [$($pass:ident: $constructor:expr,)*]], $methods:tt) => (
200204
#[allow(non_snake_case)]
201205
$v struct $name {
202-
$($passes: $passes,)*
206+
$($pass: $pass,)*
203207
}
204208

205209
impl $name {
206210
$v fn new() -> Self {
207211
Self {
208-
$($passes: $constructor,)*
212+
$($pass: $constructor,)*
209213
}
210214
}
211215

212216
$v fn get_lints() -> LintArray {
213217
let mut lints = Vec::new();
214-
$(lints.extend_from_slice(&$passes::get_lints());)*
218+
$(lints.extend_from_slice(&$pass::get_lints());)*
215219
lints
216220
}
217221
}
218222

219223
impl EarlyLintPass for $name {
220-
expand_combined_early_lint_pass_methods!([$($passes),*], $methods);
224+
expand_combined_early_lint_pass_methods!([$($pass),*], $methods);
221225
}
222226

223227
#[allow(rustc::lint_pass_impl_without_macro)]

compiler/rustc_lint/src/unused.rs

+2
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,7 @@ impl UnusedParens {
949949
}
950950

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

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

0 commit comments

Comments
 (0)