Closed
Description
This is an easy way to create UB. For example:
unsafe {
let bytes = [0u8; 4];
let num = *mem::transmute<_, &u32>(bytes);
}
The error (resulting in segfault) might be a little hard to spot. The problem is that the array is transmuted into a null pointer. The transmute argument should be &bytes
instead.
By forcing the programmer to add type annotations, he or she is not as likely to commit this mistake (since it will be catched during type checking):
unsafe {
let bytes = [0; 4];
let num = *mem::transmute<&[u8; 4], &u32>(&bytes);
}
Since this is a major source of bugs resulting in UB, I propose adding a lint being deny
by default, which forces the programmer to annotate the type.