@@ -125,29 +125,6 @@ declare_clippy_lint! {
125125 "fns that create mutable refs from immutable ref args"
126126}
127127
128- declare_clippy_lint ! {
129- /// ### What it does
130- /// This lint checks for invalid usages of `ptr::null`.
131- ///
132- /// ### Why is this bad?
133- /// This causes undefined behavior.
134- ///
135- /// ### Example
136- /// ```ignore
137- /// // Undefined behavior
138- /// unsafe { std::slice::from_raw_parts(ptr::null(), 0); }
139- /// ```
140- ///
141- /// Use instead:
142- /// ```ignore
143- /// unsafe { std::slice::from_raw_parts(NonNull::dangling().as_ptr(), 0); }
144- /// ```
145- #[ clippy:: version = "1.53.0" ]
146- pub INVALID_NULL_PTR_USAGE ,
147- correctness,
148- "invalid usage of a null pointer, suggesting `NonNull::dangling()` instead"
149- }
150-
151128declare_clippy_lint ! {
152129 /// ### What it does
153130 /// Use `std::ptr::eq` when applicable
@@ -177,7 +154,7 @@ declare_clippy_lint! {
177154 "use `std::ptr::eq` when comparing raw pointers"
178155}
179156
180- declare_lint_pass ! ( Ptr => [ PTR_ARG , CMP_NULL , MUT_FROM_REF , INVALID_NULL_PTR_USAGE , PTR_EQ ] ) ;
157+ declare_lint_pass ! ( Ptr => [ PTR_ARG , CMP_NULL , MUT_FROM_REF , PTR_EQ ] ) ;
181158
182159impl < ' tcx > LateLintPass < ' tcx > for Ptr {
183160 fn check_trait_item ( & mut self , cx : & LateContext < ' tcx > , item : & ' tcx TraitItem < ' _ > ) {
@@ -301,54 +278,6 @@ impl<'tcx> LateLintPass<'tcx> for Ptr {
301278 format ! ( "{non_null_path_snippet}.is_null()" ) ,
302279 Applicability :: MachineApplicable ,
303280 ) ;
304- } else {
305- check_invalid_ptr_usage ( cx, expr) ;
306- }
307- }
308- }
309-
310- fn check_invalid_ptr_usage < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
311- if let ExprKind :: Call ( fun, args) = expr. kind
312- && let ExprKind :: Path ( ref qpath) = fun. kind
313- && let Some ( fun_def_id) = cx. qpath_res ( qpath, fun. hir_id ) . opt_def_id ( )
314- && let Some ( name) = cx. tcx . get_diagnostic_name ( fun_def_id)
315- {
316- // TODO: `ptr_slice_from_raw_parts` and its mutable variant should probably still be linted
317- // conditionally based on how the return value is used, but not universally like the other
318- // functions since there are valid uses for null slice pointers.
319- //
320- // See: https://github.com/rust-lang/rust-clippy/pull/13452/files#r1773772034
321-
322- // `arg` positions where null would cause U.B.
323- let arg_indices: & [ _ ] = match name {
324- sym:: ptr_read
325- | sym:: ptr_read_unaligned
326- | sym:: ptr_read_volatile
327- | sym:: ptr_replace
328- | sym:: ptr_write
329- | sym:: ptr_write_bytes
330- | sym:: ptr_write_unaligned
331- | sym:: ptr_write_volatile
332- | sym:: slice_from_raw_parts
333- | sym:: slice_from_raw_parts_mut => & [ 0 ] ,
334- sym:: ptr_copy | sym:: ptr_copy_nonoverlapping | sym:: ptr_swap | sym:: ptr_swap_nonoverlapping => & [ 0 , 1 ] ,
335- _ => return ,
336- } ;
337-
338- for & arg_idx in arg_indices {
339- if let Some ( arg) = args. get ( arg_idx) . filter ( |arg| is_null_path ( cx, arg) )
340- && let Some ( std_or_core) = std_or_core ( cx)
341- {
342- span_lint_and_sugg (
343- cx,
344- INVALID_NULL_PTR_USAGE ,
345- arg. span ,
346- "pointer must be non-null" ,
347- "change this to" ,
348- format ! ( "{std_or_core}::ptr::NonNull::dangling().as_ptr()" ) ,
349- Applicability :: MachineApplicable ,
350- ) ;
351- }
352281 }
353282 }
354283}
0 commit comments