1 use crate::utils::{paths, span_help_and_lint};
2 use if_chain::if_chain;
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
6 use rustc::{declare_lint_pass, declare_tool_lint};
9 /// **What it does:** Checks for creation of references to zeroed or uninitialized memory.
11 /// **Why is this bad?** Creation of null references is undefined behavior.
13 /// **Known problems:** None.
17 /// let bad_ref: &usize = unsafe { std::mem::zeroed() };
21 "creation of invalid reference"
24 const ZERO_REF_SUMMARY: &str = "reference to zeroed memory";
25 const UNINIT_REF_SUMMARY: &str = "reference to uninitialized memory";
26 const HELP: &str = "Creation of a null reference is undefined behavior; \
27 see https://doc.rust-lang.org/reference/behavior-considered-undefined.html";
29 declare_lint_pass!(InvalidRef => [INVALID_REF]);
31 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InvalidRef {
32 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
34 if let ExprKind::Call(ref path, ref args) = expr.node;
35 if let ExprKind::Path(ref qpath) = path.node;
37 if let ty::Ref(..) = cx.tables.expr_ty(expr).sty;
38 if let Some(def_id) = cx.tables.qpath_def(qpath, path.hir_id).opt_def_id();
40 let msg = if cx.match_def_path(def_id, &paths::MEM_ZEROED) |
41 cx.match_def_path(def_id, &paths::INIT)
44 } else if cx.match_def_path(def_id, &paths::MEM_UNINIT) |
45 cx.match_def_path(def_id, &paths::UNINIT)
51 span_help_and_lint(cx, INVALID_REF, expr.span, msg, HELP);