]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/mem_forget.rs
rustfmt fallout in doc comments
[rust.git] / clippy_lints / src / mem_forget.rs
1 use rustc::lint::*;
2 use rustc::hir::{Expr, ExprCall, ExprPath};
3 use utils::{match_def_path, paths, span_lint};
4
5 /// **What it does:** Checks for usage of `std::mem::forget(t)` where `t` is `Drop`.
6 ///
7 /// **Why is this bad?** `std::mem::forget(t)` prevents `t` from running its
8 /// destructor, possibly causing leaks.
9 ///
10 /// **Known problems:** None.
11 ///
12 /// **Example:**
13 /// ```rust
14 /// mem::forget(Rc::new(55)))
15 /// ```
16 declare_lint! {
17     pub MEM_FORGET,
18     Allow,
19     "`mem::forget` usage on `Drop` types, likely to cause memory leaks"
20 }
21
22 pub struct MemForget;
23
24 impl LintPass for MemForget {
25     fn get_lints(&self) -> LintArray {
26         lint_array![MEM_FORGET]
27     }
28 }
29
30 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MemForget {
31     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
32         if let ExprCall(ref path_expr, ref args) = e.node {
33             if let ExprPath(ref qpath) = path_expr.node {
34                 let def_id = cx.tcx.tables().qpath_def(qpath, path_expr.id).def_id();
35                 if match_def_path(cx, def_id, &paths::MEM_FORGET) {
36                     let forgot_ty = cx.tcx.tables().expr_ty(&args[0]);
37
38                     if match forgot_ty.ty_adt_def() {
39                         Some(def) => def.has_dtor(),
40                         _ => false,
41                     } {
42                         span_lint(cx, MEM_FORGET, e.span, "usage of mem::forget on Drop type");
43                     }
44                 }
45             }
46         }
47     }
48 }