@@ -62,13 +62,36 @@ fn cross_crate_inlinable(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
6262 } ;
6363
6464 let mir = tcx. optimized_mir ( def_id) ;
65- let mut checker =
66- CostChecker { tcx, callee_body : mir, calls : 0 , statements : 0 , landing_pads : 0 , resumes : 0 } ;
65+ let mut checker = CostChecker {
66+ tcx,
67+ callee_body : mir,
68+ calls : 0 ,
69+ statements : 0 ,
70+ landing_pads : 0 ,
71+ resumes : 0 ,
72+ branches : 0 ,
73+ asserts : 0 ,
74+ } ;
6775 checker. visit_body ( mir) ;
68- checker. calls == 0
76+ let is_leaf = checker. calls == 0
6977 && checker. resumes == 0
7078 && checker. landing_pads == 0
71- && checker. statements <= threshold
79+ && checker. statements <= threshold;
80+
81+ let is_trivial_wrapper = checker. calls == 1
82+ && checker. resumes == 0
83+ && checker. landing_pads == 0
84+ && mir. basic_blocks . len ( ) == 2 ;
85+
86+ if is_trivial_wrapper {
87+ let span = tcx. def_span ( def_id) ;
88+ tcx. sess . emit_warning ( crate :: errors:: SuggestAddingInline {
89+ place : span,
90+ suggest_inline : span. with_hi ( span. lo ( ) ) ,
91+ statements : checker. statements ,
92+ } ) ;
93+ }
94+ is_leaf
7295}
7396
7497struct CostChecker < ' b , ' tcx > {
@@ -78,6 +101,8 @@ struct CostChecker<'b, 'tcx> {
78101 statements : usize ,
79102 landing_pads : usize ,
80103 resumes : usize ,
104+ branches : usize ,
105+ asserts : usize ,
81106}
82107
83108impl < ' tcx > Visitor < ' tcx > for CostChecker < ' _ , ' tcx > {
@@ -111,7 +136,7 @@ impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
111136 }
112137 }
113138 TerminatorKind :: Assert { unwind, .. } => {
114- self . calls += 1 ;
139+ self . asserts += 1 ;
115140 if let UnwindAction :: Cleanup ( _) = unwind {
116141 self . landing_pads += 1 ;
117142 }
@@ -123,6 +148,10 @@ impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
123148 self . landing_pads += 1 ;
124149 }
125150 }
151+ TerminatorKind :: SwitchInt { .. } => {
152+ self . statements += 1 ;
153+ self . branches += 1 ;
154+ }
126155 TerminatorKind :: Return => { }
127156 _ => self . statements += 1 ,
128157 }
0 commit comments