1
1
use super :: TRANSMUTE_INT_TO_NON_ZERO ;
2
- use clippy_utils:: diagnostics:: span_lint_and_then ;
3
- use clippy_utils:: sugg;
2
+ use clippy_utils:: diagnostics:: span_lint_and_sugg ;
3
+ use clippy_utils:: sugg:: Sugg ;
4
4
use rustc_errors:: Applicability ;
5
5
use rustc_hir:: Expr ;
6
6
use rustc_lint:: LateContext ;
@@ -16,35 +16,24 @@ pub(super) fn check<'tcx>(
16
16
to_ty : Ty < ' tcx > ,
17
17
arg : & ' tcx Expr < ' _ > ,
18
18
) -> bool {
19
- let tcx = cx. tcx ;
20
-
21
- let ( ty:: Int ( _) | ty:: Uint ( _) , ty:: Adt ( adt, substs) ) = ( & from_ty. kind ( ) , to_ty. kind ( ) ) else {
22
- return false ;
23
- } ;
24
-
25
- if !tcx. is_diagnostic_item ( sym:: NonZero , adt. did ( ) ) {
26
- return false ;
19
+ if let ty:: Int ( _) | ty:: Uint ( _) = from_ty. kind ( )
20
+ && let ty:: Adt ( adt, substs) = to_ty. kind ( )
21
+ && cx. tcx . is_diagnostic_item ( sym:: NonZero , adt. did ( ) )
22
+ && let int_ty = substs. type_at ( 0 )
23
+ && from_ty == int_ty
24
+ {
25
+ let arg = Sugg :: hir ( cx, arg, ".." ) ;
26
+ span_lint_and_sugg (
27
+ cx,
28
+ TRANSMUTE_INT_TO_NON_ZERO ,
29
+ e. span ,
30
+ format ! ( "transmute from a `{from_ty}` to a `{}<{int_ty}>`" , sym:: NonZero ) ,
31
+ "consider using" ,
32
+ format ! ( "{}::{}({arg})" , sym:: NonZero , sym:: new_unchecked) ,
33
+ Applicability :: Unspecified ,
34
+ ) ;
35
+ true
36
+ } else {
37
+ false
27
38
}
28
-
29
- let int_ty = substs. type_at ( 0 ) ;
30
- if from_ty != int_ty {
31
- return false ;
32
- }
33
-
34
- span_lint_and_then (
35
- cx,
36
- TRANSMUTE_INT_TO_NON_ZERO ,
37
- e. span ,
38
- format ! ( "transmute from a `{from_ty}` to a `{}<{int_ty}>`" , sym:: NonZero ) ,
39
- |diag| {
40
- let arg = sugg:: Sugg :: hir ( cx, arg, ".." ) ;
41
- diag. span_suggestion (
42
- e. span ,
43
- "consider using" ,
44
- format ! ( "{}::{}({arg})" , sym:: NonZero , sym:: new_unchecked) ,
45
- Applicability :: Unspecified ,
46
- ) ;
47
- } ,
48
- ) ;
49
- true
50
39
}
0 commit comments