]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/drop_forget_ref.rs
Merge remote-tracking branch 'origin/master' into 1537-drop_copy
[rust.git] / clippy_lints / src / drop_forget_ref.rs
1 use rustc::lint::*;
2 use rustc::ty;
3 use rustc::hir::*;
4 use utils::{match_def_path, paths, span_note_and_lint, is_copy};
5
6 /// **What it does:** Checks for calls to `std::mem::drop` with a reference
7 /// instead of an owned value.
8 ///
9 /// **Why is this bad?** Calling `drop` on a reference will only drop the
10 /// reference itself, which is a no-op. It will not call the `drop` method (from
11 /// the `Drop` trait implementation) on the underlying referenced value, which
12 /// is likely what was intended.
13 ///
14 /// **Known problems:** None.
15 ///
16 /// **Example:**
17 /// ```rust
18 /// let mut lock_guard = mutex.lock();
19 /// std::mem::drop(&lock_guard) // Should have been drop(lock_guard), mutex still locked
20 /// operation_that_requires_mutex_to_be_unlocked();
21 /// ```
22 declare_lint! {
23     pub DROP_REF,
24     Warn,
25     "calls to `std::mem::drop` with a reference instead of an owned value"
26 }
27
28 /// **What it does:** Checks for calls to `std::mem::forget` with a reference
29 /// instead of an owned value.
30 ///
31 /// **Why is this bad?** Calling `forget` on a reference will only forget the
32 /// reference itself, which is a no-op. It will not forget the underlying referenced
33 /// value, which is likely what was intended.
34 ///
35 /// **Known problems:** None.
36 ///
37 /// **Example:**
38 /// ```rust
39 /// let x = Box::new(1);
40 /// std::mem::forget(&x) // Should have been forget(x), x will still be dropped
41 /// ```
42 declare_lint! {
43     pub FORGET_REF,
44     Warn,
45     "calls to `std::mem::forget` with a reference instead of an owned value"
46 }
47
48 /// **What it does:** Checks for calls to `std::mem::drop` with a value
49 /// that derives the Copy trait
50 ///
51 /// **Why is this bad?** Calling `std::mem::drop` [does nothing for types that
52 /// implement Copy](https://doc.rust-lang.org/std/mem/fn.drop.html), since the
53 /// value will be copied and moved into the function on invocation.
54 ///
55 /// **Known problems:** None.
56 ///
57 /// **Example:**
58 /// ```rust
59 /// let x:i32 = 42;   // i32 implements Copy
60 /// std::mem::drop(x) // A copy of x is passed to the function, leaving the original unaffected
61 /// ```
62 declare_lint! {
63     pub DROP_COPY,
64     Warn,
65     "calls to `std::mem::drop` with a value that implements Copy"
66 }
67
68 /// **What it does:** Checks for calls to `std::mem::forget` with a value that
69 /// derives the Copy trait
70 ///
71 /// **Why is this bad?** Calling `std::mem::forget` [does nothing for types that
72 /// implement Copy](https://doc.rust-lang.org/std/mem/fn.drop.html) since the
73 /// value will be copied and moved into the function on invocation.
74 ///
75 /// An alternative, but also valid, explanation is that Copy types do not implement
76 /// the Drop trait, which means they have no destructors. Without a destructor, there
77 /// is nothing for `std::mem::forget` to ignore.
78 ///
79 /// **Known problems:** None.
80 ///
81 /// **Example:**
82 /// ```rust
83 /// let x:i32 = 42;     // i32 implements Copy
84 /// std::mem::forget(x) // A copy of x is passed to the function, leaving the original unaffected
85 /// ```
86 declare_lint! {
87     pub FORGET_COPY,
88     Warn,
89     "calls to `std::mem::forget` with a value that implements Copy"
90 }
91
92 const DROP_REF_SUMMARY: &str = "calls to `std::mem::drop` with a reference instead of an owned value.
93                                Dropping a reference does nothing.";
94 const FORGET_REF_SUMMARY: &str = "calls to `std::mem::forget` with a reference instead of an owned value.
95                                  Forgetting a reference does nothing.";
96 const DROP_COPY_SUMMARY: &str = "calls to `std::mem::drop` with a value that implements Copy.
97                                 Dropping a copy leaves the original intact.";
98 const FORGET_COPY_SUMMARY: &str = "calls to `std::mem::forget` with a value that implements Copy.
99                                   Forgetting a copy leaves the original intact.";
100
101 #[allow(missing_copy_implementations)]
102 pub struct Pass;
103
104 impl LintPass for Pass {
105     fn get_lints(&self) -> LintArray {
106         lint_array!(DROP_REF, FORGET_REF)
107     }
108 }
109
110 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
111     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
112         if_let_chain!{[
113             let ExprCall(ref path, ref args) = expr.node,
114             let ExprPath(ref qpath) = path.node,
115             args.len() == 1,
116         ], {
117             let def_id = cx.tables.qpath_def(qpath, path.id).def_id();
118             let lint;
119             let msg;
120             let arg = &args[0];
121             let arg_ty = cx.tables.expr_ty(arg);
122
123             if let ty::TyRef(..) = arg_ty.sty {
124                 if match_def_path(cx.tcx, def_id, &paths::DROP) {
125                     lint = DROP_REF;
126                     msg = DROP_REF_SUMMARY.to_string();
127                 } else if match_def_path(cx.tcx, def_id, &paths::MEM_FORGET) {
128                     lint = FORGET_REF;
129                     msg = FORGET_REF_SUMMARY.to_string();
130                 } else {
131                     return;
132                 }
133                 span_note_and_lint(cx,
134                                    lint,
135                                    expr.span,
136                                    &msg,
137                                    arg.span,
138                                    &format!("argument has type {}", arg_ty.sty));
139             } else if is_copy(cx, arg_ty, cx.tcx.hir.get_parent(arg.id)) {
140                 if match_def_path(cx.tcx, def_id, &paths::DROP) {
141                     lint = DROP_COPY;
142                     msg = DROP_COPY_SUMMARY.to_string();
143                 } else if match_def_path(cx.tcx, def_id, &paths::MEM_FORGET) {
144                     lint = FORGET_COPY;
145                     msg = FORGET_COPY_SUMMARY.to_string();
146                 } else {
147                     return;
148                 }
149                 span_note_and_lint(cx,
150                                    lint,
151                                    expr.span,
152                                    &msg,
153                                    arg.span,
154                                    &format!("argument has type {}", arg_ty.sty));
155             }
156         }}
157     }
158 }