Skip to content

Commit

Permalink
Auto merge of rust-lang#11913 - KisaragiEffective:fix/ptr-as-ptr-with…
Browse files Browse the repository at this point in the history
…-null, r=llogiq

fix(ptr_as_ptr): handle `std::ptr::null{_mut}`

close rust-lang#11066
close rust-lang#11665
close rust-lang#11911

*Please write a short comment explaining your change (or "none" for internal only changes)*

changelog: [`ptr_as_ptr`]: handle `std::ptr::null` and `std::ptr::null_mut`
  • Loading branch information
bors committed Dec 6, 2023
2 parents 4a56563 + 8eea8b1 commit 2793e8d
Show file tree
Hide file tree
Showing 4 changed files with 426 additions and 5 deletions.
55 changes: 51 additions & 4 deletions clippy_lints/src/casts/ptr_as_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,29 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::sugg::Sugg;
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind, Mutability, TyKind};
use rustc_hir::{Expr, ExprKind, Mutability, QPath, TyKind};
use rustc_hir_pretty::qpath_to_string;
use rustc_lint::LateContext;
use rustc_middle::ty::{self, TypeAndMut};
use rustc_span::sym;

use super::PTR_AS_PTR;

enum OmitFollowedCastReason<'a> {
None,
Null(&'a QPath<'a>),
NullMut(&'a QPath<'a>),
}

impl OmitFollowedCastReason<'_> {
fn corresponding_item(&self) -> Option<&QPath<'_>> {
match self {
OmitFollowedCastReason::None => None,
OmitFollowedCastReason::Null(x) | OmitFollowedCastReason::NullMut(x) => Some(*x),
}
}
}

pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, msrv: &Msrv) {
if !msrv.meets(msrvs::POINTER_CAST) {
return;
Expand All @@ -25,7 +42,6 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, msrv: &Msrv) {
&& to_pointee_ty.is_sized(cx.tcx, cx.param_env)
{
let mut app = Applicability::MachineApplicable;
let cast_expr_sugg = Sugg::hir_with_applicability(cx, cast_expr, "_", &mut app);
let turbofish = match &cast_to_hir_ty.kind {
TyKind::Infer => String::new(),
TyKind::Ptr(mut_ty) => {
Expand All @@ -41,13 +57,44 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, msrv: &Msrv) {
_ => return,
};

// following `cast` does not compile because it fails to infer what type is expected
// as type argument to `std::ptr::ptr_null` or `std::ptr::ptr_null_mut`, so
// we omit following `cast`:
let omit_cast = if let ExprKind::Call(func, []) = cast_expr.kind
&& let ExprKind::Path(ref qpath @ QPath::Resolved(None, path)) = func.kind
{
let method_defid = path.res.def_id();
if cx.tcx.is_diagnostic_item(sym::ptr_null, method_defid) {
OmitFollowedCastReason::Null(qpath)
} else if cx.tcx.is_diagnostic_item(sym::ptr_null_mut, method_defid) {
OmitFollowedCastReason::NullMut(qpath)
} else {
OmitFollowedCastReason::None
}
} else {
OmitFollowedCastReason::None
};

let (help, final_suggestion) = if let Some(method) = omit_cast.corresponding_item() {
// don't force absolute path
let method = qpath_to_string(method);
("try call directly", format!("{method}{turbofish}()"))
} else {
let cast_expr_sugg = Sugg::hir_with_applicability(cx, cast_expr, "_", &mut app);

(
"try `pointer::cast`, a safer alternative",
format!("{}.cast{turbofish}()", cast_expr_sugg.maybe_par()),
)
};

span_lint_and_sugg(
cx,
PTR_AS_PTR,
expr.span,
"`as` casting between raw pointers without changing its mutability",
"try `pointer::cast`, a safer alternative",
format!("{}.cast{turbofish}()", cast_expr_sugg.maybe_par()),
help,
final_suggestion,
app,
);
}
Expand Down
115 changes: 115 additions & 0 deletions tests/ui/ptr_as_ptr.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,118 @@ fn _msrv_1_38() {
let _ = ptr.cast::<i32>();
let _ = mut_ptr.cast::<i32>();
}

#[allow(clippy::unnecessary_cast)]
mod null {
fn use_path_mut() -> *mut u32 {
use std::ptr;
ptr::null_mut::<u32>()
}

fn full_path_mut() -> *mut u32 {
std::ptr::null_mut::<u32>()
}

fn core_path_mut() -> *mut u32 {
use core::ptr;
ptr::null_mut::<u32>()
}

fn full_core_path_mut() -> *mut u32 {
core::ptr::null_mut::<u32>()
}

fn use_path() -> *const u32 {
use std::ptr;
ptr::null::<u32>()
}

fn full_path() -> *const u32 {
std::ptr::null::<u32>()
}

fn core_path() -> *const u32 {
use core::ptr;
ptr::null::<u32>()
}

fn full_core_path() -> *const u32 {
core::ptr::null::<u32>()
}
}

mod null_ptr_infer {
fn use_path_mut() -> *mut u32 {
use std::ptr;
ptr::null_mut()
}

fn full_path_mut() -> *mut u32 {
std::ptr::null_mut()
}

fn core_path_mut() -> *mut u32 {
use core::ptr;
ptr::null_mut()
}

fn full_core_path_mut() -> *mut u32 {
core::ptr::null_mut()
}

fn use_path() -> *const u32 {
use std::ptr;
ptr::null()
}

fn full_path() -> *const u32 {
std::ptr::null()
}

fn core_path() -> *const u32 {
use core::ptr;
ptr::null()
}

fn full_core_path() -> *const u32 {
core::ptr::null()
}
}

mod null_entire_infer {
fn use_path_mut() -> *mut u32 {
use std::ptr;
ptr::null_mut()
}

fn full_path_mut() -> *mut u32 {
std::ptr::null_mut()
}

fn core_path_mut() -> *mut u32 {
use core::ptr;
ptr::null_mut()
}

fn full_core_path_mut() -> *mut u32 {
core::ptr::null_mut()
}

fn use_path() -> *const u32 {
use std::ptr;
ptr::null()
}

fn full_path() -> *const u32 {
std::ptr::null()
}

fn core_path() -> *const u32 {
use core::ptr;
ptr::null()
}

fn full_core_path() -> *const u32 {
core::ptr::null()
}
}
115 changes: 115 additions & 0 deletions tests/ui/ptr_as_ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,118 @@ fn _msrv_1_38() {
let _ = ptr as *const i32;
let _ = mut_ptr as *mut i32;
}

#[allow(clippy::unnecessary_cast)]
mod null {
fn use_path_mut() -> *mut u32 {
use std::ptr;
ptr::null_mut() as *mut u32
}

fn full_path_mut() -> *mut u32 {
std::ptr::null_mut() as *mut u32
}

fn core_path_mut() -> *mut u32 {
use core::ptr;
ptr::null_mut() as *mut u32
}

fn full_core_path_mut() -> *mut u32 {
core::ptr::null_mut() as *mut u32
}

fn use_path() -> *const u32 {
use std::ptr;
ptr::null() as *const u32
}

fn full_path() -> *const u32 {
std::ptr::null() as *const u32
}

fn core_path() -> *const u32 {
use core::ptr;
ptr::null() as *const u32
}

fn full_core_path() -> *const u32 {
core::ptr::null() as *const u32
}
}

mod null_ptr_infer {
fn use_path_mut() -> *mut u32 {
use std::ptr;
ptr::null_mut() as *mut _
}

fn full_path_mut() -> *mut u32 {
std::ptr::null_mut() as *mut _
}

fn core_path_mut() -> *mut u32 {
use core::ptr;
ptr::null_mut() as *mut _
}

fn full_core_path_mut() -> *mut u32 {
core::ptr::null_mut() as *mut _
}

fn use_path() -> *const u32 {
use std::ptr;
ptr::null() as *const _
}

fn full_path() -> *const u32 {
std::ptr::null() as *const _
}

fn core_path() -> *const u32 {
use core::ptr;
ptr::null() as *const _
}

fn full_core_path() -> *const u32 {
core::ptr::null() as *const _
}
}

mod null_entire_infer {
fn use_path_mut() -> *mut u32 {
use std::ptr;
ptr::null_mut() as _
}

fn full_path_mut() -> *mut u32 {
std::ptr::null_mut() as _
}

fn core_path_mut() -> *mut u32 {
use core::ptr;
ptr::null_mut() as _
}

fn full_core_path_mut() -> *mut u32 {
core::ptr::null_mut() as _
}

fn use_path() -> *const u32 {
use std::ptr;
ptr::null() as _
}

fn full_path() -> *const u32 {
std::ptr::null() as _
}

fn core_path() -> *const u32 {
use core::ptr;
ptr::null() as _
}

fn full_core_path() -> *const u32 {
core::ptr::null() as _
}
}
Loading

0 comments on commit 2793e8d

Please sign in to comment.