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