diff options
| author | bors <bors@rust-lang.org> | 2024-09-11 12:07:13 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-09-11 12:07:13 +0000 |
| commit | 8be0b36687d8776ddb0dde19de63640be03d91f0 (patch) | |
| tree | cf77d7b100634d3e3080a5c7a299654f1eddbbf0 | |
| parent | 78bdd4508d1738e9e4f508a116af9ccf33edbdd4 (diff) | |
| parent | 7d2e6ebcbeb2ca161b4b0f8a88bd41bf4c1381f5 (diff) | |
| download | rust-8be0b36687d8776ddb0dde19de63640be03d91f0.tar.gz rust-8be0b36687d8776ddb0dde19de63640be03d91f0.zip | |
Auto merge of #13344 - lukaslueg:issue13339, r=Alexendoo
Expand missing_transmute_annotations docs Fixes #13339 changelog: [`missing_transmute_annotations `]: Expand docs, raison d'ĂȘtre
| -rw-r--r-- | clippy_lints/src/transmute/mod.rs | 32 |
1 files changed, 26 insertions, 6 deletions
diff --git a/clippy_lints/src/transmute/mod.rs b/clippy_lints/src/transmute/mod.rs index 373bf61d8ff..6e62668fd52 100644 --- a/clippy_lints/src/transmute/mod.rs +++ b/clippy_lints/src/transmute/mod.rs @@ -527,24 +527,44 @@ declare_clippy_lint! { /// Checks if transmute calls have all generics specified. /// /// ### Why is this bad? - /// If not set, some unexpected output type could be retrieved instead of the expected one, - /// potentially leading to invalid code. + /// 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 can cause type - /// inference to start inferring a different type. E.g. the transmute is the tail expression of - /// an `if` branch, and a different branches type changes, causing the transmute to silently - /// have a different type, instead of a proper error. + /// 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 /// ```no_run /// # unsafe { + /// // Avoid "naked" calls to `transmute()`! /// let x: i32 = 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: /// ```no_run /// # unsafe { /// 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]); /// # } /// ``` #[clippy::version = "1.79.0"] |
