Skip to content

cast_slice_from_raw_parts: check for implicit cast to raw slice pointer #15437

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
43 changes: 42 additions & 1 deletion clippy_lints/src/casts/cast_slice_from_raw_parts.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::source::snippet_with_context;
use clippy_utils::{get_parent_expr, is_no_std_crate};
use rustc_errors::Applicability;
use rustc_hir::def_id::DefId;
use rustc_hir::{Expr, ExprKind};
use rustc_lint::LateContext;
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow};
use rustc_middle::ty::{self, Ty};
use rustc_span::sym;

Expand Down Expand Up @@ -42,13 +44,52 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>,
let mut applicability = Applicability::MachineApplicable;
let ptr = snippet_with_context(cx, ptr_arg.span, ctxt, "ptr", &mut applicability).0;
let len = snippet_with_context(cx, len_arg.span, ctxt, "len", &mut applicability).0;
let krate = if is_no_std_crate(cx) { "core" } else { "std" };
span_lint_and_sugg(
cx,
CAST_SLICE_FROM_RAW_PARTS,
span,
format!("casting the result of `{func}` to {cast_to}"),
"replace with",
format!("core::ptr::slice_{func}({ptr}, {len})"),
format!("{krate}::ptr::slice_{func}({ptr}, {len})"),
applicability,
);
}
}

