4 use utils::{match_def_path, paths, span_note_and_lint, is_copy};
6 /// **What it does:** Checks for calls to `std::mem::drop` with a reference
7 /// instead of an owned value.
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.
14 /// **Known problems:** None.
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();
25 "calls to `std::mem::drop` with a reference instead of an owned value"
28 /// **What it does:** Checks for calls to `std::mem::forget` with a reference
29 /// instead of an owned value.
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.
35 /// **Known problems:** None.
39 /// let x = Box::new(1);
40 /// std::mem::forget(&x) // Should have been forget(x), x will still be dropped
45 "calls to `std::mem::forget` with a reference instead of an owned value"
48 /// **What it does:** Checks for calls to `std::mem::drop` with a value
49 /// that derives the Copy trait
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.
55 /// **Known problems:** None.
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
65 "calls to `std::mem::drop` with a value that implements Copy"
68 /// **What it does:** Checks for calls to `std::mem::forget` with a value that
69 /// derives the Copy trait
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.
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.
79 /// **Known problems:** None.
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
89 "calls to `std::mem::forget` with a value that implements Copy"
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.";
101 #[allow(missing_copy_implementations)]
104 impl LintPass for Pass {
105 fn get_lints(&self) -> LintArray {
106 lint_array!(DROP_REF, FORGET_REF)
110 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
111 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
113 let ExprCall(ref path, ref args) = expr.node,
114 let ExprPath(ref qpath) = path.node,
117 let def_id = cx.tables.qpath_def(qpath, path.id).def_id();
121 let arg_ty = cx.tables.expr_ty(arg);
123 if let ty::TyRef(..) = arg_ty.sty {
124 if match_def_path(cx.tcx, def_id, &paths::DROP) {
126 msg = DROP_REF_SUMMARY.to_string();
127 } else if match_def_path(cx.tcx, def_id, &paths::MEM_FORGET) {
129 msg = FORGET_REF_SUMMARY.to_string();
133 span_note_and_lint(cx,
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) {
142 msg = DROP_COPY_SUMMARY.to_string();
143 } else if match_def_path(cx.tcx, def_id, &paths::MEM_FORGET) {
145 msg = FORGET_COPY_SUMMARY.to_string();
149 span_note_and_lint(cx,
154 &format!("argument has type {}", arg_ty.sty));