Skip to content
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
13 changes: 11 additions & 2 deletions clippy_lints/src/casts/ptr_cast_constness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use clippy_utils::source::snippet_with_applicability;
use clippy_utils::sugg::Sugg;
use clippy_utils::{std_or_core, sym};
use rustc_errors::Applicability;
use rustc_hir::{Expr, ExprKind, QPath};
use rustc_hir::{self as hir, Expr, ExprKind, QPath};
use rustc_lint::LateContext;
use rustc_middle::ty::{self, Ty, TypeVisitableExt};

Expand Down Expand Up @@ -51,7 +51,16 @@ pub(super) fn check<'tcx>(

if msrv.meets(cx, msrvs::POINTER_CAST_CONSTNESS) {
let mut app = Applicability::MachineApplicable;
let sugg = Sugg::hir_with_context(cx, cast_from_expr, expr.span.ctxt(), "_", &mut app);
let sugg = if let ExprKind::Cast(nested_from, nested_hir_ty) = cast_from_expr.kind
&& let hir::TyKind::Ptr(ptr_ty) = nested_hir_ty.kind
&& let hir::TyKind::Infer(()) = ptr_ty.ty.kind
{
// `(foo as *const _).cast_mut()` fails method name resolution
// avoid this by `as`-ing the full type
Sugg::hir_with_context(cx, nested_from, expr.span.ctxt(), "_", &mut app).as_ty(cast_from)
} else {
Sugg::hir_with_context(cx, cast_from_expr, expr.span.ctxt(), "_", &mut app)
};
let constness = to_mutbl.ptr_str();

span_lint_and_sugg(
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/ptr_cast_constness.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,9 @@ fn issue14621() {
let _ = std::ptr::addr_of_mut!(local).cast_const();
//~^ ptr_cast_constness
}

fn issue11317() {
let r = &0_u32;
let _ptr: *mut u32 = (r as *const u32).cast_mut();
//~^ ptr_cast_constness
}
6 changes: 6 additions & 0 deletions tests/ui/ptr_cast_constness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,9 @@ fn issue14621() {
let _ = std::ptr::addr_of_mut!(local) as *const _;
//~^ ptr_cast_constness
}

fn issue11317() {
let r = &0_u32;
let _ptr: *mut u32 = r as *const _ as *mut _;
//~^ ptr_cast_constness
}
8 changes: 7 additions & 1 deletion tests/ui/ptr_cast_constness.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,11 @@ error: `as` casting between raw pointers while changing only its constness
LL | let _ = std::ptr::addr_of_mut!(local) as *const _;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast_const`, a safer alternative: `std::ptr::addr_of_mut!(local).cast_const()`

error: aborting due to 14 previous errors
error: `as` casting between raw pointers while changing only its constness
--> tests/ui/ptr_cast_constness.rs:112:26
|
LL | let _ptr: *mut u32 = r as *const _ as *mut _;
| ^^^^^^^^^^^^^^^^^^^^^^^ help: try `pointer::cast_mut`, a safer alternative: `(r as *const u32).cast_mut()`

error: aborting due to 15 previous errors