@@ -9,6 +9,7 @@ mod transmute_ptr_to_ref;
9
9
mod transmute_ref_to_ref;
10
10
mod transmute_undefined_repr;
11
11
mod transmutes_expressible_as_ptr_casts;
12
+ mod transmuting_null;
12
13
mod unsound_collection_transmute;
13
14
mod useless_transmute;
14
15
mod utils;
@@ -386,6 +387,28 @@ declare_clippy_lint! {
386
387
"transmute to or from a type with an undefined representation"
387
388
}
388
389
390
+ declare_clippy_lint ! {
391
+ /// ### What it does
392
+ /// Checks for transmute calls which would receive a null pointer.
393
+ ///
394
+ /// ### Why is this bad?
395
+ /// Transmuting a null pointer is undefined behavior.
396
+ ///
397
+ /// ### Known problems
398
+ /// Not all cases can be detected at the moment of this writing.
399
+ /// For example, variables which hold a null pointer and are then fed to a `transmute`
400
+ /// call, aren't detectable yet.
401
+ ///
402
+ /// ### Example
403
+ /// ```rust
404
+ /// let null_ref: &u64 = unsafe { std::mem::transmute(0 as *const u64) };
405
+ /// ```
406
+ #[ clippy:: version = "1.35.0" ]
407
+ pub TRANSMUTING_NULL ,
408
+ correctness,
409
+ "transmutes from a null pointer to a reference, which is undefined behavior"
410
+ }
411
+
389
412
pub struct Transmute {
390
413
msrv : Option < RustcVersion > ,
391
414
}
@@ -404,6 +427,7 @@ impl_lint_pass!(Transmute => [
404
427
UNSOUND_COLLECTION_TRANSMUTE ,
405
428
TRANSMUTES_EXPRESSIBLE_AS_PTR_CASTS ,
406
429
TRANSMUTE_UNDEFINED_REPR ,
430
+ TRANSMUTING_NULL ,
407
431
] ) ;
408
432
impl Transmute {
409
433
#[ must_use]
@@ -436,6 +460,7 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
436
460
437
461
let linted = wrong_transmute:: check( cx, e, from_ty, to_ty)
438
462
| crosspointer_transmute:: check( cx, e, from_ty, to_ty)
463
+ | transmuting_null:: check( cx, e, arg, to_ty)
439
464
| transmute_ptr_to_ref:: check( cx, e, from_ty, to_ty, arg, path, self . msrv)
440
465
| transmute_int_to_char:: check( cx, e, from_ty, to_ty, arg, const_context)
441
466
| transmute_ref_to_ref:: check( cx, e, from_ty, to_ty, arg, const_context)
0 commit comments