Skip to content

Commit 461f060

Browse files
authored
transmuting_null: Check single expression const blocks and blocks (rust-lang#16260)
changelog: [`transmuting_null`]: now checks const blocks and blocks with only a single expression
2 parents f07c787 + 558ff51 commit 461f060

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

clippy_lints/src/transmute/transmuting_null.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::consts::{ConstEvalCtxt, Constant};
22
use clippy_utils::diagnostics::span_lint;
33
use clippy_utils::is_integer_const;
44
use clippy_utils::res::{MaybeDef, MaybeResPath};
5-
use rustc_hir::{Expr, ExprKind};
5+
use rustc_hir::{ConstBlock, Expr, ExprKind};
66
use rustc_lint::LateContext;
77
use rustc_middle::ty::Ty;
88
use rustc_span::symbol::sym;
@@ -42,5 +42,23 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, arg: &'t
4242
return true;
4343
}
4444

45+
// Catching:
46+
// `std::mem::transmute({ 0 as *const u64 })` and similar const blocks
47+
if let ExprKind::Block(block, _) = arg.kind
48+
&& block.stmts.is_empty()
49+
&& let Some(inner) = block.expr
50+
{
51+
// Run again with the inner expression
52+
return check(cx, expr, inner, to_ty);
53+
}
54+
55+
// Catching:
56+
// `std::mem::transmute(const { u64::MIN as *const u64 });`
57+
if let ExprKind::ConstBlock(ConstBlock { body, .. }) = arg.kind {
58+
// Strip out the const and run again
59+
let block = cx.tcx.hir_body(body).value;
60+
return check(cx, expr, block, to_ty);
61+
}
62+
4563
false
4664
}

tests/ui/transmuting_null.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,19 @@ fn transmute_const_int() {
3737
}
3838
}
3939

40+
fn transumute_single_expr_blocks() {
41+
unsafe {
42+
let _: &u64 = std::mem::transmute({ 0 as *const u64 });
43+
//~^ transmuting_null
44+
45+
let _: &u64 = std::mem::transmute(const { u64::MIN as *const u64 });
46+
//~^ transmuting_null
47+
}
48+
}
49+
4050
fn main() {
4151
one_liners();
4252
transmute_const();
4353
transmute_const_int();
54+
transumute_single_expr_blocks();
4455
}

tests/ui/transmuting_null.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,17 @@ error: transmuting a known null pointer into a reference
2525
LL | let _: &u64 = std::mem::transmute(u64::MIN as *const u64);
2626
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2727

28-
error: aborting due to 4 previous errors
28+
error: transmuting a known null pointer into a reference
29+
--> tests/ui/transmuting_null.rs:42:23
30+
|
31+
LL | let _: &u64 = std::mem::transmute({ 0 as *const u64 });
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
33+
34+
error: transmuting a known null pointer into a reference
35+
--> tests/ui/transmuting_null.rs:45:23
36+
|
37+
LL | let _: &u64 = std::mem::transmute(const { u64::MIN as *const u64 });
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
39+
40+
error: aborting due to 6 previous errors
2941

0 commit comments

Comments
 (0)