Skip to content

Commit 3edee2b

Browse files
committed
Emit a lint for small functions without #[inline]
1 parent f81d6f0 commit 3edee2b

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

compiler/rustc_builtin_macros/src/deriving/debug.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub fn expand_deriving_debug(
3333
explicit_self: true,
3434
nonself_args: vec![(fmtr, sym::f)],
3535
ret_ty: Path(path_std!(fmt::Result)),
36-
attributes: ast::AttrVec::new(),
36+
attributes: thin_vec![cx.attr_word(sym::inline, span)],
3737
fieldless_variants_strategy:
3838
FieldlessVariantsStrategy::SpecializeIfAllVariantsFieldless,
3939
combine_substructure: combine_substructure(Box::new(|a, b, c| {

compiler/rustc_mir_transform/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ mir_transform_requires_unsafe = {$details} is unsafe and requires unsafe {$op_in
4242
}
4343
.not_inherited = items do not inherit unsafety from separate enclosing items
4444
45+
mir_transform_small_fn_without_inline = this function looks small ({$statements}) but doesn't have #[inline], consider adding it
46+
.suggestion = add the inline attribute
47+
4548
mir_transform_target_feature_call_label = call to function with `#[target_feature]`
4649
mir_transform_target_feature_call_note = can only be called if the required target features are available
4750

compiler/rustc_mir_transform/src/cross_crate_inline.rs

+34-5
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,37 @@ fn cross_crate_inlinable(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
5555
}
5656

5757
let mir = tcx.optimized_mir(def_id);
58-
let mut checker =
59-
CostChecker { tcx, callee_body: mir, calls: 0, statements: 0, landing_pads: 0, resumes: 0 };
58+
let mut checker = CostChecker {
59+
tcx,
60+
callee_body: mir,
61+
calls: 0,
62+
statements: 0,
63+
landing_pads: 0,
64+
resumes: 0,
65+
branches: 0,
66+
asserts: 0,
67+
};
6068
checker.visit_body(mir);
61-
checker.calls == 0
69+
let is_leaf = checker.calls == 0
6270
&& checker.resumes == 0
6371
&& checker.landing_pads == 0
6472
&& checker.statements
65-
<= tcx.sess.opts.unstable_opts.cross_crate_inline_threshold.unwrap_or(100)
73+
<= tcx.sess.opts.unstable_opts.cross_crate_inline_threshold.unwrap_or(100);
74+
75+
let is_trivial_wrapper = checker.calls == 1
76+
&& checker.resumes == 0
77+
&& checker.landing_pads == 0
78+
&& mir.basic_blocks.len() == 2;
79+
80+
if is_trivial_wrapper {
81+
let span = tcx.def_span(def_id);
82+
tcx.sess.emit_warning(crate::errors::SuggestAddingInline {
83+
place: span,
84+
suggest_inline: span.with_hi(span.lo()),
85+
statements: checker.statements,
86+
});
87+
}
88+
is_leaf
6689
}
6790

6891
struct CostChecker<'b, 'tcx> {
@@ -72,6 +95,8 @@ struct CostChecker<'b, 'tcx> {
7295
statements: usize,
7396
landing_pads: usize,
7497
resumes: usize,
98+
branches: usize,
99+
asserts: usize,
75100
}
76101

77102
impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
@@ -105,7 +130,7 @@ impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
105130
}
106131
}
107132
TerminatorKind::Assert { unwind, .. } => {
108-
self.calls += 1;
133+
self.asserts += 1;
109134
if let UnwindAction::Cleanup(_) = unwind {
110135
self.landing_pads += 1;
111136
}
@@ -117,6 +142,10 @@ impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
117142
self.landing_pads += 1;
118143
}
119144
}
145+
TerminatorKind::SwitchInt { .. } => {
146+
self.statements += 1;
147+
self.branches += 1;
148+
}
120149
TerminatorKind::Return => {}
121150
_ => self.statements += 1,
122151
}

compiler/rustc_mir_transform/src/errors.rs

+10
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,13 @@ pub(crate) struct MustNotSuspendReason {
278278
pub span: Span,
279279
pub reason: String,
280280
}
281+
282+
#[derive(Diagnostic)]
283+
#[diag(mir_transform_small_fn_without_inline)]
284+
pub struct SuggestAddingInline {
285+
#[primary_span]
286+
pub place: Span,
287+
#[suggestion(code = "#[inline]\n", applicability = "machine-applicable")]
288+
pub suggest_inline: Span,
289+
pub statements: usize,
290+
}

0 commit comments

Comments
 (0)