1 use crate::utils::{is_copy, match_def_path, paths, qpath_res, span_lint_and_note};
2 use if_chain::if_chain;
3 use rustc_hir::{Expr, ExprKind};
4 use rustc_lint::{LateContext, LateLintPass};
6 use rustc_session::{declare_lint_pass, declare_tool_lint};
9 /// **What it does:** Checks for calls to `std::mem::drop` with a reference
10 /// instead of an owned value.
12 /// **Why is this bad?** Calling `drop` on a reference will only drop the
13 /// reference itself, which is a no-op. It will not call the `drop` method (from
14 /// the `Drop` trait implementation) on the underlying referenced value, which
15 /// is likely what was intended.
17 /// **Known problems:** None.
21 /// let mut lock_guard = mutex.lock();
22 /// std::mem::drop(&lock_guard) // Should have been drop(lock_guard), mutex
24 /// operation_that_requires_mutex_to_be_unlocked();
28 "calls to `std::mem::drop` with a reference instead of an owned value"
31 declare_clippy_lint! {
32 /// **What it does:** Checks for calls to `std::mem::forget` with a reference
33 /// instead of an owned value.
35 /// **Why is this bad?** Calling `forget` on a reference will only forget the
36 /// reference itself, which is a no-op. It will not forget the underlying
38 /// value, which is likely what was intended.
40 /// **Known problems:** None.
44 /// let x = Box::new(1);
45 /// std::mem::forget(&x) // Should have been forget(x), x will still be dropped
49 "calls to `std::mem::forget` with a reference instead of an owned value"
52 declare_clippy_lint! {
53 /// **What it does:** Checks for calls to `std::mem::drop` with a value
54 /// that derives the Copy trait
56 /// **Why is this bad?** Calling `std::mem::drop` [does nothing for types that
57 /// implement Copy](https://doc.rust-lang.org/std/mem/fn.drop.html), since the
58 /// value will be copied and moved into the function on invocation.
60 /// **Known problems:** None.
64 /// let x: i32 = 42; // i32 implements Copy
65 /// std::mem::drop(x) // A copy of x is passed to the function, leaving the
66 /// // original unaffected
70 "calls to `std::mem::drop` with a value that implements Copy"
73 declare_clippy_lint! {
74 /// **What it does:** Checks for calls to `std::mem::forget` with a value that
75 /// derives the Copy trait
77 /// **Why is this bad?** Calling `std::mem::forget` [does nothing for types that
78 /// implement Copy](https://doc.rust-lang.org/std/mem/fn.drop.html) since the
79 /// value will be copied and moved into the function on invocation.
81 /// An alternative, but also valid, explanation is that Copy types do not
83 /// the Drop trait, which means they have no destructors. Without a destructor,
85 /// is nothing for `std::mem::forget` to ignore.
87 /// **Known problems:** None.
91 /// let x: i32 = 42; // i32 implements Copy
92 /// std::mem::forget(x) // A copy of x is passed to the function, leaving the
93 /// // original unaffected
97 "calls to `std::mem::forget` with a value that implements Copy"
100 const DROP_REF_SUMMARY: &str = "calls to `std::mem::drop` with a reference instead of an owned value. \
101 Dropping a reference does nothing.";
102 const FORGET_REF_SUMMARY: &str = "calls to `std::mem::forget` with a reference instead of an owned value. \
103 Forgetting a reference does nothing.";
104 const DROP_COPY_SUMMARY: &str = "calls to `std::mem::drop` with a value that implements `Copy`. \
105 Dropping a copy leaves the original intact.";
106 const FORGET_COPY_SUMMARY: &str = "calls to `std::mem::forget` with a value that implements `Copy`. \
107 Forgetting a copy leaves the original intact.";
109 declare_lint_pass!(DropForgetRef => [DROP_REF, FORGET_REF, DROP_COPY, FORGET_COPY]);
111 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DropForgetRef {
112 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
114 if let ExprKind::Call(ref path, ref args) = expr.kind;
115 if let ExprKind::Path(ref qpath) = path.kind;
117 if let Some(def_id) = qpath_res(cx, qpath, path.hir_id).opt_def_id();
122 let arg_ty = cx.tables.expr_ty(arg);
124 if let ty::Ref(..) = arg_ty.kind {
125 if match_def_path(cx, def_id, &paths::DROP) {
127 msg = DROP_REF_SUMMARY.to_string();
128 } else if match_def_path(cx, def_id, &paths::MEM_FORGET) {
130 msg = FORGET_REF_SUMMARY.to_string();
134 span_lint_and_note(cx,
139 &format!("argument has type `{}`", arg_ty));
140 } else if is_copy(cx, arg_ty) {
141 if match_def_path(cx, def_id, &paths::DROP) {
143 msg = DROP_COPY_SUMMARY.to_string();
144 } else if match_def_path(cx, def_id, &paths::MEM_FORGET) {
146 msg = FORGET_COPY_SUMMARY.to_string();
150 span_lint_and_note(cx,
155 &format!("argument has type {}", arg_ty));