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