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