/// Checks for implicit cast from slice reference to raw slice pointer.
pub(super) fn check_implicit_cast(cx: &LateContext<'_>, expr: &Expr<'_>) {
if let ExprKind::Call(fun, [ptr_arg, len_arg]) = expr.peel_blocks().kind
&& let ExprKind::Path(ref qpath) = fun.kind
&& let Some(fun_def_id) = cx.qpath_res(qpath, fun.hir_id).opt_def_id()
&& let Some(rpk) = raw_parts_kind(cx, fun_def_id)
&& !matches!(get_parent_expr(cx, expr).map(|e| e.kind), Some(ExprKind::Cast(..)))
&& let [deref, borrow] = cx.typeck_results().expr_adjustments(expr)
&& matches!(deref.kind, Adjust::Deref(..))
&& let Adjustment {
kind: Adjust::Borrow(AutoBorrow::RawPtr(..)),
target,
} = borrow
&& let ty::RawPtr(pointee_ty, _) = target.kind()
&& pointee_ty.is_slice()
&& !expr.span.from_expansion()
{
let func = match rpk {
RawPartsKind::Immutable => "from_raw_parts",
RawPartsKind::Mutable => "from_raw_parts_mut",
};
let mut applicability = Applicability::MachineApplicable;
let ctxt = expr.span.ctxt();
let ptr = snippet_with_context(cx, ptr_arg.span, ctxt, "ptr", &mut applicability).0;
let len = snippet_with_context(cx, len_arg.span, ctxt, "len", &mut applicability).0;
let krate = if is_no_std_crate(cx) { "core" } else { "std" };
span_lint_and_sugg(
cx,
CAST_SLICE_FROM_RAW_PARTS,
expr.span,
format!("implicitly casting the result of `{func}` to `{target}`"),
"replace_with",
format!("{krate}::ptr::slice_{func}({ptr}, {len})"),
applicability,
);
}
Expand Down
3 changes: 3 additions & 0 deletions clippy_lints/src/casts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,9 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
if self.msrv.meets(cx, msrvs::RAW_REF_OP) {
borrow_as_ptr::check_implicit_cast(cx, expr);
}
if self.msrv.meets(cx, msrvs::PTR_SLICE_RAW_PARTS) {
cast_slice_from_raw_parts::check_implicit_cast(cx, expr);
}
cast_ptr_alignment::check(cx, expr);
char_lit_as_u8::check(cx, expr);
ptr_as_ptr::check(cx, expr, self.msrv);
Expand Down
26 changes: 19 additions & 7 deletions tests/ui/cast_raw_slice_pointer_cast.fixed
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
#![warn(clippy::cast_slice_from_raw_parts)]

fn require_raw_slice_ptr<T>(_: *const [T]) {}

#[allow(unused_imports, unused_unsafe)]
fn main() {
let mut vec = vec![0u8; 1];
let ptr: *const u8 = vec.as_ptr();
let mptr = vec.as_mut_ptr();
let _: *const [u8] = unsafe { core::ptr::slice_from_raw_parts(ptr, 1) };
let _: *const [u8] = unsafe { std::ptr::slice_from_raw_parts(ptr, 1) };
//~^ cast_slice_from_raw_parts
let _: *const [u8] = unsafe { core::ptr::slice_from_raw_parts_mut(mptr, 1) };
let _: *const [u8] = unsafe { std::ptr::slice_from_raw_parts_mut(mptr, 1) };
//~^ cast_slice_from_raw_parts
let _: *const [u8] = core::ptr::slice_from_raw_parts(ptr, 1);
let _: *const [u8] = std::ptr::slice_from_raw_parts(ptr, 1);
//~^ cast_slice_from_raw_parts
{
use core::slice;
let _: *const [u8] = core::ptr::slice_from_raw_parts(ptr, 1);
let _: *const [u8] = std::ptr::slice_from_raw_parts(ptr, 1);
//~^ cast_slice_from_raw_parts
use slice as one;
let _: *const [u8] = core::ptr::slice_from_raw_parts(ptr, 1);
let _: *const [u8] = std::ptr::slice_from_raw_parts(ptr, 1);
//~^ cast_slice_from_raw_parts
}
{
use std::slice;
let _: *const [u8] = core::ptr::slice_from_raw_parts(ptr, 1);
let _: *const [u8] = std::ptr::slice_from_raw_parts(ptr, 1);
//~^ cast_slice_from_raw_parts
use slice as one;
let _: *const [u8] = core::ptr::slice_from_raw_parts(ptr, 1);
let _: *const [u8] = std::ptr::slice_from_raw_parts(ptr, 1);
//~^ cast_slice_from_raw_parts
}

// implicit cast
{
let _: *const [u8] = unsafe { std::ptr::slice_from_raw_parts(ptr, 1) };
//~^ cast_slice_from_raw_parts
let _: *mut [u8] = unsafe { std::ptr::slice_from_raw_parts_mut(mptr, 1) };
//~^ cast_slice_from_raw_parts
require_raw_slice_ptr(unsafe { std::ptr::slice_from_raw_parts(ptr, 1) });
//~^ cast_slice_from_raw_parts
}
}
12 changes: 12 additions & 0 deletions tests/ui/cast_raw_slice_pointer_cast.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![warn(clippy::cast_slice_from_raw_parts)]

fn require_raw_slice_ptr<T>(_: *const [T]) {}

#[allow(unused_imports, unused_unsafe)]
fn main() {
let mut vec = vec![0u8; 1];
Expand Down Expand Up @@ -27,4 +29,14 @@ fn main() {
let _: *const [u8] = unsafe { one::from_raw_parts(ptr, 1) } as *const [u8];
//~^ cast_slice_from_raw_parts
}

// implicit cast
{
let _: *const [u8] = unsafe { std::slice::from_raw_parts(ptr, 1) };
//~^ cast_slice_from_raw_parts
let _: *mut [u8] = unsafe { std::slice::from_raw_parts_mut(mptr, 1) };
//~^ cast_slice_from_raw_parts
require_raw_slice_ptr(unsafe { std::slice::from_raw_parts(ptr, 1) });
//~^ cast_slice_from_raw_parts
}
}
48 changes: 33 additions & 15 deletions tests/ui/cast_raw_slice_pointer_cast.stderr
Original file line number Diff line number Diff line change
@@ -1,47 +1,65 @@
error: casting the result of `from_raw_parts` to *const [u8]
--> tests/ui/cast_raw_slice_pointer_cast.rs:8:35
--> tests/ui/cast_raw_slice_pointer_cast.rs:10:35
|
LL | let _: *const [u8] = unsafe { std::slice::from_raw_parts(ptr, 1) as *const [u8] };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `core::ptr::slice_from_raw_parts(ptr, 1)`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `std::ptr::slice_from_raw_parts(ptr, 1)`
|
= note: `-D clippy::cast-slice-from-raw-parts` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::cast_slice_from_raw_parts)]`

error: casting the result of `from_raw_parts_mut` to *mut [u8]
--> tests/ui/cast_raw_slice_pointer_cast.rs:10:35
--> tests/ui/cast_raw_slice_pointer_cast.rs:12:35
|
LL | let _: *const [u8] = unsafe { std::slice::from_raw_parts_mut(mptr, 1) as *mut [u8] };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `core::ptr::slice_from_raw_parts_mut(mptr, 1)`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `std::ptr::slice_from_raw_parts_mut(mptr, 1)`

error: casting the result of `from_raw_parts` to *const [u8]
--> tests/ui/cast_raw_slice_pointer_cast.rs:12:26
--> tests/ui/cast_raw_slice_pointer_cast.rs:14:26
|
LL | let _: *const [u8] = unsafe { std::slice::from_raw_parts(ptr, 1) } as *const [u8];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `core::ptr::slice_from_raw_parts(ptr, 1)`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `std::ptr::slice_from_raw_parts(ptr, 1)`

error: casting the result of `from_raw_parts` to *const [u8]
--> tests/ui/cast_raw_slice_pointer_cast.rs:16:30
--> tests/ui/cast_raw_slice_pointer_cast.rs:18:30
|
LL | let _: *const [u8] = unsafe { slice::from_raw_parts(ptr, 1) } as *const [u8];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `core::ptr::slice_from_raw_parts(ptr, 1)`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `std::ptr::slice_from_raw_parts(ptr, 1)`

error: casting the result of `from_raw_parts` to *const [u8]
--> tests/ui/cast_raw_slice_pointer_cast.rs:19:30
--> tests/ui/cast_raw_slice_pointer_cast.rs:21:30
|
LL | let _: *const [u8] = unsafe { one::from_raw_parts(ptr, 1) } as *const [u8];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `core::ptr::slice_from_raw_parts(ptr, 1)`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `std::ptr::slice_from_raw_parts(ptr, 1)`

error: casting the result of `from_raw_parts` to *const [u8]
--> tests/ui/cast_raw_slice_pointer_cast.rs:24:30
--> tests/ui/cast_raw_slice_pointer_cast.rs:26:30
|
LL | let _: *const [u8] = unsafe { slice::from_raw_parts(ptr, 1) } as *const [u8];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `core::ptr::slice_from_raw_parts(ptr, 1)`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `std::ptr::slice_from_raw_parts(ptr, 1)`

error: casting the result of `from_raw_parts` to *const [u8]
--> tests/ui/cast_raw_slice_pointer_cast.rs:27:30
--> tests/ui/cast_raw_slice_pointer_cast.rs:29:30
|
LL | let _: *const [u8] = unsafe { one::from_raw_parts(ptr, 1) } as *const [u8];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `core::ptr::slice_from_raw_parts(ptr, 1)`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `std::ptr::slice_from_raw_parts(ptr, 1)`

error: implicitly casting the result of `from_raw_parts` to `*const [u8]`
--> tests/ui/cast_raw_slice_pointer_cast.rs:35:39
|
LL | let _: *const [u8] = unsafe { std::slice::from_raw_parts(ptr, 1) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace_with: `std::ptr::slice_from_raw_parts(ptr, 1)`

error: implicitly casting the result of `from_raw_parts_mut` to `*mut [u8]`
--> tests/ui/cast_raw_slice_pointer_cast.rs:37:37
|
LL | let _: *mut [u8] = unsafe { std::slice::from_raw_parts_mut(mptr, 1) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace_with: `std::ptr::slice_from_raw_parts_mut(mptr, 1)`

error: implicitly casting the result of `from_raw_parts` to `*const [u8]`
--> tests/ui/cast_raw_slice_pointer_cast.rs:39:40
|
LL | require_raw_slice_ptr(unsafe { std::slice::from_raw_parts(ptr, 1) });
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace_with: `std::ptr::slice_from_raw_parts(ptr, 1)`

error: aborting due to 7 previous errors
error: aborting due to 10 previous errors