@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then, span_lin
22use clippy_utils:: source:: SpanRangeExt ;
33use clippy_utils:: sugg:: Sugg ;
44use clippy_utils:: visitors:: contains_unsafe_block;
5- use clippy_utils:: { get_expr_use_or_unification_node, is_lint_allowed, path_def_id, path_to_local, std_or_core } ;
5+ use clippy_utils:: { get_expr_use_or_unification_node, is_lint_allowed, path_def_id, path_to_local} ;
66use hir:: LifetimeName ;
77use rustc_abi:: ExternAbi ;
88use rustc_errors:: { Applicability , MultiSpan } ;
@@ -125,30 +125,7 @@ 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-
151- declare_lint_pass ! ( Ptr => [ PTR_ARG , CMP_NULL , MUT_FROM_REF , INVALID_NULL_PTR_USAGE ] ) ;
128+ declare_lint_pass ! ( Ptr => [ PTR_ARG , CMP_NULL , MUT_FROM_REF ] ) ;
152129
153130impl < ' tcx > LateLintPass < ' tcx > for Ptr {
154131 fn check_trait_item ( & mut self , cx : & LateContext < ' tcx > , item : & ' tcx TraitItem < ' _ > ) {
@@ -268,54 +245,6 @@ impl<'tcx> LateLintPass<'tcx> for Ptr {
268245 format ! ( "{non_null_path_snippet}.is_null()" ) ,
269246 Applicability :: MachineApplicable ,
270247 ) ;
271- } else {
272- check_invalid_ptr_usage ( cx, expr) ;
273- }
274- }
275- }
276-
277- fn check_invalid_ptr_usage < ' tcx > ( cx : & LateContext < ' tcx > , expr : & ' tcx Expr < ' _ > ) {
278- if let ExprKind :: Call ( fun, args) = expr. kind
279- && let ExprKind :: Path ( ref qpath) = fun. kind
280- && let Some ( fun_def_id) = cx. qpath_res ( qpath, fun. hir_id ) . opt_def_id ( )
281- && let Some ( name) = cx. tcx . get_diagnostic_name ( fun_def_id)
282- {
283- // TODO: `ptr_slice_from_raw_parts` and its mutable variant should probably still be linted
284- // conditionally based on how the return value is used, but not universally like the other
285- // functions since there are valid uses for null slice pointers.
286- //
287- // See: https://github.com/rust-lang/rust-clippy/pull/13452/files#r1773772034
288-
289- // `arg` positions where null would cause U.B.
290- let arg_indices: & [ _ ] = match name {
291- sym:: ptr_read
292- | sym:: ptr_read_unaligned
293- | sym:: ptr_read_volatile
294- | sym:: ptr_replace
295- | sym:: ptr_write
296- | sym:: ptr_write_bytes
297- | sym:: ptr_write_unaligned
298- | sym:: ptr_write_volatile
299- | sym:: slice_from_raw_parts
300- | sym:: slice_from_raw_parts_mut => & [ 0 ] ,
301- sym:: ptr_copy | sym:: ptr_copy_nonoverlapping | sym:: ptr_swap | sym:: ptr_swap_nonoverlapping => & [ 0 , 1 ] ,
302- _ => return ,
303- } ;
304-
305- for & arg_idx in arg_indices {
306- if let Some ( arg) = args. get ( arg_idx) . filter ( |arg| is_null_path ( cx, arg) )
307- && let Some ( std_or_core) = std_or_core ( cx)
308- {
309- span_lint_and_sugg (
310- cx,
311- INVALID_NULL_PTR_USAGE ,
312- arg. span ,
313- "pointer must be non-null" ,
314- "change this to" ,
315- format ! ( "{std_or_core}::ptr::NonNull::dangling().as_ptr()" ) ,
316- Applicability :: MachineApplicable ,
317- ) ;
318- }
319248 }
320249 }
321250}
0 commit comments