4 use utils::{match_def_path, opt_def_id, paths, span_help_and_lint};
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();
19 "creation of invalid reference"
22 const ZERO_REF_SUMMARY: &str = "reference to zeroed memory";
23 const UNINIT_REF_SUMMARY: &str = "reference to uninitialized memory";
24 const HELP: &str = "Creation of a null reference is undefined behavior; \
25 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 if let ExprCall(ref path, ref args) = expr.node;
39 if let ExprPath(ref qpath) = path.node;
41 if let ty::TyRef(..) = cx.tables.expr_ty(expr).sty;
42 if 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) |
45 match_def_path(cx.tcx, def_id, &paths::INIT)
48 } else if match_def_path(cx.tcx, def_id, &paths::MEM_UNINIT) |
49 match_def_path(cx.tcx, def_id, &paths::UNINIT)
55 span_help_and_lint(cx, INVALID_REF, expr.span, msg, HELP);