1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
11 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
12 use crate::rustc::{declare_tool_lint, lint_array};
13 use if_chain::if_chain;
15 use crate::rustc::hir::*;
16 use crate::utils::{is_copy, match_def_path, opt_def_id, paths, span_note_and_lint};
18 /// **What it does:** Checks for calls to `std::mem::drop` with a reference
19 /// instead of an owned value.
21 /// **Why is this bad?** Calling `drop` on a reference will only drop the
22 /// reference itself, which is a no-op. It will not call the `drop` method (from
23 /// the `Drop` trait implementation) on the underlying referenced value, which
24 /// is likely what was intended.
26 /// **Known problems:** None.
30 /// let mut lock_guard = mutex.lock();
31 /// std::mem::drop(&lock_guard) // Should have been drop(lock_guard), mutex
33 /// operation_that_requires_mutex_to_be_unlocked();
35 declare_clippy_lint! {
38 "calls to `std::mem::drop` with a reference instead of an owned value"
41 /// **What it does:** Checks for calls to `std::mem::forget` with a reference
42 /// instead of an owned value.
44 /// **Why is this bad?** Calling `forget` on a reference will only forget the
45 /// reference itself, which is a no-op. It will not forget the underlying
47 /// value, which is likely what was intended.
49 /// **Known problems:** None.
53 /// let x = Box::new(1);
54 /// std::mem::forget(&x) // Should have been forget(x), x will still be dropped
56 declare_clippy_lint! {
59 "calls to `std::mem::forget` with a reference instead of an owned value"
62 /// **What it does:** Checks for calls to `std::mem::drop` with a value
63 /// that derives the Copy trait
65 /// **Why is this bad?** Calling `std::mem::drop` [does nothing for types that
66 /// implement Copy](https://doc.rust-lang.org/std/mem/fn.drop.html), since the
67 /// value will be copied and moved into the function on invocation.
69 /// **Known problems:** None.
73 /// let x:i32 = 42; // i32 implements Copy
74 /// std::mem::drop(x) // A copy of x is passed to the function, leaving the
75 /// // original unaffected
77 declare_clippy_lint! {
80 "calls to `std::mem::drop` with a value that implements Copy"
83 /// **What it does:** Checks for calls to `std::mem::forget` with a value that
84 /// derives the Copy trait
86 /// **Why is this bad?** Calling `std::mem::forget` [does nothing for types that
87 /// implement Copy](https://doc.rust-lang.org/std/mem/fn.drop.html) since the
88 /// value will be copied and moved into the function on invocation.
90 /// An alternative, but also valid, explanation is that Copy types do not
92 /// the Drop trait, which means they have no destructors. Without a destructor,
94 /// is nothing for `std::mem::forget` to ignore.
96 /// **Known problems:** None.
100 /// let x:i32 = 42; // i32 implements Copy
101 /// std::mem::forget(x) // A copy of x is passed to the function, leaving the
102 /// // original unaffected
104 declare_clippy_lint! {
107 "calls to `std::mem::forget` with a value that implements Copy"
110 const DROP_REF_SUMMARY: &str = "calls to `std::mem::drop` with a reference instead of an owned value. \
111 Dropping a reference does nothing.";
112 const FORGET_REF_SUMMARY: &str = "calls to `std::mem::forget` with a reference instead of an owned value. \
113 Forgetting a reference does nothing.";
114 const DROP_COPY_SUMMARY: &str = "calls to `std::mem::drop` with a value that implements Copy. \
115 Dropping a copy leaves the original intact.";
116 const FORGET_COPY_SUMMARY: &str = "calls to `std::mem::forget` with a value that implements Copy. \
117 Forgetting a copy leaves the original intact.";
119 #[allow(missing_copy_implementations)]
122 impl LintPass for Pass {
123 fn get_lints(&self) -> LintArray {
124 lint_array!(DROP_REF, FORGET_REF, DROP_COPY, FORGET_COPY)
128 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
129 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
131 if let ExprKind::Call(ref path, ref args) = expr.node;
132 if let ExprKind::Path(ref qpath) = path.node;
134 if let Some(def_id) = opt_def_id(cx.tables.qpath_def(qpath, path.hir_id));
139 let arg_ty = cx.tables.expr_ty(arg);
141 if let ty::Ref(..) = arg_ty.sty {
142 if match_def_path(cx.tcx, def_id, &paths::DROP) {
144 msg = DROP_REF_SUMMARY.to_string();
145 } else if match_def_path(cx.tcx, def_id, &paths::MEM_FORGET) {
147 msg = FORGET_REF_SUMMARY.to_string();
151 span_note_and_lint(cx,
156 &format!("argument has type {}", arg_ty));
157 } else if is_copy(cx, arg_ty) {
158 if match_def_path(cx.tcx, def_id, &paths::DROP) {
160 msg = DROP_COPY_SUMMARY.to_string();
161 } else if match_def_path(cx.tcx, def_id, &paths::MEM_FORGET) {
163 msg = FORGET_COPY_SUMMARY.to_string();
167 span_note_and_lint(cx,
172 &format!("argument has type {}", arg_ty));