Skip to content

Commit 2bb25d3

Browse files
committed
Handle Deref expressions in invalid_reference_casting
1 parent 8f9080d commit 2bb25d3

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

compiler/rustc_lint/src/reference_casting.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,10 @@ fn is_cast_to_bigger_memory_layout<'tcx>(
202202

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

tests/ui/lint/reference_casting.rs

+7
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,13 @@ unsafe fn bigger_layout() {
261261
let ptr = r as *mut i32 as *mut Vec3<i32>;
262262
unsafe { *ptr = Vec3(0, 0, 0) }
263263
}
264+
265+
unsafe fn deref(v: &mut Vec3<i32>) {
266+
let r = &mut v.0;
267+
let r = &mut *r;
268+
let ptr = &mut *(r as *mut i32 as *mut Vec3<i32>);
269+
unsafe { *ptr = Vec3(0, 0, 0) }
270+
}
264271
}
265272

266273
const RAW_PTR: *mut u8 = 1 as *mut u8;

0 commit comments

Comments
 (0)