1 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
2 use rustc::{declare_tool_lint, lint_array};
3 use if_chain::if_chain;
6 use crate::utils::{is_copy, match_def_path, opt_def_id, paths, span_note_and_lint};
8 /// **What it does:** Checks for calls to `std::mem::drop` with a reference
9 /// instead of an owned value.
11 /// **Why is this bad?** Calling `drop` on a reference will only drop the
12 /// reference itself, which is a no-op. It will not call the `drop` method (from
13 /// the `Drop` trait implementation) on the underlying referenced value, which
14 /// is likely what was intended.
16 /// **Known problems:** None.
20 /// let mut lock_guard = mutex.lock();
21 /// std::mem::drop(&lock_guard) // Should have been drop(lock_guard), mutex
23 /// operation_that_requires_mutex_to_be_unlocked();
25 declare_clippy_lint! {
28 "calls to `std::mem::drop` with a reference instead of an owned value"
31 /// **What it does:** Checks for calls to `std::mem::forget` with a reference
32 /// instead of an owned value.
34 /// **Why is this bad?** Calling `forget` on a reference will only forget the
35 /// reference itself, which is a no-op. It will not forget the underlying
37 /// value, which is likely what was intended.
39 /// **Known problems:** None.
43 /// let x = Box::new(1);
44 /// std::mem::forget(&x) // Should have been forget(x), x will still be dropped
46 declare_clippy_lint! {
49 "calls to `std::mem::forget` with a reference instead of an owned value"
52 /// **What it does:** Checks for calls to `std::mem::drop` with a value
53 /// that derives the Copy trait
55 /// **Why is this bad?** Calling `std::mem::drop` [does nothing for types that
56 /// implement Copy](https://doc.rust-lang.org/std/mem/fn.drop.html), since the
57 /// value will be copied and moved into the function on invocation.
59 /// **Known problems:** None.
63 /// let x:i32 = 42; // i32 implements Copy
64 /// std::mem::drop(x) // A copy of x is passed to the function, leaving the
65 /// // original unaffected
67 declare_clippy_lint! {
70 "calls to `std::mem::drop` with a value that implements Copy"
73 /// **What it does:** Checks for calls to `std::mem::forget` with a value that
74 /// derives the Copy trait
76 /// **Why is this bad?** Calling `std::mem::forget` [does nothing for types that
77 /// implement Copy](https://doc.rust-lang.org/std/mem/fn.drop.html) since the
78 /// value will be copied and moved into the function on invocation.
80 /// An alternative, but also valid, explanation is that Copy types do not
82 /// the Drop trait, which means they have no destructors. Without a destructor,
84 /// is nothing for `std::mem::forget` to ignore.
86 /// **Known problems:** None.
90 /// let x:i32 = 42; // i32 implements Copy
91 /// std::mem::forget(x) // A copy of x is passed to the function, leaving the
92 /// // original unaffected
94 declare_clippy_lint! {
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 #[allow(missing_copy_implementations)]
112 impl LintPass for Pass {
113 fn get_lints(&self) -> LintArray {
114 lint_array!(DROP_REF, FORGET_REF, DROP_COPY, FORGET_COPY)
118 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
119 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
121 if let ExprKind::Call(ref path, ref args) = expr.node;
122 if let ExprKind::Path(ref qpath) = path.node;
124 if let Some(def_id) = opt_def_id(cx.tables.qpath_def(qpath, path.hir_id));
129 let arg_ty = cx.tables.expr_ty(arg);
131 if let ty::Ref(..) = arg_ty.sty {
132 if match_def_path(cx.tcx, def_id, &paths::DROP) {
134 msg = DROP_REF_SUMMARY.to_string();
135 } else if match_def_path(cx.tcx, def_id, &paths::MEM_FORGET) {
137 msg = FORGET_REF_SUMMARY.to_string();
141 span_note_and_lint(cx,
146 &format!("argument has type {}", arg_ty));
147 } else if is_copy(cx, arg_ty) {
148 if match_def_path(cx.tcx, def_id, &paths::DROP) {
150 msg = DROP_COPY_SUMMARY.to_string();
151 } else if match_def_path(cx.tcx, def_id, &paths::MEM_FORGET) {
153 msg = FORGET_COPY_SUMMARY.to_string();
157 span_note_and_lint(cx,
162 &format!("argument has type {}", arg_ty));