Skip to content

Commit 737f0a6

Browse files
committed
Auto merge of #4599 - lzutao:zero-ptr-suggestion, r=flip1995
Add suggestion for zero-ptr lint changelog: Improve suggestion of `zero_ptr` lint
2 parents 844765c + 6b1a868 commit 737f0a6

File tree

5 files changed

+69
-23
lines changed

5 files changed

+69
-23
lines changed

clippy_lints/src/misc.rs

+17-9
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! {
@@ -621,17 +621,25 @@ fn non_macro_local(cx: &LateContext<'_, '_>, res: def::Res) -> bool {
621621

622622
fn check_cast(cx: &LateContext<'_, '_>, span: Span, e: &Expr, ty: &Ty) {
623623
if_chain! {
624-
if let TyKind::Ptr(MutTy { mutbl, .. }) = ty.kind;
624+
if let TyKind::Ptr(ref mut_ty) = 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 mut_ty.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+
634+
let (sugg, appl) = if let TyKind::Infer = mut_ty.ty.kind {
635+
(format!("{}()", sugg_fn), Applicability::MachineApplicable)
636+
} else if let Some(mut_ty_snip) = snippet_opt(cx, mut_ty.ty.span) {
637+
(format!("{}::<{}>()", sugg_fn, mut_ty_snip), Applicability::MachineApplicable)
638+
} else {
639+
// `MaybeIncorrect` as type inference may not work with the suggested code
640+
(format!("{}()", sugg_fn), Applicability::MaybeIncorrect)
641+
};
642+
span_lint_and_sugg(cx, ZERO_PTR, span, msg, "try", sugg, appl);
635643
}
636644
}
637645
}

clippy_lints/src/utils/mod.rs

+1-1
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.fixed

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// run-rustfix
2+
pub fn foo(_const: *const f32, _mut: *mut i64) {}
3+
4+
fn main() {
5+
let _ = std::ptr::null::<usize>();
6+
let _ = std::ptr::null_mut::<f64>();
7+
let _: *const u8 = std::ptr::null();
8+
9+
foo(0 as _, 0 as _);
10+
foo(std::ptr::null(), std::ptr::null_mut());
11+
12+
let z = 0;
13+
let _ = z as *const usize; // this is currently not caught
14+
}

tests/ui/zero_ptr.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
#[allow(unused_variables)]
1+
// run-rustfix
2+
pub fn foo(_const: *const f32, _mut: *mut i64) {}
3+
24
fn main() {
3-
let x = 0 as *const usize;
4-
let y = 0 as *mut f64;
5+
let _ = 0 as *const usize;
6+
let _ = 0 as *mut f64;
7+
let _: *const u8 = 0 as *const _;
8+
9+
foo(0 as _, 0 as _);
10+
foo(0 as *const _, 0 as *mut _);
511

612
let z = 0;
7-
let z = z as *const usize; // this is currently not caught
13+
let _ = z as *const usize; // this is currently not caught
814
}

tests/ui/zero_ptr.stderr

+27-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,34 @@
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:5:13
33
|
4-
LL | let x = 0 as *const usize;
5-
| ^^^^^^^^^^^^^^^^^
4+
LL | let _ = 0 as *const usize;
5+
| ^^^^^^^^^^^^^^^^^ help: try: `std::ptr::null::<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:6: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::<f64>()`
1414

15-
error: aborting due to 2 previous errors
15+
error: `0 as *const _` detected
16+
--> $DIR/zero_ptr.rs:7:24
17+
|
18+
LL | let _: *const u8 = 0 as *const _;
19+
| ^^^^^^^^^^^^^ help: try: `std::ptr::null()`
20+
21+
error: `0 as *const _` detected
22+
--> $DIR/zero_ptr.rs:10:9
23+
|
24+
LL | foo(0 as *const _, 0 as *mut _);
25+
| ^^^^^^^^^^^^^ help: try: `std::ptr::null()`
26+
27+
error: `0 as *mut _` detected
28+
--> $DIR/zero_ptr.rs:10:24
29+
|
30+
LL | foo(0 as *const _, 0 as *mut _);
31+
| ^^^^^^^^^^^ help: try: `std::ptr::null_mut()`
32+
33+
error: aborting due to 5 previous errors
1634

0 commit comments

Comments
 (0)