1 use clippy_utils::diagnostics::span_lint;
2 use clippy_utils::{match_def_path, paths};
3 use rustc_hir::{Expr, ExprKind};
4 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_session::{declare_lint_pass, declare_tool_lint};
9 /// Checks for usage of `std::mem::forget(t)` where `t` is
12 /// ### Why is this bad?
13 /// `std::mem::forget(t)` prevents `t` from running its
14 /// destructor, possibly causing leaks.
19 /// # use std::rc::Rc;
20 /// mem::forget(Rc::new(55))
22 #[clippy::version = "pre 1.29.0"]
25 "`mem::forget` usage on `Drop` types, likely to cause memory leaks"
28 declare_lint_pass!(MemForget => [MEM_FORGET]);
30 impl<'tcx> LateLintPass<'tcx> for MemForget {
31 fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
32 if let ExprKind::Call(path_expr, [ref first_arg, ..]) = e.kind {
33 if let ExprKind::Path(ref qpath) = path_expr.kind {
34 if let Some(def_id) = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id() {
35 if match_def_path(cx, def_id, &paths::MEM_FORGET) {
36 let forgot_ty = cx.typeck_results().expr_ty(first_arg);
38 if forgot_ty.ty_adt_def().map_or(false, |def| def.has_dtor(cx.tcx)) {
39 span_lint(cx, MEM_FORGET, e.span, "usage of `mem::forget` on `Drop` type");