Skip to content

Fix as_ptr_cast_mut FP when self is not mutable #15266

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 1 commit 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
2 changes: 2 additions & 0 deletions clippy_lints/src/casts/as_ptr_cast_mut.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::is_mutable;
use clippy_utils::source::SpanRangeExt;
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind};
Expand All @@ -20,6 +21,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>,
&& let Some(first_param_ty) = as_ptr_sig.skip_binder().inputs().iter().next()
&& let ty::Ref(_, _, Mutability::Not) = first_param_ty.kind()
&& let Some(recv) = receiver.span.get_source_text(cx)
&& is_mutable(cx, receiver)
{
// `as_mut_ptr` might not exist
let applicability = Applicability::MaybeIncorrect;
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/as_ptr_cast_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,12 @@ fn main() {
let ref_with_write_perm = Covariant(std::ptr::addr_of_mut!(local) as *const _);
let _ = ref_with_write_perm.as_ptr() as *mut u8;
}

mod issue15259 {
use std::slice::from_raw_parts_mut;

#[allow(clippy::ptr_arg, clippy::mut_from_ref)]
pub fn as_mut_slice<T>(self_: &Vec<T>) -> &mut [T] {
unsafe { from_raw_parts_mut(self_.as_ptr() as *mut T, self_.len()) }
}
}
Loading