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