Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note for future improvement:
This should be replaced with check of:

  1. if calling function is <T>(...args) -> *{const,mut} T
  2. one of:
    • args does not contain or refer T -> we can give arbitrary T
    • check if it still pass type-checking if we change T=Src to T=Dst

}

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