1 use clippy_utils::diagnostics::span_lint_and_note;
2 use clippy_utils::ty::is_copy;
3 use clippy_utils::{match_def_path, paths};
4 use if_chain::if_chain;
5 use rustc_hir::{Expr, ExprKind};
6 use rustc_lint::{LateContext, LateLintPass};
8 use rustc_session::{declare_lint_pass, declare_tool_lint};
10 declare_clippy_lint! {
12 /// Checks for calls to `std::mem::drop` with a reference
13 /// instead of an owned value.
15 /// ### Why is this bad?
16 /// Calling `drop` on a reference will only drop the
17 /// reference itself, which is a no-op. It will not call the `drop` method (from
18 /// the `Drop` trait implementation) on the underlying referenced value, which
19 /// is likely what was intended.
23 /// let mut lock_guard = mutex.lock();
24 /// std::mem::drop(&lock_guard) // Should have been drop(lock_guard), mutex
26 /// operation_that_requires_mutex_to_be_unlocked();
30 "calls to `std::mem::drop` with a reference instead of an owned value"
33 declare_clippy_lint! {
35 /// Checks for calls to `std::mem::forget` with a reference
36 /// instead of an owned value.
38 /// ### Why is this bad?
39 /// Calling `forget` on a reference will only forget the
40 /// reference itself, which is a no-op. It will not forget the underlying
42 /// value, which is likely what was intended.
46 /// let x = Box::new(1);
47 /// std::mem::forget(&x) // Should have been forget(x), x will still be dropped
51 "calls to `std::mem::forget` with a reference instead of an owned value"
54 declare_clippy_lint! {
56 /// Checks for calls to `std::mem::drop` with a value
57 /// that derives the Copy trait
59 /// ### Why is this bad?
60 /// Calling `std::mem::drop` [does nothing for types that
61 /// implement Copy](https://doc.rust-lang.org/std/mem/fn.drop.html), since the
62 /// value will be copied and moved into the function on invocation.
66 /// let x: i32 = 42; // i32 implements Copy
67 /// std::mem::drop(x) // A copy of x is passed to the function, leaving the
68 /// // original unaffected
72 "calls to `std::mem::drop` with a value that implements Copy"
75 declare_clippy_lint! {
77 /// Checks for calls to `std::mem::forget` with a value that
78 /// derives the Copy trait
80 /// ### Why is this bad?
81 /// Calling `std::mem::forget` [does nothing for types that
82 /// implement Copy](https://doc.rust-lang.org/std/mem/fn.drop.html) since the
83 /// value will be copied and moved into the function on invocation.
85 /// An alternative, but also valid, explanation is that Copy types do not
87 /// the Drop trait, which means they have no destructors. Without a destructor,
89 /// is nothing for `std::mem::forget` to ignore.
93 /// let x: i32 = 42; // i32 implements Copy
94 /// std::mem::forget(x) // A copy of x is passed to the function, leaving the
95 /// // original unaffected
99 "calls to `std::mem::forget` with a value that implements Copy"
102 const DROP_REF_SUMMARY: &str = "calls to `std::mem::drop` with a reference instead of an owned value. \
103 Dropping a reference does nothing";
104 const FORGET_REF_SUMMARY: &str = "calls to `std::mem::forget` with a reference instead of an owned value. \
105 Forgetting a reference does nothing";
106 const DROP_COPY_SUMMARY: &str = "calls to `std::mem::drop` with a value that implements `Copy`. \
107 Dropping a copy leaves the original intact";
108 const FORGET_COPY_SUMMARY: &str = "calls to `std::mem::forget` with a value that implements `Copy`. \
109 Forgetting a copy leaves the original intact";
111 declare_lint_pass!(DropForgetRef => [DROP_REF, FORGET_REF, DROP_COPY, FORGET_COPY]);
113 impl<'tcx> LateLintPass<'tcx> for DropForgetRef {
114 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
116 if let ExprKind::Call(path, args) = expr.kind;
117 if let ExprKind::Path(ref qpath) = path.kind;
119 if let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id();
124 let arg_ty = cx.typeck_results().expr_ty(arg);
126 if let ty::Ref(..) = arg_ty.kind() {
127 if match_def_path(cx, def_id, &paths::DROP) {
129 msg = DROP_REF_SUMMARY.to_string();
130 } else if match_def_path(cx, def_id, &paths::MEM_FORGET) {
132 msg = FORGET_REF_SUMMARY.to_string();
136 span_lint_and_note(cx,
141 &format!("argument has type `{}`", arg_ty));
142 } else if is_copy(cx, arg_ty) {
143 if match_def_path(cx, def_id, &paths::DROP) {
145 msg = DROP_COPY_SUMMARY.to_string();
146 } else if match_def_path(cx, def_id, &paths::MEM_FORGET) {
148 msg = FORGET_COPY_SUMMARY.to_string();
152 span_lint_and_note(cx,
157 &format!("argument has type {}", arg_ty));