@@ -41,8 +41,8 @@ declare_clippy_lint! {
41
41
declare_lint_pass ! ( UnsafeSizeofCountCopies => [ UNSAFE_SIZEOF_COUNT_COPIES ] ) ;
42
42
43
43
fn get_size_of_ty ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) -> Option < Ty < ' tcx > > {
44
- match & expr. kind {
45
- ExprKind :: Call ( ref count_func, _func_args) => {
44
+ match expr. kind {
45
+ ExprKind :: Call ( count_func, _func_args) => {
46
46
if_chain ! {
47
47
if let ExprKind :: Path ( ref count_func_qpath) = count_func. kind;
48
48
if let Some ( def_id) = cx. qpath_res( count_func_qpath, count_func. hir_id) . opt_def_id( ) ;
@@ -56,21 +56,24 @@ fn get_size_of_ty(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<Ty<'tc
56
56
}
57
57
} ,
58
58
ExprKind :: Binary ( op, left, right) if BinOpKind :: Mul == op. node || BinOpKind :: Div == op. node => {
59
- get_size_of_ty ( cx, & * left) . or_else ( || get_size_of_ty ( cx, & * right) )
59
+ get_size_of_ty ( cx, left) . or_else ( || get_size_of_ty ( cx, right) )
60
60
} ,
61
61
_ => None ,
62
62
}
63
63
}
64
64
65
65
fn get_pointee_ty_and_count_expr ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) -> Option < ( Ty < ' tcx > , & ' tcx Expr < ' tcx > ) > {
66
66
if_chain ! {
67
- // Find calls to ptr::copy and copy_nonoverlapping
68
- if let ExprKind :: Call ( ref func, ref args) = expr. kind;
69
- if let [ _src, _dest, count] = & * * args;
67
+ // Find calls to ptr::{copy, copy_nonoverlapping}
68
+ // and ptr::{swap_nonoverlapping, write_bytes},
69
+ if let ExprKind :: Call ( func, args) = expr. kind;
70
+ if let [ _, _, count] = args;
70
71
if let ExprKind :: Path ( ref func_qpath) = func. kind;
71
72
if let Some ( def_id) = cx. qpath_res( func_qpath, func. hir_id) . opt_def_id( ) ;
72
73
if match_def_path( cx, def_id, & paths:: COPY_NONOVERLAPPING )
73
- || match_def_path( cx, def_id, & paths:: COPY ) ;
74
+ || match_def_path( cx, def_id, & paths:: COPY )
75
+ || match_def_path( cx, def_id, & paths:: WRITE_BYTES )
76
+ || match_def_path( cx, def_id, & paths:: PTR_SWAP_NONOVERLAPPING ) ;
74
77
75
78
// Get the pointee type
76
79
if let Some ( pointee_ty) = cx. typeck_results( ) . node_substs( func. hir_id) . types( ) . next( ) ;
@@ -79,11 +82,11 @@ fn get_pointee_ty_and_count_expr(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -
79
82
}
80
83
} ;
81
84
if_chain ! {
82
- // Find calls to copy_{from,to}{,_nonoverlapping}
83
- if let ExprKind :: MethodCall ( ref method_path, _, ref args, _) = expr. kind;
84
- if let [ ptr_self, _, count] = & * * args;
85
+ // Find calls to copy_{from,to}{,_nonoverlapping} and write_bytes methods
86
+ if let ExprKind :: MethodCall ( method_path, _, args, _) = expr. kind;
87
+ if let [ ptr_self, _, count] = args;
85
88
let method_ident = method_path. ident. as_str( ) ;
86
- if method_ident== "copy_to" || method_ident == "copy_from"
89
+ if method_ident == "write_bytes" || method_ident == "copy_to" || method_ident == "copy_from"
87
90
|| method_ident == "copy_to_nonoverlapping" || method_ident == "copy_from_nonoverlapping" ;
88
91
89
92
// Get the pointee type
@@ -93,6 +96,21 @@ fn get_pointee_ty_and_count_expr(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -
93
96
return Some ( ( pointee_ty, count) ) ;
94
97
}
95
98
} ;
99
+ if_chain ! {
100
+ // Find calls to ptr::copy and copy_nonoverlapping
101
+ if let ExprKind :: Call ( func, args) = expr. kind;
102
+ if let [ _data, count] = args;
103
+ if let ExprKind :: Path ( ref func_qpath) = func. kind;
104
+ if let Some ( def_id) = cx. qpath_res( func_qpath, func. hir_id) . opt_def_id( ) ;
105
+ if match_def_path( cx, def_id, & paths:: PTR_SLICE_FROM_RAW_PARTS )
106
+ || match_def_path( cx, def_id, & paths:: PTR_SLICE_FROM_RAW_PARTS_MUT ) ;
107
+
108
+ // Get the pointee type
109
+ if let Some ( pointee_ty) = cx. typeck_results( ) . node_substs( func. hir_id) . types( ) . next( ) ;
110
+ then {
111
+ return Some ( ( pointee_ty, count) ) ;
112
+ }
113
+ } ;
96
114
None
97
115
}
98
116
@@ -102,7 +120,7 @@ impl<'tcx> LateLintPass<'tcx> for UnsafeSizeofCountCopies {
102
120
for the count parameter, it already gets multiplied by the size of the pointed to type";
103
121
104
122
const LINT_MSG : & str = "unsafe memory copying using a byte count \
105
- (Multiplied by size_of::<T>) instead of a count of T";
123
+ (multiplied by size_of/size_of_val ::<T>) instead of a count of T";
106
124
107
125
if_chain ! {
108
126
// Find calls to unsafe copy functions and get
0 commit comments