Skip to content

Commit 1db73d7

Browse files
committed
fix: as_ptr_cast_mut FP when self is not mutable
This can happen in `unsafe`
1 parent 7e2d26f commit 1db73d7

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

clippy_lints/src/casts/as_ptr_cast_mut.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::is_mutable;
23
use clippy_utils::source::SpanRangeExt;
34
use rustc_errors::Applicability;
45
use rustc_hir::{Expr, ExprKind};
@@ -20,6 +21,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>,
2021
&& let Some(first_param_ty) = as_ptr_sig.skip_binder().inputs().iter().next()
2122
&& let ty::Ref(_, _, Mutability::Not) = first_param_ty.kind()
2223
&& let Some(recv) = receiver.span.get_source_text(cx)
24+
&& is_mutable(cx, receiver)
2325
{
2426
// `as_mut_ptr` might not exist
2527
let applicability = Applicability::MaybeIncorrect;

tests/ui/as_ptr_cast_mut.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,12 @@ fn main() {
4040
let ref_with_write_perm = Covariant(std::ptr::addr_of_mut!(local) as *const _);
4141
let _ = ref_with_write_perm.as_ptr() as *mut u8;
4242
}
43+
44+
mod issue15259 {
45+
use std::slice::from_raw_parts_mut;
46+
47+
#[allow(clippy::ptr_arg, clippy::mut_from_ref)]
48+
pub fn as_mut_slice<T>(self_: &Vec<T>) -> &mut [T] {
49+
unsafe { from_raw_parts_mut(self_.as_ptr() as *mut T, self_.len()) }
50+
}
51+
}

0 commit comments

Comments
 (0)