Skip to content

Commit 8f9a93d

Browse files
committed
transmuting_null: Check const integer casts
changelog: [`transmuting_null`]: now checks const integers being casted to pointers
1 parent 9e3e964 commit 8f9a93d

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

clippy_lints/src/transmute/transmuting_null.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::consts::{ConstEvalCtxt, Constant};
22
use clippy_utils::diagnostics::span_lint;
3-
use clippy_utils::is_integer_literal;
3+
use clippy_utils::is_integer_const;
44
use clippy_utils::res::{MaybeDef, MaybeResPath};
55
use rustc_hir::{Expr, ExprKind};
66
use rustc_lint::LateContext;
@@ -27,7 +27,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, arg: &'t
2727
// Catching:
2828
// `std::mem::transmute(0 as *const i32)`
2929
if let ExprKind::Cast(inner_expr, _cast_ty) = arg.kind
30-
&& is_integer_literal(inner_expr, 0)
30+
&& is_integer_const(cx, inner_expr, 0)
3131
{
3232
span_lint(cx, TRANSMUTING_NULL, expr.span, LINT_MSG);
3333
return true;
@@ -42,10 +42,5 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, arg: &'t
4242
return true;
4343
}
4444

45-
// FIXME:
46-
// Also catch transmutations of variables which are known nulls.
47-
// To do this, MIR const propagation seems to be the better tool.
48-
// Whenever MIR const prop routines are more developed, this will
49-
// become available. As of this writing (25/03/19) it is not yet.
5045
false
5146
}

tests/ui/transmuting_null.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@ fn transmute_const() {
3030
}
3131
}
3232

33+
fn transmute_const_int() {
34+
unsafe {
35+
let _: &u64 = std::mem::transmute(u64::MIN as *const u64);
36+
//~^ transmuting_null
37+
}
38+
}
39+
3340
fn main() {
3441
one_liners();
3542
transmute_const();
43+
transmute_const_int();
3644
}

tests/ui/transmuting_null.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,11 @@ error: transmuting a known null pointer into a reference
1919
LL | let _: &u64 = std::mem::transmute(ZPTR);
2020
| ^^^^^^^^^^^^^^^^^^^^^^^^^
2121

22-
error: aborting due to 3 previous errors
22+
error: transmuting a known null pointer into a reference
23+
--> tests/ui/transmuting_null.rs:35:23
24+
|
25+
LL | let _: &u64 = std::mem::transmute(u64::MIN as *const u64);
26+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
27+
28+
error: aborting due to 4 previous errors
2329

0 commit comments

Comments
 (0)