@@ -3,11 +3,10 @@ use clippy_utils::is_in_const_context;
3
3
use clippy_utils:: msrvs:: { self , Msrv } ;
4
4
use clippy_utils:: source:: SpanRangeExt ;
5
5
use clippy_utils:: sugg:: Sugg ;
6
- use clippy_utils:: ty:: is_isize_or_usize;
7
6
use rustc_errors:: Applicability ;
8
7
use rustc_hir:: { Expr , QPath , TyKind } ;
9
8
use rustc_lint:: LateContext ;
10
- use rustc_middle:: ty:: { self , FloatTy , Ty } ;
9
+ use rustc_middle:: ty:: { self , FloatTy , IntTy , Ty , UintTy } ;
11
10
use rustc_span:: hygiene;
12
11
13
12
use super :: { CAST_LOSSLESS , utils} ;
@@ -76,29 +75,21 @@ fn should_lint(cx: &LateContext<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>, msrv: M
76
75
return false ;
77
76
}
78
77
79
- match ( cast_from. is_integral ( ) , cast_to. is_integral ( ) ) {
80
- ( true , true ) => {
81
- let cast_signed_to_unsigned = cast_from. is_signed ( ) && !cast_to. is_signed ( ) ;
82
- let from_nbits = utils:: int_ty_to_nbits ( cast_from, cx. tcx ) ;
83
- let to_nbits = utils:: int_ty_to_nbits ( cast_to, cx. tcx ) ;
84
- !is_isize_or_usize ( cast_from)
85
- && !is_isize_or_usize ( cast_to)
86
- && from_nbits < to_nbits
87
- && !cast_signed_to_unsigned
78
+ match ( cast_from. kind ( ) , cast_to. kind ( ) ) {
79
+ ( ty:: Bool , ty:: Uint ( _) | ty:: Int ( _) ) => msrv. meets ( cx, msrvs:: FROM_BOOL ) ,
80
+ ( ty:: Uint ( _) , ty:: Uint ( UintTy :: Usize ) ) | ( ty:: Uint ( UintTy :: U8 ) | ty:: Int ( _) , ty:: Int ( IntTy :: Isize ) ) => {
81
+ utils:: int_ty_to_nbits ( cast_from, cx. tcx ) <= 16
88
82
} ,
89
-
90
- ( true , false ) => {
91
- let from_nbits = utils:: int_ty_to_nbits ( cast_from, cx. tcx ) ;
92
- let to_nbits = if let ty:: Float ( FloatTy :: F32 ) = cast_to. kind ( ) {
93
- 32
94
- } else {
95
- 64
96
- } ;
97
- !is_isize_or_usize ( cast_from) && from_nbits < to_nbits
98
- } ,
99
- ( false , true ) if matches ! ( cast_from. kind( ) , ty:: Bool ) && msrv. meets ( cx, msrvs:: FROM_BOOL ) => true ,
100
- ( _, _) => {
101
- matches ! ( cast_from. kind( ) , ty:: Float ( FloatTy :: F32 ) ) && matches ! ( cast_to. kind( ) , ty:: Float ( FloatTy :: F64 ) )
83
+ // No `f16` to `f32`: https://github.com/rust-lang/rust/issues/123831
84
+ ( ty:: Uint ( UintTy :: Usize ) | ty:: Int ( IntTy :: Isize ) , _)
85
+ | ( _, ty:: Uint ( UintTy :: Usize ) | ty:: Int ( IntTy :: Isize ) )
86
+ | ( ty:: Float ( FloatTy :: F16 ) , ty:: Float ( FloatTy :: F32 ) ) => false ,
87
+ ( ty:: Uint ( _) | ty:: Int ( _) , ty:: Int ( _) ) | ( ty:: Uint ( _) , ty:: Uint ( _) ) => {
88
+ utils:: int_ty_to_nbits ( cast_from, cx. tcx ) < utils:: int_ty_to_nbits ( cast_to, cx. tcx )
102
89
} ,
90
+ ( ty:: Uint ( _) | ty:: Int ( _) , ty:: Float ( fl) ) => utils:: int_ty_to_nbits ( cast_from, cx. tcx ) < fl. bit_width ( ) ,
91
+ ( ty:: Char , ty:: Uint ( _) ) => utils:: int_ty_to_nbits ( cast_to, cx. tcx ) >= 32 ,
92
+ ( ty:: Float ( fl_from) , ty:: Float ( fl_to) ) => fl_from. bit_width ( ) < fl_to. bit_width ( ) ,
93
+ _ => false ,
103
94
}
104
95
}
0 commit comments