Skip to content

Commit b37f971

Browse files
committed
Add suggestion for zero-ptr lint
1 parent b292183 commit b37f971

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

clippy_lints/src/misc.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use crate::consts::{constant, Constant};
1313
use crate::utils::sugg::Sugg;
1414
use crate::utils::{
1515
get_item_name, get_parent_expr, implements_trait, in_constant, is_integer_const, iter_input_pats,
16-
last_path_segment, match_qpath, match_trait_method, paths, snippet, span_lint, span_lint_and_then,
17-
span_lint_hir_and_then, walk_ptrs_ty, SpanlessEq,
16+
last_path_segment, match_qpath, match_trait_method, paths, snippet, span_lint, span_lint_and_sugg,
17+
span_lint_and_then, span_lint_hir_and_then, walk_ptrs_ty, SpanlessEq,
1818
};
1919

2020
declare_clippy_lint! {
@@ -623,15 +623,14 @@ fn check_cast(cx: &LateContext<'_, '_>, span: Span, e: &Expr, ty: &Ty) {
623623
if_chain! {
624624
if let TyKind::Ptr(MutTy { mutbl, .. }) = ty.kind;
625625
if let ExprKind::Lit(ref lit) = e.kind;
626-
if let LitKind::Int(value, ..) = lit.node;
627-
if value == 0;
626+
if let LitKind::Int(0, _) = lit.node;
628627
if !in_constant(cx, e.hir_id);
629628
then {
630-
let msg = match mutbl {
631-
Mutability::MutMutable => "`0 as *mut _` detected. Consider using `ptr::null_mut()`",
632-
Mutability::MutImmutable => "`0 as *const _` detected. Consider using `ptr::null()`",
629+
let (msg, sugg) = match mutbl {
630+
Mutability::MutMutable => ("`0 as *mut _` detected. Consider using `ptr::null_mut()`", "std::ptr::null_mut()"),
631+
Mutability::MutImmutable => ("`0 as *const _` detected. Consider using `ptr::null()`", "std::ptr::null()"),
633632
};
634-
span_lint(cx, ZERO_PTR, span, msg);
633+
span_lint_and_sugg(cx, ZERO_PTR, span, msg, "try", sugg.to_string(), Applicability::MaybeIncorrect);
635634
}
636635
}
637636
}

tests/ui/zero_ptr.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
#[allow(unused_variables)]
21
fn main() {
3-
let x = 0 as *const usize;
4-
let y = 0 as *mut f64;
2+
let _ = 0 as *const usize;
3+
let _ = 0 as *mut f64;
54

65
let z = 0;
7-
let z = z as *const usize; // this is currently not caught
6+
let _ = z as *const usize; // this is currently not caught
87
}

tests/ui/zero_ptr.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
error: `0 as *const _` detected. Consider using `ptr::null()`
2-
--> $DIR/zero_ptr.rs:3:13
2+
--> $DIR/zero_ptr.rs:2:13
33
|
4-
LL | let x = 0 as *const usize;
5-
| ^^^^^^^^^^^^^^^^^
4+
LL | let _ = 0 as *const usize;
5+
| ^^^^^^^^^^^^^^^^^ help: try: `std::ptr::null()`
66
|
77
= note: `-D clippy::zero-ptr` implied by `-D warnings`
88

99
error: `0 as *mut _` detected. Consider using `ptr::null_mut()`
10-
--> $DIR/zero_ptr.rs:4:13
10+
--> $DIR/zero_ptr.rs:3:13
1111
|
12-
LL | let y = 0 as *mut f64;
13-
| ^^^^^^^^^^^^^
12+
LL | let _ = 0 as *mut f64;
13+
| ^^^^^^^^^^^^^ help: try: `std::ptr::null_mut()`
1414

1515
error: aborting due to 2 previous errors
1616

0 commit comments

Comments
 (0)