Clippy lints / Suspicious
Clippy lint · Suspicious · checked with Clippy 0.1.99
clippy::missing_transmute_annotations: Checks if transmute calls have all generics specified
What it meansChecks if transmute calls have all generics specified. It is in the suspicious group and warns by default.
Does it matter?
If not, one or more unexpected types could be used during transmute(), potentially leading to Undefined Behavior or other problems. This is particularly dangerous in case a seemingly innocent/unrelated change causes type inference to result in a different type. For example, if transmute() is the tail expression of an if-branch, and the else-branch type changes, the compiler may silently infer a different type to be returned by transmute(). That is because the compiler is free to change the inference of a type as long as that inference is technically correct, regardless of the programmer’s unknown expectation. Both type-parameters, the input- and the output-type, to any transmute() should be given explicitly: Setting the input-type explicitly avoids confusion about what the argument’s type actually is. Setting the output-type explicitly avoids type-inference to infer a technically correct yet unexpected type.
Code that is most likely wrong or useless. Warns by default.
How to fix it
Clippy does not fix this one automatically (its suggestion may change behaviour, so it only shows it). Change the code by hand:
Flagged
let mut x: i32 = 0;
// Avoid "naked" calls to `transmute()`!
x = std::mem::transmute([1u16, 2u16]);
// `first_answers` is intended to transmute a slice of bool to a slice of u8.
// But the programmer forgot to index the first element of the outer slice,
// so we are actually transmuting from "pointers to slices" instead of
// transmuting from "a slice of bool", causing a nonsensical result.
let the_answers: &[&[bool]] = &[&[true, false, true]];
let first_answers: &[u8] = std::mem::transmute(the_answers);
Write this instead
let x = std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]);
// The explicit type parameters on `transmute()` makes the intention clear,
// and cause a type-error if the actual types don't match our expectation.
let the_answers: &[&[bool]] = &[&[true, false, true]];
let first_answers: &[u8] = std::mem::transmute::<&[bool], &[u8]>(the_answers[0]);
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::missing_transmute_annotations)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
missing_transmute_annotations = "allow"One run
cargo clippy -- -A clippy::missing-transmute-annotationsThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
What it does
Checks if transmute calls have all generics specified.
Why is this bad?
If not, one or more unexpected types could be used during transmute(), potentially leading
to Undefined Behavior or other problems.
This is particularly dangerous in case a seemingly innocent/unrelated change causes type
inference to result in a different type. For example, if transmute() is the tail
expression of an if-branch, and the else-branch type changes, the compiler may silently
infer a different type to be returned by transmute(). That is because the compiler is
free to change the inference of a type as long as that inference is technically correct,
regardless of the programmer’s unknown expectation.
Both type-parameters, the input- and the output-type, to any transmute() should
be given explicitly: Setting the input-type explicitly avoids confusion about what the
argument’s type actually is. Setting the output-type explicitly avoids type-inference
to infer a technically correct yet unexpected type.
Example
let mut x: i32 = 0;
// Avoid "naked" calls to `transmute()`!
x = std::mem::transmute([1u16, 2u16]);
// `first_answers` is intended to transmute a slice of bool to a slice of u8.
// But the programmer forgot to index the first element of the outer slice,
// so we are actually transmuting from "pointers to slices" instead of
// transmuting from "a slice of bool", causing a nonsensical result.
let the_answers: &[&[bool]] = &[&[true, false, true]];
let first_answers: &[u8] = std::mem::transmute(the_answers);
Use instead:
let x = std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]);
// The explicit type parameters on `transmute()` makes the intention clear,
// and cause a type-error if the actual types don't match our expectation.
let the_answers: &[&[bool]] = &[&[true, false, true]];
let first_answers: &[u8] = std::mem::transmute::<&[bool], &[u8]>(the_answers[0]);
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- mismatched_bit_width_typepossible buggy implementation of `bit_width`
- misnamed_gettersgetter function appears to return the wrong field
- misrefactored_assign_opvariable appears on both sides of an assignment operation
- multi_assignmentsassignments don't nest intuitively
- mut_range_boundattempt to mutate range bound within loop
- mutable_key_typeChecks for sets/maps with mutable key types
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::mismatched_bit_width_typepossible buggy implementation of `bit_width`Read it next →Did this answer your question?