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