File tree 1 file changed +14
-3
lines changed
clippy_lints/src/transmute
1 file changed +14
-3
lines changed Original file line number Diff line number Diff line change @@ -441,21 +441,32 @@ declare_clippy_lint! {
441
441
442
442
declare_clippy_lint ! {
443
443
/// ### What it does
444
+ /// Checks for slice creation which has larger element before transmute.
444
445
///
445
446
/// ### Why is this bad?
447
+ /// Creating those slices leads to out-of-bounds read, considered Undefined Behavior.
446
448
///
447
449
/// ### Example
448
450
/// ```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) };
450
453
/// ```
454
+ ///
451
455
/// 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
+ ///
452
463
/// ```rust
453
- /// // example code which does not raise clippy warning
464
+ /// let i32_slice: &[i32] = std::slice::align_to::<i32>(i8_slice).1;
454
465
/// ```
455
466
#[ clippy:: version = "1.69.0" ]
456
467
pub TRANSMUTE_SLICE_TO_LARGER_ELEMENT_TYPE ,
457
468
correctness,
458
- "default lint description "
469
+ "transmute leads out-of-bounds read, which is undefined behavior "
459
470
}
460
471
461
472
pub struct Transmute {
You can’t perform that action at this time.
0 commit comments