Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions clippy_lints/src/transmute/transmuting_null.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clippy_utils::consts::{ConstEvalCtxt, Constant};
use clippy_utils::diagnostics::span_lint;
use clippy_utils::is_integer_literal;
use clippy_utils::is_integer_const;
use clippy_utils::res::{MaybeDef, MaybeResPath};
use rustc_hir::{Expr, ExprKind};
use rustc_lint::LateContext;
Expand All @@ -27,7 +27,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, arg: &'t
// Catching:
// `std::mem::transmute(0 as *const i32)`
if let ExprKind::Cast(inner_expr, _cast_ty) = arg.kind
&& is_integer_literal(inner_expr, 0)
&& is_integer_const(cx, inner_expr, 0)
{
span_lint(cx, TRANSMUTING_NULL, expr.span, LINT_MSG);
return true;
Expand All @@ -42,10 +42,5 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, arg: &'t
return true;
}

// FIXME:
// Also catch transmutations of variables which are known nulls.
// To do this, MIR const propagation seems to be the better tool.
// Whenever MIR const prop routines are more developed, this will
// become available. As of this writing (25/03/19) it is not yet.
false
}
8 changes: 8 additions & 0 deletions tests/ui/transmuting_null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ fn transmute_const() {
}
}

fn transmute_const_int() {
unsafe {
let _: &u64 = std::mem::transmute(u64::MIN as *const u64);
//~^ transmuting_null
}
}

fn main() {
one_liners();
transmute_const();
transmute_const_int();
}
8 changes: 7 additions & 1 deletion tests/ui/transmuting_null.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,11 @@ error: transmuting a known null pointer into a reference
LL | let _: &u64 = std::mem::transmute(ZPTR);
| ^^^^^^^^^^^^^^^^^^^^^^^^^

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

error: aborting due to 4 previous errors