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.
10 use crate::rustc::hir::*;
11 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
13 use crate::rustc::{declare_tool_lint, lint_array};
14 use crate::utils::{is_copy, match_def_path, opt_def_id, paths, span_note_and_lint};
15 use if_chain::if_chain;
17 /// **What it does:** Checks for calls to `std::mem::drop` with a reference
18 /// instead of an owned value.
20 /// **Why is this bad?** Calling `drop` on a reference will only drop the
21 /// reference itself, which is a no-op. It will not call the `drop` method (from
22 /// the `Drop` trait implementation) on the underlying referenced value, which
23 /// is likely what was intended.
25 /// **Known problems:** None.
29 /// let mut lock_guard = mutex.lock();
30 /// std::mem::drop(&lock_guard) // Should have been drop(lock_guard), mutex
32 /// operation_that_requires_mutex_to_be_unlocked();
34 declare_clippy_lint! {
37 "calls to `std::mem::drop` with a reference instead of an owned value"
40 /// **What it does:** Checks for calls to `std::mem::forget` with a reference
41 /// instead of an owned value.
43 /// **Why is this bad?** Calling `forget` on a reference will only forget the
44 /// reference itself, which is a no-op. It will not forget the underlying
46 /// value, which is likely what was intended.
48 /// **Known problems:** None.
52 /// let x = Box::new(1);
53 /// std::mem::forget(&x) // Should have been forget(x), x will still be dropped
55 declare_clippy_lint! {
58 "calls to `std::mem::forget` with a reference instead of an owned value"
61 /// **What it does:** Checks for calls to `std::mem::drop` with a value
62 /// that derives the Copy trait
64 /// **Why is this bad?** Calling `std::mem::drop` [does nothing for types that
65 /// implement Copy](https://doc.rust-lang.org/std/mem/fn.drop.html), since the
66 /// value will be copied and moved into the function on invocation.
68 /// **Known problems:** None.
72 /// let x: i32 = 42; // i32 implements Copy
73 /// std::mem::drop(x) // A copy of x is passed to the function, leaving the
74 /// // original unaffected
76 declare_clippy_lint! {
79 "calls to `std::mem::drop` with a value that implements Copy"
82 /// **What it does:** Checks for calls to `std::mem::forget` with a value that
83 /// derives the Copy trait
85 /// **Why is this bad?** Calling `std::mem::forget` [does nothing for types that
86 /// implement Copy](https://doc.rust-lang.org/std/mem/fn.drop.html) since the
87 /// value will be copied and moved into the function on invocation.
89 /// An alternative, but also valid, explanation is that Copy types do not
91 /// the Drop trait, which means they have no destructors. Without a destructor,
93 /// is nothing for `std::mem::forget` to ignore.
95 /// **Known problems:** None.
99 /// let x: i32 = 42; // i32 implements Copy
100 /// std::mem::forget(x) // A copy of x is passed to the function, leaving the
101 /// // original unaffected
103 declare_clippy_lint! {
106 "calls to `std::mem::forget` with a value that implements Copy"
109 const DROP_REF_SUMMARY: &str = "calls to `std::mem::drop` with a reference instead of an owned value. \
110 Dropping a reference does nothing.";
111 const FORGET_REF_SUMMARY: &str = "calls to `std::mem::forget` with a reference instead of an owned value. \
112 Forgetting a reference does nothing.";
113 const DROP_COPY_SUMMARY: &str = "calls to `std::mem::drop` with a value that implements Copy. \
114 Dropping a copy leaves the original intact.";
115 const FORGET_COPY_SUMMARY: &str = "calls to `std::mem::forget` with a value that implements Copy. \
116 Forgetting a copy leaves the original intact.";
120 impl LintPass for Pass {
121 fn get_lints(&self) -> LintArray {
122 lint_array!(DROP_REF, FORGET_REF, DROP_COPY, FORGET_COPY)
126 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
127 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
129 if let ExprKind::Call(ref path, ref args) = expr.node;
130 if let ExprKind::Path(ref qpath) = path.node;
132 if let Some(def_id) = opt_def_id(cx.tables.qpath_def(qpath, path.hir_id));
137 let arg_ty = cx.tables.expr_ty(arg);
139 if let ty::Ref(..) = arg_ty.sty {
140 if match_def_path(cx.tcx, def_id, &paths::DROP) {
142 msg = DROP_REF_SUMMARY.to_string();
143 } else if match_def_path(cx.tcx, def_id, &paths::MEM_FORGET) {
145 msg = FORGET_REF_SUMMARY.to_string();
149 span_note_and_lint(cx,
154 &format!("argument has type {}", arg_ty));
155 } else if is_copy(cx, arg_ty) {
156 if match_def_path(cx.tcx, def_id, &paths::DROP) {
158 msg = DROP_COPY_SUMMARY.to_string();
159 } else if match_def_path(cx.tcx, def_id, &paths::MEM_FORGET) {
161 msg = FORGET_COPY_SUMMARY.to_string();
165 span_note_and_lint(cx,
170 &format!("argument has type {}", arg_ty));