|
1 | 1 | //! Checks for usage of `&Vec[_]` and `&String`. |
2 | 2 |
|
3 | | -use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then, span_lint_hir_and_then}; |
| 3 | +use clippy_utils::diagnostics::{span_lint, span_lint_and_then, span_lint_hir_and_then}; |
4 | 4 | use clippy_utils::source::snippet_opt; |
5 | 5 | use clippy_utils::ty::expr_sig; |
6 | 6 | use clippy_utils::visitors::contains_unsafe_block; |
@@ -129,30 +129,7 @@ declare_clippy_lint! { |
129 | 129 | "fns that create mutable refs from immutable ref args" |
130 | 130 | } |
131 | 131 |
|
132 | | -declare_clippy_lint! { |
133 | | - /// ### What it does |
134 | | - /// This lint checks for invalid usages of `ptr::null`. |
135 | | - /// |
136 | | - /// ### Why is this bad? |
137 | | - /// This causes undefined behavior. |
138 | | - /// |
139 | | - /// ### Example |
140 | | - /// ```ignore |
141 | | - /// // Undefined behavior |
142 | | - /// unsafe { std::slice::from_raw_parts(ptr::null(), 0); } |
143 | | - /// ``` |
144 | | - /// |
145 | | - /// Use instead: |
146 | | - /// ```ignore |
147 | | - /// unsafe { std::slice::from_raw_parts(NonNull::dangling().as_ptr(), 0); } |
148 | | - /// ``` |
149 | | - #[clippy::version = "1.53.0"] |
150 | | - pub INVALID_NULL_PTR_USAGE, |
151 | | - correctness, |
152 | | - "invalid usage of a null pointer, suggesting `NonNull::dangling()` instead" |
153 | | -} |
154 | | - |
155 | | -declare_lint_pass!(Ptr => [PTR_ARG, CMP_NULL, MUT_FROM_REF, INVALID_NULL_PTR_USAGE]); |
| 132 | +declare_lint_pass!(Ptr => [PTR_ARG, CMP_NULL, MUT_FROM_REF]); |
156 | 133 |
|
157 | 134 | impl<'tcx> LateLintPass<'tcx> for Ptr { |
158 | 135 | fn check_trait_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx TraitItem<'_>) { |
@@ -264,48 +241,6 @@ impl<'tcx> LateLintPass<'tcx> for Ptr { |
264 | 241 | "comparing with null is better expressed by the `.is_null()` method", |
265 | 242 | ); |
266 | 243 | } |
267 | | - } else { |
268 | | - check_invalid_ptr_usage(cx, expr); |
269 | | - } |
270 | | - } |
271 | | -} |
272 | | - |
273 | | -fn check_invalid_ptr_usage<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) { |
274 | | - if let ExprKind::Call(fun, args) = expr.kind |
275 | | - && let ExprKind::Path(ref qpath) = fun.kind |
276 | | - && let Some(fun_def_id) = cx.qpath_res(qpath, fun.hir_id).opt_def_id() |
277 | | - && let Some(name) = cx.tcx.get_diagnostic_name(fun_def_id) |
278 | | - { |
279 | | - // `arg` positions where null would cause U.B. |
280 | | - let arg_indices: &[_] = match name { |
281 | | - sym::ptr_read |
282 | | - | sym::ptr_read_unaligned |
283 | | - | sym::ptr_read_volatile |
284 | | - | sym::ptr_replace |
285 | | - | sym::ptr_slice_from_raw_parts |
286 | | - | sym::ptr_slice_from_raw_parts_mut |
287 | | - | sym::ptr_write |
288 | | - | sym::ptr_write_bytes |
289 | | - | sym::ptr_write_unaligned |
290 | | - | sym::ptr_write_volatile |
291 | | - | sym::slice_from_raw_parts |
292 | | - | sym::slice_from_raw_parts_mut => &[0], |
293 | | - sym::ptr_copy | sym::ptr_copy_nonoverlapping | sym::ptr_swap | sym::ptr_swap_nonoverlapping => &[0, 1], |
294 | | - _ => return, |
295 | | - }; |
296 | | - |
297 | | - for &arg_idx in arg_indices { |
298 | | - if let Some(arg) = args.get(arg_idx).filter(|arg| is_null_path(cx, arg)) { |
299 | | - span_lint_and_sugg( |
300 | | - cx, |
301 | | - INVALID_NULL_PTR_USAGE, |
302 | | - arg.span, |
303 | | - "pointer must be non-null", |
304 | | - "change this to", |
305 | | - "core::ptr::NonNull::dangling().as_ptr()".to_string(), |
306 | | - Applicability::MachineApplicable, |
307 | | - ); |
308 | | - } |
309 | 244 | } |
310 | 245 | } |
311 | 246 | } |
|
0 commit comments