2 use rustc::hir::{Expr, ExprCall, ExprPath};
3 use utils::{match_def_path, paths, span_lint};
5 /// **What it does:** Checks for usage of `std::mem::forget(t)` where `t` is `Drop`.
7 /// **Why is this bad?** `std::mem::forget(t)` prevents `t` from running its
8 /// destructor, possibly causing leaks.
10 /// **Known problems:** None.
14 /// mem::forget(Rc::new(55)))
19 "`mem::forget` usage on `Drop` types is likely to cause memory leaks"
24 impl LintPass for MemForget {
25 fn get_lints(&self) -> LintArray {
26 lint_array![MEM_FORGET]
30 impl LateLintPass for MemForget {
31 fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
32 if let ExprCall(ref path_expr, ref args) = e.node {
33 if let ExprPath(None, _) = path_expr.node {
34 let def_id = cx.tcx.expect_def(path_expr.id).def_id();
35 if match_def_path(cx, def_id, &paths::MEM_FORGET) {
36 let forgot_ty = cx.tcx.expr_ty(&args[0]);
38 if match forgot_ty.ty_adt_def() {
39 Some(def) => def.has_dtor(),
42 span_lint(cx, MEM_FORGET, e.span, "usage of mem::forget on Drop type");