1 use crate::utils::{match_def_path, paths, span_lint};
2 use rustc::hir::{Expr, ExprKind};
3 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
4 use rustc::{declare_lint_pass, declare_tool_lint};
7 /// **What it does:** Checks for usage of `std::mem::forget(t)` where `t` is
10 /// **Why is this bad?** `std::mem::forget(t)` prevents `t` from running its
11 /// destructor, possibly causing leaks.
13 /// **Known problems:** None.
17 /// mem::forget(Rc::new(55))
21 "`mem::forget` usage on `Drop` types, likely to cause memory leaks"
24 declare_lint_pass!(MemForget => [MEM_FORGET]);
26 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MemForget {
27 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
28 if let ExprKind::Call(ref path_expr, ref args) = e.node {
29 if let ExprKind::Path(ref qpath) = path_expr.node {
30 if let Some(def_id) = cx.tables.qpath_res(qpath, path_expr.hir_id).opt_def_id() {
31 if match_def_path(cx, def_id, &*paths::MEM_FORGET) {
32 let forgot_ty = cx.tables.expr_ty(&args[0]);
34 if forgot_ty.ty_adt_def().map_or(false, |def| def.has_dtor(cx.tcx)) {
35 span_lint(cx, MEM_FORGET, e.span, "usage of mem::forget on Drop type");