@@ -67,7 +67,7 @@ impl<'tcx> LateLintPass<'tcx> for DefaultNumericFallback {
67
67
68
68
struct NumericFallbackVisitor < ' a , ' tcx > {
69
69
/// Stack manages type bound of exprs. The top element holds current expr type.
70
- ty_bounds : Vec < TyBound < ' tcx > > ,
70
+ ty_bounds : Vec < ExplicitTyBound > ,
71
71
72
72
cx : & ' a LateContext < ' tcx > ,
73
73
}
@@ -76,9 +76,9 @@ impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> {
76
76
fn new ( cx : & ' a LateContext < ' tcx > , is_parent_const : bool ) -> Self {
77
77
Self {
78
78
ty_bounds : vec ! [ if is_parent_const {
79
- TyBound :: Any
79
+ ExplicitTyBound ( true )
80
80
} else {
81
- TyBound :: Nothing
81
+ ExplicitTyBound ( false )
82
82
} ] ,
83
83
cx,
84
84
}
@@ -88,10 +88,10 @@ impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> {
88
88
fn check_lit ( & self , lit : & Lit , lit_ty : Ty < ' tcx > , emit_hir_id : HirId ) {
89
89
if_chain ! {
90
90
if !in_external_macro( self . cx. sess( ) , lit. span) ;
91
- if let Some ( ty_bound ) = self . ty_bounds. last( ) ;
91
+ if let Some ( explicit_ty_bounds ) = self . ty_bounds. last( ) ;
92
92
if matches!( lit. node,
93
93
LitKind :: Int ( _, LitIntType :: Unsuffixed ) | LitKind :: Float ( _, LitFloatType :: Unsuffixed ) ) ;
94
- if !ty_bound . is_numeric ( ) ;
94
+ if !explicit_ty_bounds . 0 ;
95
95
then {
96
96
let ( suffix, is_float) = match lit_ty. kind( ) {
97
97
ty:: Int ( IntTy :: I32 ) => ( "i32" , false ) ,
@@ -132,7 +132,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> {
132
132
if let Some ( fn_sig) = fn_sig_opt ( self . cx , func. hir_id ) {
133
133
for ( expr, bound) in iter:: zip ( * args, fn_sig. skip_binder ( ) . inputs ( ) ) {
134
134
// Push found arg type, then visit arg.
135
- self . ty_bounds . push ( TyBound :: Ty ( * bound) ) ;
135
+ self . ty_bounds . push ( ( * bound) . into ( ) ) ;
136
136
self . visit_expr ( expr) ;
137
137
self . ty_bounds . pop ( ) ;
138
138
}
@@ -144,7 +144,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> {
144
144
if let Some ( def_id) = self . cx . typeck_results ( ) . type_dependent_def_id ( expr. hir_id ) {
145
145
let fn_sig = self . cx . tcx . fn_sig ( def_id) . skip_binder ( ) ;
146
146
for ( expr, bound) in iter:: zip ( std:: iter:: once ( * receiver) . chain ( args. iter ( ) ) , fn_sig. inputs ( ) ) {
147
- self . ty_bounds . push ( TyBound :: Ty ( * bound) ) ;
147
+ self . ty_bounds . push ( ( * bound) . into ( ) ) ;
148
148
self . visit_expr ( expr) ;
149
149
self . ty_bounds . pop ( ) ;
150
150
}
@@ -178,7 +178,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> {
178
178
179
179
// Visit base with no bound.
180
180
if let Some ( base) = base {
181
- self . ty_bounds. push( TyBound :: Nothing ) ;
181
+ self . ty_bounds. push( ExplicitTyBound ( false ) ) ;
182
182
self . visit_expr( base) ;
183
183
self . ty_bounds. pop( ) ;
184
184
}
@@ -201,9 +201,10 @@ impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> {
201
201
202
202
fn visit_stmt ( & mut self , stmt : & ' tcx Stmt < ' _ > ) {
203
203
match stmt. kind {
204
- StmtKind :: Local ( local) if local. ty . is_some ( ) => self . ty_bounds . push ( TyBound :: Any ) ,
204
+ // we cannot check the exact type since it's a hir::Ty which does not implement `is_numeric`
205
+ StmtKind :: Local ( local) => self . ty_bounds . push ( ExplicitTyBound ( local. ty . is_some ( ) ) ) ,
205
206
206
- _ => self . ty_bounds . push ( TyBound :: Nothing ) ,
207
+ _ => self . ty_bounds . push ( ExplicitTyBound ( false ) ) ,
207
208
}
208
209
209
210
walk_stmt ( self , stmt) ;
@@ -221,28 +222,18 @@ fn fn_sig_opt<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Option<PolyFnSig<'
221
222
}
222
223
}
223
224
225
+ /// Wrapper around a `bool` to make the meaning of the value clearer
224
226
#[ derive( Debug , Clone , Copy ) ]
225
- enum TyBound < ' tcx > {
226
- Any ,
227
- Ty ( Ty < ' tcx > ) ,
228
- Nothing ,
229
- }
227
+ struct ExplicitTyBound ( pub bool ) ;
230
228
231
- impl < ' tcx > TyBound < ' tcx > {
232
- fn is_numeric ( self ) -> bool {
233
- match self {
234
- TyBound :: Any => true ,
235
- TyBound :: Ty ( t) => t. is_numeric ( ) ,
236
- TyBound :: Nothing => false ,
237
- }
229
+ impl < ' tcx > From < Ty < ' tcx > > for ExplicitTyBound {
230
+ fn from ( v : Ty < ' tcx > ) -> Self {
231
+ Self ( v. is_numeric ( ) )
238
232
}
239
233
}
240
234
241
- impl < ' tcx > From < Option < Ty < ' tcx > > > for TyBound < ' tcx > {
235
+ impl < ' tcx > From < Option < Ty < ' tcx > > > for ExplicitTyBound {
242
236
fn from ( v : Option < Ty < ' tcx > > ) -> Self {
243
- match v {
244
- Some ( t) => TyBound :: Ty ( t) ,
245
- None => TyBound :: Nothing ,
246
- }
237
+ Self ( v. map_or ( false , Ty :: is_numeric) )
247
238
}
248
239
}
0 commit comments