Skip to content

Commit

Permalink
Handle Deref expressions in invalid_reference_casting
Browse files Browse the repository at this point in the history
  • Loading branch information
saethlin committed May 10, 2024
1 parent 8f9080d commit 2bb25d3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
6 changes: 4 additions & 2 deletions compiler/rustc_lint/src/reference_casting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,10 @@ fn is_cast_to_bigger_memory_layout<'tcx>(

// if the current expr looks like this `&mut expr[index]` then just looking
// at `expr[index]` won't give us the underlying allocation, so we just skip it
// the same logic applies field access like `&mut expr.field`
if let ExprKind::Index(..) | ExprKind::Field(..) = e_alloc.kind {
// the same logic applies field access `&mut expr.field` and reborrows `&mut *expr`.
if let ExprKind::Index(..) | ExprKind::Field(..) | ExprKind::Unary(UnOp::Deref, ..) =
e_alloc.kind
{
return None;
}

Expand Down
7 changes: 7 additions & 0 deletions tests/ui/lint/reference_casting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,13 @@ unsafe fn bigger_layout() {
let ptr = r as *mut i32 as *mut Vec3<i32>;
unsafe { *ptr = Vec3(0, 0, 0) }
}

unsafe fn deref(v: &mut Vec3<i32>) {
let r = &mut v.0;
let r = &mut *r;
let ptr = &mut *(r as *mut i32 as *mut Vec3<i32>);
unsafe { *ptr = Vec3(0, 0, 0) }
}
}

const RAW_PTR: *mut u8 = 1 as *mut u8;
Expand Down

0 comments on commit 2bb25d3

Please sign in to comment.