Skip to content

Commit 3a6b425

Browse files
add documentation
1 parent 2ad02bf commit 3a6b425

File tree

1 file changed

+14
-3
lines changed
  • clippy_lints/src/transmute

1 file changed

+14
-3
lines changed

Diff for: clippy_lints/src/transmute/mod.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -441,21 +441,32 @@ declare_clippy_lint! {
441441

442442
declare_clippy_lint! {
443443
/// ### What it does
444+
/// Checks for slice creation which has larger element before transmute.
444445
///
445446
/// ### Why is this bad?
447+
/// Creating those slices leads to out-of-bounds read, considered Undefined Behavior.
446448
///
447449
/// ### Example
448450
/// ```rust
449-
/// // example code where clippy issues a warning
451+
/// let i8_slice: &[i8] = &[1i8, 2, 3, 4];
452+
// let i32_slice: &[i32] = unsafe { std::mem::transmute(i8_slice) };
450453
/// ```
454+
///
451455
/// Use instead:
456+
///
457+
/// ```rust
458+
/// let i32_slice: &[i32] = i8_slice.iter().map(|item| unsafe { std::mem::transmute(item) }).collect::<Vec<_>>().to_slice();
459+
/// ```
460+
///
461+
/// or, alternatively:
462+
///
452463
/// ```rust
453-
/// // example code which does not raise clippy warning
464+
/// let i32_slice: &[i32] = std::slice::align_to::<i32>(i8_slice).1;
454465
/// ```
455466
#[clippy::version = "1.69.0"]
456467
pub TRANSMUTE_SLICE_TO_LARGER_ELEMENT_TYPE,
457468
correctness,
458-
"default lint description"
469+
"transmute leads out-of-bounds read, which is undefined behavior"
459470
}
460471

461472
pub struct Transmute {

0 commit comments

Comments
 (0)