From 3ddd82582543a3ab0c6abc01580a0ccb887f97c1 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Sun, 29 Sep 2019 13:22:44 +0000 Subject: [PATCH] Add suggestion for zero-ptr lint --- clippy_lints/src/misc.rs | 20 ++++++++++++-------- clippy_lints/src/utils/mod.rs | 2 +- tests/ui/zero_ptr.rs | 11 +++++++---- tests/ui/zero_ptr.stderr | 16 ++++++++-------- 4 files changed, 28 insertions(+), 21 deletions(-) diff --git a/clippy_lints/src/misc.rs b/clippy_lints/src/misc.rs index 437114066cdf..501c13e6b952 100644 --- a/clippy_lints/src/misc.rs +++ b/clippy_lints/src/misc.rs @@ -13,8 +13,8 @@ use crate::consts::{constant, Constant}; use crate::utils::sugg::Sugg; use crate::utils::{ get_item_name, get_parent_expr, implements_trait, in_constant, is_integer_const, iter_input_pats, - last_path_segment, match_qpath, match_trait_method, paths, snippet, span_lint, span_lint_and_then, - span_lint_hir_and_then, walk_ptrs_ty, SpanlessEq, + last_path_segment, match_qpath, match_trait_method, paths, snippet, snippet_opt, span_lint, span_lint_and_sugg, + span_lint_and_then, span_lint_hir_and_then, walk_ptrs_ty, SpanlessEq, }; declare_clippy_lint! { @@ -623,15 +623,19 @@ fn check_cast(cx: &LateContext<'_, '_>, span: Span, e: &Expr, ty: &Ty) { if_chain! { if let TyKind::Ptr(MutTy { mutbl, .. }) = ty.kind; if let ExprKind::Lit(ref lit) = e.kind; - if let LitKind::Int(value, ..) = lit.node; - if value == 0; + if let LitKind::Int(0, _) = lit.node; if !in_constant(cx, e.hir_id); then { - let msg = match mutbl { - Mutability::MutMutable => "`0 as *mut _` detected. Consider using `ptr::null_mut()`", - Mutability::MutImmutable => "`0 as *const _` detected. Consider using `ptr::null()`", + let (msg, sugg_fn) = match mutbl { + Mutability::MutMutable => ("`0 as *mut _` detected", "std::ptr::null_mut"), + Mutability::MutImmutable => ("`0 as *const _` detected", "std::ptr::null"), }; - span_lint(cx, ZERO_PTR, span, msg); + if let Some(ty_snip) = snippet_opt(cx, ty.span) { + let sugg = format!("{}::<{}>()", sugg_fn, ty_snip); + span_lint_and_sugg(cx, ZERO_PTR, span, msg, "try", sugg, Applicability::MachineApplicable) + } else { + span_lint_and_sugg(cx, ZERO_PTR, span, msg, "try", String::from(sugg_fn) + "()", Applicability::MaybeIncorrect) + } } } } diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index 8fd84c07a7d2..27dad6322d88 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -61,7 +61,7 @@ pub fn differing_macro_contexts(lhs: Span, rhs: Span) -> bool { /// # Example /// /// ```rust,ignore -/// if in_constant(cx, expr.id) { +/// if in_constant(cx, expr.hir_id) { /// // Do something /// } /// ``` diff --git a/tests/ui/zero_ptr.rs b/tests/ui/zero_ptr.rs index 2291c77d56e6..4b3d751ae846 100644 --- a/tests/ui/zero_ptr.rs +++ b/tests/ui/zero_ptr.rs @@ -1,8 +1,11 @@ -#[allow(unused_variables)] +pub fn foo(_const: *const f32, _mut: *mut i64) {} + fn main() { - let x = 0 as *const usize; - let y = 0 as *mut f64; + let _ = 0 as *const usize; + let _ = 0 as *mut f64; + + foo(0 as _, 0 as _); let z = 0; - let z = z as *const usize; // this is currently not caught + let _ = z as *const usize; // this is currently not caught } diff --git a/tests/ui/zero_ptr.stderr b/tests/ui/zero_ptr.stderr index b79c3457b9b3..8c006233b8f0 100644 --- a/tests/ui/zero_ptr.stderr +++ b/tests/ui/zero_ptr.stderr @@ -1,16 +1,16 @@ -error: `0 as *const _` detected. Consider using `ptr::null()` - --> $DIR/zero_ptr.rs:3:13 +error: `0 as *const _` detected + --> $DIR/zero_ptr.rs:4:13 | -LL | let x = 0 as *const usize; - | ^^^^^^^^^^^^^^^^^ +LL | let _ = 0 as *const usize; + | ^^^^^^^^^^^^^^^^^ help: try: `std::ptr::null::<*const usize>()` | = note: `-D clippy::zero-ptr` implied by `-D warnings` -error: `0 as *mut _` detected. Consider using `ptr::null_mut()` - --> $DIR/zero_ptr.rs:4:13 +error: `0 as *mut _` detected + --> $DIR/zero_ptr.rs:5:13 | -LL | let y = 0 as *mut f64; - | ^^^^^^^^^^^^^ +LL | let _ = 0 as *mut f64; + | ^^^^^^^^^^^^^ help: try: `std::ptr::null_mut::<*mut f64>()` error: aborting due to 2 previous errors