Skip to content
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

Handle field projections like slice indexing in invalid_reference_casting #124908

Merged
Merged
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
3 changes: 2 additions & 1 deletion compiler/rustc_lint/src/reference_casting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ 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
if let ExprKind::Index(..) = e_alloc.kind {
// the same logic applies field access like `&mut expr.field`
if let ExprKind::Index(..) | ExprKind::Field(..) = e_alloc.kind {
return None;
}

Expand Down
6 changes: 6 additions & 0 deletions tests/ui/lint/reference_casting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ unsafe fn bigger_layout() {
let a3 = a2 as *mut u64;
unsafe { *a3 = 3 };
}

unsafe fn field_access(v: &mut Vec3<i32>) {
let r = &mut v.0;
let ptr = 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
Loading