Skip to content

Commit 3ddd825

Browse files
committed
Add suggestion for zero-ptr lint
1 parent 83f90aa commit 3ddd825

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed

clippy_lints/src/misc.rs

Lines changed: 12 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, snippet_opt, 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,19 @@ 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_fn) = match mutbl {
630+
Mutability::MutMutable => ("`0 as *mut _` detected", "std::ptr::null_mut"),
631+
Mutability::MutImmutable => ("`0 as *const _` detected", "std::ptr::null"),
633632
};
634-
span_lint(cx, ZERO_PTR, span, msg);
633+
if let Some(ty_snip) = snippet_opt(cx, ty.span) {
634+
let sugg = format!("{}::<{}>()", sugg_fn, ty_snip);
635+
span_lint_and_sugg(cx, ZERO_PTR, span, msg, "try", sugg, Applicability::MachineApplicable)
636+
} else {
637+
span_lint_and_sugg(cx, ZERO_PTR, span, msg, "try", String::from(sugg_fn) + "()", Applicability::MaybeIncorrect)
638+
}
635639
}
636640
}
637641
}

clippy_lints/src/utils/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub fn differing_macro_contexts(lhs: Span, rhs: Span) -> bool {
6161
/// # Example
6262
///
6363
/// ```rust,ignore
64-
/// if in_constant(cx, expr.id) {
64+
/// if in_constant(cx, expr.hir_id) {
6565
/// // Do something
6666
/// }
6767
/// ```

tests/ui/zero_ptr.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
#[allow(unused_variables)]
1+
pub fn foo(_const: *const f32, _mut: *mut i64) {}
2+
23
fn main() {
3-
let x = 0 as *const usize;
4-
let y = 0 as *mut f64;
4+
let _ = 0 as *const usize;
5+
let _ = 0 as *mut f64;
6+
7+
foo(0 as _, 0 as _);
58

69
let z = 0;
7-
let z = z as *const usize; // this is currently not caught
10+
let _ = z as *const usize; // this is currently not caught
811
}

tests/ui/zero_ptr.stderr

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

9-
error: `0 as *mut _` detected. Consider using `ptr::null_mut()`
10-
--> $DIR/zero_ptr.rs:4:13
9+
error: `0 as *mut _` detected
10+
--> $DIR/zero_ptr.rs:5: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::<*mut f64>()`
1414

1515
error: aborting due to 2 previous errors
1616

0 commit comments

Comments
 (0)