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 Oct 2, 2019
1 parent 83f90aa commit 6b1a868
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 23 deletions.
26 changes: 17 additions & 9 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, snippet_opt, 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 @@ -621,17 +621,25 @@ fn non_macro_local(cx: &LateContext<'_, '_>, res: def::Res) -> bool {

fn check_cast(cx: &LateContext<'_, '_>, span: Span, e: &Expr, ty: &Ty) {
if_chain! {
if let TyKind::Ptr(MutTy { mutbl, .. }) = ty.kind;
if let TyKind::Ptr(ref mut_ty) = 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 mut_ty.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);

let (sugg, appl) = if let TyKind::Infer = mut_ty.ty.kind {
(format!("{}()", sugg_fn), Applicability::MachineApplicable)
} else if let Some(mut_ty_snip) = snippet_opt(cx, mut_ty.ty.span) {
(format!("{}::<{}>()", sugg_fn, mut_ty_snip), Applicability::MachineApplicable)
} else {
// `MaybeIncorrect` as type inference may not work with the suggested code
(format!("{}()", sugg_fn), Applicability::MaybeIncorrect)
};
span_lint_and_sugg(cx, ZERO_PTR, span, msg, "try", sugg, appl);
}
}
}
2 changes: 1 addition & 1 deletion clippy_lints/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
/// }
/// ```
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/zero_ptr.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// run-rustfix
pub fn foo(_const: *const f32, _mut: *mut i64) {}

fn main() {
let _ = std::ptr::null::<usize>();
let _ = std::ptr::null_mut::<f64>();
let _: *const u8 = std::ptr::null();

foo(0 as _, 0 as _);
foo(std::ptr::null(), std::ptr::null_mut());

let z = 0;
let _ = z as *const usize; // this is currently not caught
}
14 changes: 10 additions & 4 deletions tests/ui/zero_ptr.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
#[allow(unused_variables)]
// run-rustfix
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;
let _: *const u8 = 0 as *const _;

foo(0 as _, 0 as _);
foo(0 as *const _, 0 as *mut _);

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

error: aborting due to 2 previous errors
error: `0 as *const _` detected
--> $DIR/zero_ptr.rs:7:24
|
LL | let _: *const u8 = 0 as *const _;
| ^^^^^^^^^^^^^ help: try: `std::ptr::null()`

error: `0 as *const _` detected
--> $DIR/zero_ptr.rs:10:9
|
LL | foo(0 as *const _, 0 as *mut _);
| ^^^^^^^^^^^^^ help: try: `std::ptr::null()`

error: `0 as *mut _` detected
--> $DIR/zero_ptr.rs:10:24
|
LL | foo(0 as *const _, 0 as *mut _);
| ^^^^^^^^^^^ help: try: `std::ptr::null_mut()`

error: aborting due to 5 previous errors

0 comments on commit 6b1a868

Please sign in to comment.