]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/drop_forget_ref.rs
Auto merge of #3946 - rchaser53:issue-3920, r=flip1995
[rust.git] / clippy_lints / src / drop_forget_ref.rs
1 use crate::utils::{is_copy, match_def_path, paths, span_note_and_lint};
2 use if_chain::if_chain;
3 use rustc::hir::*;
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use rustc::ty;
6 use rustc::{declare_tool_lint, lint_array};
7
8 declare_clippy_lint! {
9     /// **What it does:** Checks for calls to `std::mem::drop` with a reference
10     /// instead of an owned value.
11     ///
12     /// **Why is this bad?** Calling `drop` on a reference will only drop the
13     /// reference itself, which is a no-op. It will not call the `drop` method (from
14     /// the `Drop` trait implementation) on the underlying referenced value, which
15     /// is likely what was intended.
16     ///
17     /// **Known problems:** None.
18     ///
19     /// **Example:**
20     /// ```ignore
21     /// let mut lock_guard = mutex.lock();
22     /// std::mem::drop(&lock_guard) // Should have been drop(lock_guard), mutex
23     /// // still locked
24     /// operation_that_requires_mutex_to_be_unlocked();
25     /// ```
26     pub DROP_REF,
27     correctness,
28     "calls to `std::mem::drop` with a reference instead of an owned value"
29 }
30
31 declare_clippy_lint! {
32     /// **What it does:** Checks for calls to `std::mem::forget` with a reference
33     /// instead of an owned value.
34     ///
35     /// **Why is this bad?** Calling `forget` on a reference will only forget the
36     /// reference itself, which is a no-op. It will not forget the underlying
37     /// referenced
38     /// value, which is likely what was intended.
39     ///
40     /// **Known problems:** None.
41     ///
42     /// **Example:**
43     /// ```rust
44     /// let x = Box::new(1);
45     /// std::mem::forget(&x) // Should have been forget(x), x will still be dropped
46     /// ```
47     pub FORGET_REF,
48     correctness,
49     "calls to `std::mem::forget` with a reference instead of an owned value"
50 }
51
52 declare_clippy_lint! {
53     /// **What it does:** Checks for calls to `std::mem::drop` with a value
54     /// that derives the Copy trait
55     ///
56     /// **Why is this bad?** Calling `std::mem::drop` [does nothing for types that
57     /// implement Copy](https://doc.rust-lang.org/std/mem/fn.drop.html), since the
58     /// value will be copied and moved into the function on invocation.
59     ///
60     /// **Known problems:** None.
61     ///
62     /// **Example:**
63     /// ```rust
64     /// let x: i32 = 42; // i32 implements Copy
65     /// std::mem::drop(x) // A copy of x is passed to the function, leaving the
66     ///                   // original unaffected
67     /// ```
68     pub DROP_COPY,
69     correctness,
70     "calls to `std::mem::drop` with a value that implements Copy"
71 }
72
73 declare_clippy_lint! {
74     /// **What it does:** Checks for calls to `std::mem::forget` with a value that
75     /// derives the Copy trait
76     ///
77     /// **Why is this bad?** Calling `std::mem::forget` [does nothing for types that
78     /// implement Copy](https://doc.rust-lang.org/std/mem/fn.drop.html) since the
79     /// value will be copied and moved into the function on invocation.
80     ///
81     /// An alternative, but also valid, explanation is that Copy types do not
82     /// implement
83     /// the Drop trait, which means they have no destructors. Without a destructor,
84     /// there
85     /// is nothing for `std::mem::forget` to ignore.
86     ///
87     /// **Known problems:** None.
88     ///
89     /// **Example:**
90     /// ```rust
91     /// let x: i32 = 42; // i32 implements Copy
92     /// std::mem::forget(x) // A copy of x is passed to the function, leaving the
93     ///                     // original unaffected
94     /// ```
95     pub FORGET_COPY,
96     correctness,
97     "calls to `std::mem::forget` with a value that implements Copy"
98 }
99
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.";
108
109 pub struct Pass;
110
111 impl LintPass for Pass {
112     fn get_lints(&self) -> LintArray {
113         lint_array!(DROP_REF, FORGET_REF, DROP_COPY, FORGET_COPY)
114     }
115
116     fn name(&self) -> &'static str {
117         "DropForgetRef"
118     }
119 }
120
121 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
122     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
123         if_chain! {
124             if let ExprKind::Call(ref path, ref args) = expr.node;
125             if let ExprKind::Path(ref qpath) = path.node;
126             if args.len() == 1;
127             if let Some(def_id) = cx.tables.qpath_def(qpath, path.hir_id).opt_def_id();
128             then {
129                 let lint;
130                 let msg;
131                 let arg = &args[0];
132                 let arg_ty = cx.tables.expr_ty(arg);
133
134                 if let ty::Ref(..) = arg_ty.sty {
135                     if match_def_path(cx.tcx, def_id, &paths::DROP) {
136                         lint = DROP_REF;
137                         msg = DROP_REF_SUMMARY.to_string();
138                     } else if match_def_path(cx.tcx, def_id, &paths::MEM_FORGET) {
139                         lint = FORGET_REF;
140                         msg = FORGET_REF_SUMMARY.to_string();
141                     } else {
142                         return;
143                     }
144                     span_note_and_lint(cx,
145                                        lint,
146                                        expr.span,
147                                        &msg,
148                                        arg.span,
149                                        &format!("argument has type {}", arg_ty));
150                 } else if is_copy(cx, arg_ty) {
151                     if match_def_path(cx.tcx, def_id, &paths::DROP) {
152                         lint = DROP_COPY;
153                         msg = DROP_COPY_SUMMARY.to_string();
154                     } else if match_def_path(cx.tcx, def_id, &paths::MEM_FORGET) {
155                         lint = FORGET_COPY;
156                         msg = FORGET_COPY_SUMMARY.to_string();
157                     } else {
158                         return;
159                     }
160                     span_note_and_lint(cx,
161                                        lint,
162                                        expr.span,
163                                        &msg,
164                                        arg.span,
165                                        &format!("argument has type {}", arg_ty));
166                 }
167             }
168         }
169     }
170 }