]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/drop_forget_ref.rs
Merge pull request #3265 from mikerite/fix-export
[rust.git] / clippy_lints / src / drop_forget_ref.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
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.
9
10
11 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
12 use crate::rustc::{declare_tool_lint, lint_array};
13 use if_chain::if_chain;
14 use crate::rustc::ty;
15 use crate::rustc::hir::*;
16 use crate::utils::{is_copy, match_def_path, opt_def_id, paths, span_note_and_lint};
17
18 /// **What it does:** Checks for calls to `std::mem::drop` with a reference
19 /// instead of an owned value.
20 ///
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.
25 ///
26 /// **Known problems:** None.
27 ///
28 /// **Example:**
29 /// ```rust
30 /// let mut lock_guard = mutex.lock();
31 /// std::mem::drop(&lock_guard) // Should have been drop(lock_guard), mutex
32 /// // still locked
33 /// operation_that_requires_mutex_to_be_unlocked();
34 /// ```
35 declare_clippy_lint! {
36     pub DROP_REF,
37     correctness,
38     "calls to `std::mem::drop` with a reference instead of an owned value"
39 }
40
41 /// **What it does:** Checks for calls to `std::mem::forget` with a reference
42 /// instead of an owned value.
43 ///
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
46 /// referenced
47 /// value, which is likely what was intended.
48 ///
49 /// **Known problems:** None.
50 ///
51 /// **Example:**
52 /// ```rust
53 /// let x = Box::new(1);
54 /// std::mem::forget(&x) // Should have been forget(x), x will still be dropped
55 /// ```
56 declare_clippy_lint! {
57     pub FORGET_REF,
58     correctness,
59     "calls to `std::mem::forget` with a reference instead of an owned value"
60 }
61
62 /// **What it does:** Checks for calls to `std::mem::drop` with a value
63 /// that derives the Copy trait
64 ///
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.
68 ///
69 /// **Known problems:** None.
70 ///
71 /// **Example:**
72 /// ```rust
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
76 /// ```
77 declare_clippy_lint! {
78     pub DROP_COPY,
79     correctness,
80     "calls to `std::mem::drop` with a value that implements Copy"
81 }
82
83 /// **What it does:** Checks for calls to `std::mem::forget` with a value that
84 /// derives the Copy trait
85 ///
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.
89 ///
90 /// An alternative, but also valid, explanation is that Copy types do not
91 /// implement
92 /// the Drop trait, which means they have no destructors. Without a destructor,
93 /// there
94 /// is nothing for `std::mem::forget` to ignore.
95 ///
96 /// **Known problems:** None.
97 ///
98 /// **Example:**
99 /// ```rust
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
103 /// ```
104 declare_clippy_lint! {
105     pub FORGET_COPY,
106     correctness,
107     "calls to `std::mem::forget` with a value that implements Copy"
108 }
109
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.";
118
119 #[allow(missing_copy_implementations)]
120 pub struct Pass;
121
122 impl LintPass for Pass {
123     fn get_lints(&self) -> LintArray {
124         lint_array!(DROP_REF, FORGET_REF, DROP_COPY, FORGET_COPY)
125     }
126 }
127
128 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
129     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
130         if_chain! {
131             if let ExprKind::Call(ref path, ref args) = expr.node;
132             if let ExprKind::Path(ref qpath) = path.node;
133             if args.len() == 1;
134             if let Some(def_id) = opt_def_id(cx.tables.qpath_def(qpath, path.hir_id));
135             then {
136                 let lint;
137                 let msg;
138                 let arg = &args[0];
139                 let arg_ty = cx.tables.expr_ty(arg);
140
141                 if let ty::Ref(..) = arg_ty.sty {
142                     if match_def_path(cx.tcx, def_id, &paths::DROP) {
143                         lint = DROP_REF;
144                         msg = DROP_REF_SUMMARY.to_string();
145                     } else if match_def_path(cx.tcx, def_id, &paths::MEM_FORGET) {
146                         lint = FORGET_REF;
147                         msg = FORGET_REF_SUMMARY.to_string();
148                     } else {
149                         return;
150                     }
151                     span_note_and_lint(cx,
152                                        lint,
153                                        expr.span,
154                                        &msg,
155                                        arg.span,
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) {
159                         lint = DROP_COPY;
160                         msg = DROP_COPY_SUMMARY.to_string();
161                     } else if match_def_path(cx.tcx, def_id, &paths::MEM_FORGET) {
162                         lint = FORGET_COPY;
163                         msg = FORGET_COPY_SUMMARY.to_string();
164                     } else {
165                         return;
166                     }
167                     span_note_and_lint(cx,
168                                        lint,
169                                        expr.span,
170                                        &msg,
171                                        arg.span,
172                                        &format!("argument has type {}", arg_ty));
173                 }
174             }
175         }
176     }
177 }