@@ -18,70 +18,67 @@ pub(super) fn check<'tcx>(
18
18
) -> bool {
19
19
let mut triggered = false ;
20
20
21
- match ( & from_ty. kind ( ) , & to_ty. kind ( ) ) {
22
- ( ty:: Ref ( _, ty_from, from_mutbl) , ty:: Ref ( _, ty_to, to_mutbl) ) => {
23
- if_chain ! {
24
- if let ( & ty:: Slice ( slice_ty) , & ty:: Str ) = ( & ty_from. kind( ) , & ty_to. kind( ) ) ;
25
- if let ty:: Uint ( ty:: UintTy :: U8 ) = slice_ty. kind( ) ;
26
- if from_mutbl == to_mutbl;
27
- then {
28
- let postfix = if * from_mutbl == Mutability :: Mut {
29
- "_mut"
30
- } else {
31
- ""
32
- } ;
21
+ if let ( ty:: Ref ( _, ty_from, from_mutbl) , ty:: Ref ( _, ty_to, to_mutbl) ) = ( & from_ty. kind ( ) , & to_ty. kind ( ) ) {
22
+ if_chain ! {
23
+ if let ( & ty:: Slice ( slice_ty) , & ty:: Str ) = ( & ty_from. kind( ) , & ty_to. kind( ) ) ;
24
+ if let ty:: Uint ( ty:: UintTy :: U8 ) = slice_ty. kind( ) ;
25
+ if from_mutbl == to_mutbl;
26
+ then {
27
+ let postfix = if * from_mutbl == Mutability :: Mut {
28
+ "_mut"
29
+ } else {
30
+ ""
31
+ } ;
33
32
34
- span_lint_and_sugg(
33
+ span_lint_and_sugg(
34
+ cx,
35
+ TRANSMUTE_BYTES_TO_STR ,
36
+ e. span,
37
+ & format!( "transmute from a `{}` to a `{}`" , from_ty, to_ty) ,
38
+ "consider using" ,
39
+ format!(
40
+ "std::str::from_utf8{}({}).unwrap()" ,
41
+ postfix,
42
+ snippet( cx, args[ 0 ] . span, ".." ) ,
43
+ ) ,
44
+ Applicability :: Unspecified ,
45
+ ) ;
46
+ triggered = true ;
47
+ } else {
48
+ if ( cx. tcx. erase_regions( from_ty) != cx. tcx. erase_regions( to_ty) )
49
+ && !const_context {
50
+ span_lint_and_then(
35
51
cx,
36
- TRANSMUTE_BYTES_TO_STR ,
52
+ TRANSMUTE_PTR_TO_PTR ,
37
53
e. span,
38
- & format!( "transmute from a `{}` to a `{}`" , from_ty, to_ty) ,
39
- "consider using" ,
40
- format!(
41
- "std::str::from_utf8{}({}).unwrap()" ,
42
- postfix,
43
- snippet( cx, args[ 0 ] . span, ".." ) ,
44
- ) ,
45
- Applicability :: Unspecified ,
54
+ "transmute from a reference to a reference" ,
55
+ |diag| if let Some ( arg) = sugg:: Sugg :: hir_opt( cx, & args[ 0 ] ) {
56
+ let ty_from_and_mut = ty:: TypeAndMut {
57
+ ty: ty_from,
58
+ mutbl: * from_mutbl
59
+ } ;
60
+ let ty_to_and_mut = ty:: TypeAndMut { ty: ty_to, mutbl: * to_mutbl } ;
61
+ let sugg_paren = arg
62
+ . as_ty( cx. tcx. mk_ptr( ty_from_and_mut) )
63
+ . as_ty( cx. tcx. mk_ptr( ty_to_and_mut) ) ;
64
+ let sugg = if * to_mutbl == Mutability :: Mut {
65
+ sugg_paren. mut_addr_deref( )
66
+ } else {
67
+ sugg_paren. addr_deref( )
68
+ } ;
69
+ diag. span_suggestion(
70
+ e. span,
71
+ "try" ,
72
+ sugg. to_string( ) ,
73
+ Applicability :: Unspecified ,
74
+ ) ;
75
+ } ,
46
76
) ;
47
- triggered = true ;
48
- } else {
49
- if ( cx. tcx. erase_regions( from_ty) != cx. tcx. erase_regions( to_ty) )
50
- && !const_context {
51
- span_lint_and_then(
52
- cx,
53
- TRANSMUTE_PTR_TO_PTR ,
54
- e. span,
55
- "transmute from a reference to a reference" ,
56
- |diag| if let Some ( arg) = sugg:: Sugg :: hir_opt( cx, & args[ 0 ] ) {
57
- let ty_from_and_mut = ty:: TypeAndMut {
58
- ty: ty_from,
59
- mutbl: * from_mutbl
60
- } ;
61
- let ty_to_and_mut = ty:: TypeAndMut { ty: ty_to, mutbl: * to_mutbl } ;
62
- let sugg_paren = arg
63
- . as_ty( cx. tcx. mk_ptr( ty_from_and_mut) )
64
- . as_ty( cx. tcx. mk_ptr( ty_to_and_mut) ) ;
65
- let sugg = if * to_mutbl == Mutability :: Mut {
66
- sugg_paren. mut_addr_deref( )
67
- } else {
68
- sugg_paren. addr_deref( )
69
- } ;
70
- diag. span_suggestion(
71
- e. span,
72
- "try" ,
73
- sugg. to_string( ) ,
74
- Applicability :: Unspecified ,
75
- ) ;
76
- } ,
77
- ) ;
78
77
79
- triggered = true ;
80
- }
78
+ triggered = true ;
81
79
}
82
80
}
83
- } ,
84
- _ => { } ,
81
+ }
85
82
}
86
83
87
84
triggered
0 commit comments