]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/drop_forget_ref.rs
Rollup merge of #73949 - wesleywiser:simplify_try_fixes, r=oli-obk
[rust.git] / src / tools / clippy / clippy_lints / src / drop_forget_ref.rs
1 use crate::utils::{is_copy, match_def_path, paths, qpath_res, span_lint_and_note};
2 use if_chain::if_chain;
3 use rustc_hir::{Expr, ExprKind};
4 use rustc_lint::{LateContext, LateLintPass};
5 use rustc_middle::ty;
6 use rustc_session::{declare_lint_pass, declare_tool_lint};
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 declare_lint_pass!(DropForgetRef => [DROP_REF, FORGET_REF, DROP_COPY, FORGET_COPY]);
110
111 impl<'tcx> LateLintPass<'tcx> for DropForgetRef {
112     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
113         if_chain! {
114             if let ExprKind::Call(ref path, ref args) = expr.kind;
115             if let ExprKind::Path(ref qpath) = path.kind;
116             if args.len() == 1;
117             if let Some(def_id) = qpath_res(cx, qpath, path.hir_id).opt_def_id();
118             then {
119                 let lint;
120                 let msg;
121                 let arg = &args[0];
122                 let arg_ty = cx.tables().expr_ty(arg);
123
124                 if let ty::Ref(..) = arg_ty.kind {
125                     if match_def_path(cx, def_id, &paths::DROP) {
126                         lint = DROP_REF;
127                         msg = DROP_REF_SUMMARY.to_string();
128                     } else if match_def_path(cx, def_id, &paths::MEM_FORGET) {
129                         lint = FORGET_REF;
130                         msg = FORGET_REF_SUMMARY.to_string();
131                     } else {
132                         return;
133                     }
134                     span_lint_and_note(cx,
135                                        lint,
136                                        expr.span,
137                                        &msg,
138                                        Some(arg.span),
139                                        &format!("argument has type `{}`", arg_ty));
140                 } else if is_copy(cx, arg_ty) {
141                     if match_def_path(cx, def_id, &paths::DROP) {
142                         lint = DROP_COPY;
143                         msg = DROP_COPY_SUMMARY.to_string();
144                     } else if match_def_path(cx, def_id, &paths::MEM_FORGET) {
145                         lint = FORGET_COPY;
146                         msg = FORGET_COPY_SUMMARY.to_string();
147                     } else {
148                         return;
149                     }
150                     span_lint_and_note(cx,
151                                        lint,
152                                        expr.span,
153                                        &msg,
154                                        Some(arg.span),
155                                        &format!("argument has type {}", arg_ty));
156                 }
157             }
158         }
159     }
160 }