]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/mem_forget.rs
Merge remote-tracking branch 'origin/beta_backport' into HEAD
[rust.git] / clippy_lints / src / mem_forget.rs
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};
5
6 declare_clippy_lint! {
7     /// **What it does:** Checks for usage of `std::mem::forget(t)` where `t` is
8     /// `Drop`.
9     ///
10     /// **Why is this bad?** `std::mem::forget(t)` prevents `t` from running its
11     /// destructor, possibly causing leaks.
12     ///
13     /// **Known problems:** None.
14     ///
15     /// **Example:**
16     /// ```rust
17     /// mem::forget(Rc::new(55))
18     /// ```
19     pub MEM_FORGET,
20     restriction,
21     "`mem::forget` usage on `Drop` types, likely to cause memory leaks"
22 }
23
24 declare_lint_pass!(MemForget => [MEM_FORGET]);
25
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]);
33
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");
36                         }
37                     }
38                 }
39             }
40         }
41     }
42 }