4 use utils
::{is_copy, match_def_path, opt_def_id, paths, span_note_and_lint}
;
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
21 /// operation_that_requires_mutex_to_be_unlocked();
26 "calls to `std::mem::drop` with a reference instead of an owned value"
29 /// **What it does:** Checks for calls to `std::mem::forget` with a reference
30 /// instead of an owned value.
32 /// **Why is this bad?** Calling `forget` on a reference will only forget the
33 /// reference itself, which is a no-op. It will not forget the underlying
35 /// value, which is likely what was intended.
37 /// **Known problems:** None.
41 /// let x = Box::new(1);
42 /// std::mem::forget(&x) // Should have been forget(x), x will still be dropped
47 "calls to `std::mem::forget` with a reference instead of an owned value"
50 /// **What it does:** Checks for calls to `std::mem::drop` with a value
51 /// that derives the Copy trait
53 /// **Why is this bad?** Calling `std::mem::drop` [does nothing for types that
54 /// implement Copy](https://doc.rust-lang.org/std/mem/fn.drop.html), since the
55 /// value will be copied and moved into the function on invocation.
57 /// **Known problems:** None.
61 /// let x:i32 = 42; // i32 implements Copy
62 /// std::mem::drop(x) // A copy of x is passed to the function, leaving the
63 /// original unaffected
68 "calls to `std::mem::drop` with a value that implements Copy"
71 /// **What it does:** Checks for calls to `std::mem::forget` with a value that
72 /// derives the Copy trait
74 /// **Why is this bad?** Calling `std::mem::forget` [does nothing for types that
75 /// implement Copy](https://doc.rust-lang.org/std/mem/fn.drop.html) since the
76 /// value will be copied and moved into the function on invocation.
78 /// An alternative, but also valid, explanation is that Copy types do not
80 /// the Drop trait, which means they have no destructors. Without a destructor,
82 /// is nothing for `std::mem::forget` to ignore.
84 /// **Known problems:** None.
88 /// let x:i32 = 42; // i32 implements Copy
89 /// std::mem::forget(x) // A copy of x is passed to the function, leaving the
90 /// original unaffected
95 "calls to `std::mem::forget` with a value that implements Copy"
98 const DROP_REF_SUMMARY
: &str = "calls to `std::mem::drop` with a reference instead of an owned value. \
99 Dropping a reference does nothing.";
100 const FORGET_REF_SUMMARY
: &str = "calls to `std::mem::forget` with a reference instead of an owned value. \
101 Forgetting a reference does nothing.";
102 const DROP_COPY_SUMMARY
: &str = "calls to `std::mem::drop` with a value that implements Copy. \
103 Dropping a copy leaves the original intact.";
104 const FORGET_COPY_SUMMARY
: &str = "calls to `std::mem::forget` with a value that implements Copy. \
105 Forgetting a copy leaves the original intact.";
107 #[allow(missing_copy_implementations)]
110 impl LintPass
for Pass
{
111 fn get_lints(&self) -> LintArray
{
112 lint_array
!(DROP_REF
, FORGET_REF
, DROP_COPY
, FORGET_COPY
)
116 impl<'a
, 'tcx
> LateLintPass
<'a
, 'tcx
> for Pass
{
117 fn check_expr(&mut self, cx
: &LateContext
<'a
, 'tcx
>, expr
: &'tcx Expr
) {
119 if let ExprCall(ref path
, ref args
) = expr
.node
;
120 if let ExprPath(ref qpath
) = path
.node
;
122 if let Some(def_id
) = opt_def_id(cx
.tables
.qpath_def(qpath
, path
.hir_id
));
127 let arg_ty
= cx
.tables
.expr_ty(arg
);
129 if let ty
::TyRef(..) = arg_ty
.sty
{
130 if match_def_path(cx
.tcx
, def_id
, &paths
::DROP
) {
132 msg
= DROP_REF_SUMMARY
.to_string();
133 } else if match_def_path(cx
.tcx
, def_id
, &paths
::MEM_FORGET
) {
135 msg
= FORGET_REF_SUMMARY
.to_string();
139 span_note_and_lint(cx
,
144 &format
!("argument has type {}", arg_ty
));
145 } else if is_copy(cx
, arg_ty
) {
146 if match_def_path(cx
.tcx
, def_id
, &paths
::DROP
) {
148 msg
= DROP_COPY_SUMMARY
.to_string();
149 } else if match_def_path(cx
.tcx
, def_id
, &paths
::MEM_FORGET
) {
151 msg
= FORGET_COPY_SUMMARY
.to_string();
155 span_note_and_lint(cx
,
160 &format
!("argument has type {}", arg_ty
));