]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/mem_forget.rs
Fix false positive with cast_sign_loss lint
[rust.git] / clippy_lints / src / mem_forget.rs
1 use crate::utils::{match_def_path, paths, qpath_res, span_lint};
2 use rustc::declare_lint_pass;
3 use rustc::hir::{Expr, ExprKind};
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use rustc_session::declare_tool_lint;
6
7 declare_clippy_lint! {
8     /// **What it does:** Checks for usage of `std::mem::forget(t)` where `t` is
9     /// `Drop`.
10     ///
11     /// **Why is this bad?** `std::mem::forget(t)` prevents `t` from running its
12     /// destructor, possibly causing leaks.
13     ///
14     /// **Known problems:** None.
15     ///
16     /// **Example:**
17     /// ```rust
18     /// # use std::mem;
19     /// # use std::rc::Rc;
20     /// mem::forget(Rc::new(55))
21     /// ```
22     pub MEM_FORGET,
23     restriction,
24     "`mem::forget` usage on `Drop` types, likely to cause memory leaks"
25 }
26
27 declare_lint_pass!(MemForget => [MEM_FORGET]);
28
29 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MemForget {
30     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
31         if let ExprKind::Call(ref path_expr, ref args) = e.kind {
32             if let ExprKind::Path(ref qpath) = path_expr.kind {
33                 if let Some(def_id) = qpath_res(cx, qpath, path_expr.hir_id).opt_def_id() {
34                     if match_def_path(cx, def_id, &paths::MEM_FORGET) {
35                         let forgot_ty = cx.tables.expr_ty(&args[0]);
36
37                         if forgot_ty.ty_adt_def().map_or(false, |def| def.has_dtor(cx.tcx)) {
38                             span_lint(cx, MEM_FORGET, e.span, "usage of mem::forget on Drop type");
39                         }
40                     }
41                 }
42             }
43         }
44     }
45 }