4 use utils::{match_def_path, paths, span_help_and_lint, opt_def_id};
6 /// **What it does:** Checks for creation of references to zeroed or uninitialized memory.
8 /// **Why is this bad?** Creation of null references is undefined behavior.
10 /// **Known problems:** None.
14 /// let bad_ref: &usize = std::mem::zeroed();
20 "creation of invalid reference"
23 const ZERO_REF_SUMMARY: &str = "reference to zeroed memory";
24 const UNINIT_REF_SUMMARY: &str = "reference to uninitialized memory";
25 const HELP: &str = "Creation of a null reference is undefined behavior; see https://doc.rust-lang.org/reference/behavior-considered-undefined.html";
27 pub struct InvalidRef;
29 impl LintPass for InvalidRef {
30 fn get_lints(&self) -> LintArray {
31 lint_array!(INVALID_REF)
35 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidRef {
36 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
38 let ExprCall(ref path, ref args) = expr.node,
39 let ExprPath(ref qpath) = path.node,
41 let ty::TyRef(..) = cx.tables.expr_ty(expr).sty,
42 let Some(def_id) = opt_def_id(cx.tables.qpath_def(qpath, path.hir_id)),
44 let msg = if match_def_path(cx.tcx, def_id, &paths::MEM_ZEROED) | match_def_path(cx.tcx, def_id, &paths::INIT) {
46 } else if match_def_path(cx.tcx, def_id, &paths::MEM_UNINIT) | match_def_path(cx.tcx, def_id, &paths::UNINIT) {
51 span_help_and_lint(cx, INVALID_REF, expr.span, msg, HELP);