Change lint message to be stronger for &T -> &mut T transmute by 5225225 · Pull Request #92704 · rust-lang/rust (original) (raw)
The old message implied that it's only UB if you use the reference to mutate, which (as far as I know) is not true. As in, the following program has UB, and a &T -> &mut T transmute is effectively an unreachable_unchecked
.
fn main() { #[allow(mutable_transmutes)] unsafe { let _ = std::mem::transmute::<&i32, &mut i32>(&0); } }
In the future, it might be a good idea to use the edition system to make this a hard error, since I don't think it is ever defined behaviour? Unless we rule that &UnsafeCell<i32> -> &mut i32
is fine. (That, and you always could just use .get()
, so you're not losing anything)