1 use crate::utils::{is_copy, match_def_path, paths, qpath_res, span_note_and_lint};
2 use if_chain::if_chain;
3 use rustc::declare_lint_pass;
5 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
7 use rustc_session::declare_tool_lint;
10 /// **What it does:** Checks for calls to `std::mem::drop` with a reference
11 /// instead of an owned value.
13 /// **Why is this bad?** Calling `drop` on a reference will only drop the
14 /// reference itself, which is a no-op. It will not call the `drop` method (from
15 /// the `Drop` trait implementation) on the underlying referenced value, which
16 /// is likely what was intended.
18 /// **Known problems:** None.
22 /// let mut lock_guard = mutex.lock();
23 /// std::mem::drop(&lock_guard) // Should have been drop(lock_guard), mutex
25 /// operation_that_requires_mutex_to_be_unlocked();
29 "calls to `std::mem::drop` with a reference instead of an owned value"
32 declare_clippy_lint! {
33 /// **What it does:** Checks for calls to `std::mem::forget` with a reference
34 /// instead of an owned value.
36 /// **Why is this bad?** Calling `forget` on a reference will only forget the
37 /// reference itself, which is a no-op. It will not forget the underlying
39 /// value, which is likely what was intended.
41 /// **Known problems:** None.
45 /// let x = Box::new(1);
46 /// std::mem::forget(&x) // Should have been forget(x), x will still be dropped
50 "calls to `std::mem::forget` with a reference instead of an owned value"
53 declare_clippy_lint! {
54 /// **What it does:** Checks for calls to `std::mem::drop` with a value
55 /// that derives the Copy trait
57 /// **Why is this bad?** Calling `std::mem::drop` [does nothing for types that
58 /// implement Copy](https://doc.rust-lang.org/std/mem/fn.drop.html), since the
59 /// value will be copied and moved into the function on invocation.
61 /// **Known problems:** None.
65 /// let x: i32 = 42; // i32 implements Copy
66 /// std::mem::drop(x) // A copy of x is passed to the function, leaving the
67 /// // original unaffected
71 "calls to `std::mem::drop` with a value that implements Copy"
74 declare_clippy_lint! {
75 /// **What it does:** Checks for calls to `std::mem::forget` with a value that
76 /// derives the Copy trait
78 /// **Why is this bad?** Calling `std::mem::forget` [does nothing for types that
79 /// implement Copy](https://doc.rust-lang.org/std/mem/fn.drop.html) since the
80 /// value will be copied and moved into the function on invocation.
82 /// An alternative, but also valid, explanation is that Copy types do not
84 /// the Drop trait, which means they have no destructors. Without a destructor,
86 /// is nothing for `std::mem::forget` to ignore.
88 /// **Known problems:** None.
92 /// let x: i32 = 42; // i32 implements Copy
93 /// std::mem::forget(x) // A copy of x is passed to the function, leaving the
94 /// // original unaffected
98 "calls to `std::mem::forget` with a value that implements Copy"
101 const DROP_REF_SUMMARY: &str = "calls to `std::mem::drop` with a reference instead of an owned value. \
102 Dropping a reference does nothing.";
103 const FORGET_REF_SUMMARY: &str = "calls to `std::mem::forget` with a reference instead of an owned value. \
104 Forgetting a reference does nothing.";
105 const DROP_COPY_SUMMARY: &str = "calls to `std::mem::drop` with a value that implements Copy. \
106 Dropping a copy leaves the original intact.";
107 const FORGET_COPY_SUMMARY: &str = "calls to `std::mem::forget` with a value that implements Copy. \
108 Forgetting a copy leaves the original intact.";
110 declare_lint_pass!(DropForgetRef => [DROP_REF, FORGET_REF, DROP_COPY, FORGET_COPY]);
112 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DropForgetRef {
113 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
115 if let ExprKind::Call(ref path, ref args) = expr.kind;
116 if let ExprKind::Path(ref qpath) = path.kind;
118 if let Some(def_id) = qpath_res(cx, qpath, path.hir_id).opt_def_id();
123 let arg_ty = cx.tables.expr_ty(arg);
125 if let ty::Ref(..) = arg_ty.kind {
126 if match_def_path(cx, def_id, &paths::DROP) {
128 msg = DROP_REF_SUMMARY.to_string();
129 } else if match_def_path(cx, def_id, &paths::MEM_FORGET) {
131 msg = FORGET_REF_SUMMARY.to_string();
135 span_note_and_lint(cx,
140 &format!("argument has type {}", arg_ty));
141 } else if is_copy(cx, arg_ty) {
142 if match_def_path(cx, def_id, &paths::DROP) {
144 msg = DROP_COPY_SUMMARY.to_string();
145 } else if match_def_path(cx, def_id, &paths::MEM_FORGET) {
147 msg = FORGET_COPY_SUMMARY.to_string();
151 span_note_and_lint(cx,
156 &format!("argument has type {}", arg_ty));