Skip to content

Commit

Permalink
Add suggestion for zero-ptr lint
Browse files Browse the repository at this point in the history
  • Loading branch information
tesuji committed Sep 29, 2019
1 parent b292183 commit b37f971
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
15 changes: 7 additions & 8 deletions clippy_lints/src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, span_lint, span_lint_and_sugg,
span_lint_and_then, span_lint_hir_and_then, walk_ptrs_ty, SpanlessEq,
};

declare_clippy_lint! {
Expand Down Expand Up @@ -623,15 +623,14 @@ 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) = match mutbl {
Mutability::MutMutable => ("`0 as *mut _` detected. Consider using `ptr::null_mut()`", "std::ptr::null_mut()"),
Mutability::MutImmutable => ("`0 as *const _` detected. Consider using `ptr::null()`", "std::ptr::null()"),
};
span_lint(cx, ZERO_PTR, span, msg);
span_lint_and_sugg(cx, ZERO_PTR, span, msg, "try", sugg.to_string(), Applicability::MaybeIncorrect);
}
}
}
7 changes: 3 additions & 4 deletions tests/ui/zero_ptr.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#[allow(unused_variables)]
fn main() {
let x = 0 as *const usize;
let y = 0 as *mut f64;
let _ = 0 as *const usize;
let _ = 0 as *mut f64;

let z = 0;
let z = z as *const usize; // this is currently not caught
let _ = z as *const usize; // this is currently not caught
}
12 changes: 6 additions & 6 deletions tests/ui/zero_ptr.stderr
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
error: `0 as *const _` detected. Consider using `ptr::null()`
--> $DIR/zero_ptr.rs:3:13
--> $DIR/zero_ptr.rs:2:13
|
LL | let x = 0 as *const usize;
| ^^^^^^^^^^^^^^^^^
LL | let _ = 0 as *const usize;
| ^^^^^^^^^^^^^^^^^ help: try: `std::ptr::null()`
|
= 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
--> $DIR/zero_ptr.rs:3:13
|
LL | let y = 0 as *mut f64;
| ^^^^^^^^^^^^^
LL | let _ = 0 as *mut f64;
| ^^^^^^^^^^^^^ help: try: `std::ptr::null_mut()`

error: aborting due to 2 previous errors

0 comments on commit b37f971

Please sign in to comment.