rustc_lint/drop_forget_useless.rs
1use rustc_hir::{Arm, Expr, ExprKind, Node, StmtKind};
2use rustc_middle::ty;
3use rustc_session::{declare_lint, declare_lint_pass};
4use rustc_span::sym;
5
6use crate::lints::{
7 DropCopyDiag, DropRefDiag, ForgetCopyDiag, ForgetRefDiag, UndroppedManuallyDropsDiag,
8 UndroppedManuallyDropsSuggestion, UseLetUnderscoreIgnoreSuggestion,
9};
10use crate::{LateContext, LateLintPass, LintContext};
11
12declare_lint! {
13 /// The `dropping_references` lint checks for calls to `std::mem::drop` with a reference
14 /// instead of an owned value.
15 ///
16 /// ### Example
17 ///
18 /// ```rust
19 /// # fn operation_that_requires_mutex_to_be_unlocked() {} // just to make it compile
20 /// # let mutex = std::sync::Mutex::new(1); // just to make it compile
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 ///
27 /// {{produces}}
28 ///
29 /// ### Explanation
30 ///
31 /// Calling `drop` on a reference will only drop the
32 /// reference itself, which is a no-op. It will not call the `drop` method (from
33 /// the `Drop` trait implementation) on the underlying referenced value, which
34 /// is likely what was intended.
35 pub DROPPING_REFERENCES,
36 Warn,
37 "calls to `std::mem::drop` with a reference instead of an owned value"
38}
39
40declare_lint! {
41 /// The `forgetting_references` lint checks for calls to `std::mem::forget` with a reference
42 /// instead of an owned value.
43 ///
44 /// ### Example
45 ///
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 ///
51 /// {{produces}}
52 ///
53 /// ### Explanation
54 ///
55 /// Calling `forget` on a reference will only forget the
56 /// reference itself, which is a no-op. It will not forget the underlying
57 /// referenced value, which is likely what was intended.
58 pub FORGETTING_REFERENCES,
59 Warn,
60 "calls to `std::mem::forget` with a reference instead of an owned value"
61}
62
63declare_lint! {
64 /// The `dropping_copy_types` lint checks for calls to `std::mem::drop` with a value
65 /// that derives the Copy trait.
66 ///
67 /// ### Example
68 ///
69 /// ```rust
70 /// let x: i32 = 42; // i32 implements Copy
71 /// std::mem::drop(x); // A copy of x is passed to the function, leaving the
72 /// // original unaffected
73 /// ```
74 ///
75 /// {{produces}}
76 ///
77 /// ### Explanation
78 ///
79 /// Calling `std::mem::drop` [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 pub DROPPING_COPY_TYPES,
83 Warn,
84 "calls to `std::mem::drop` with a value that implements Copy"
85}
86
87declare_lint! {
88 /// The `forgetting_copy_types` lint checks for calls to `std::mem::forget` with a value
89 /// that derives the Copy trait.
90 ///
91 /// ### Example
92 ///
93 /// ```rust
94 /// let x: i32 = 42; // i32 implements Copy
95 /// std::mem::forget(x); // A copy of x is passed to the function, leaving the
96 /// // original unaffected
97 /// ```
98 ///
99 /// {{produces}}
100 ///
101 /// ### Explanation
102 ///
103 /// Calling `std::mem::forget` [does nothing for types that
104 /// implement Copy](https://doc.rust-lang.org/std/mem/fn.drop.html) since the
105 /// value will be copied and moved into the function on invocation.
106 ///
107 /// An alternative, but also valid, explanation is that Copy types do not
108 /// implement the Drop trait, which means they have no destructors. Without a
109 /// destructor, there is nothing for `std::mem::forget` to ignore.
110 pub FORGETTING_COPY_TYPES,
111 Warn,
112 "calls to `std::mem::forget` with a value that implements Copy"
113}
114
115declare_lint! {
116 /// The `undropped_manually_drops` lint check for calls to `std::mem::drop` with
117 /// a value of `std::mem::ManuallyDrop` which doesn't drop.
118 ///
119 /// ### Example
120 ///
121 /// ```rust,compile_fail
122 /// struct S;
123 /// drop(std::mem::ManuallyDrop::new(S));
124 /// ```
125 ///
126 /// {{produces}}
127 ///
128 /// ### Explanation
129 ///
130 /// `ManuallyDrop` does not drop it's inner value so calling `std::mem::drop` will
131 /// not drop the inner value of the `ManuallyDrop` either.
132 pub UNDROPPED_MANUALLY_DROPS,
133 Deny,
134 "calls to `std::mem::drop` with `std::mem::ManuallyDrop` instead of it's inner value"
135}
136
137declare_lint_pass!(DropForgetUseless => [DROPPING_REFERENCES, FORGETTING_REFERENCES, DROPPING_COPY_TYPES, FORGETTING_COPY_TYPES, UNDROPPED_MANUALLY_DROPS]);
138
139impl<'tcx> LateLintPass<'tcx> for DropForgetUseless {
140 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
141 if let ExprKind::Call(path, [arg]) = expr.kind
142 && let ExprKind::Path(ref qpath) = path.kind
143 && let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
144 && let Some(fn_name) = cx.tcx.get_diagnostic_name(def_id)
145 {
146 let arg_ty = cx.typeck_results().expr_ty(arg);
147 let is_copy = cx.type_is_copy_modulo_regions(arg_ty);
148 let drop_is_single_call_in_arm = is_single_call_in_arm(cx, arg, expr);
149 let let_underscore_ignore_sugg = || {
150 if let Some((_, node)) = cx.tcx.hir_parent_iter(expr.hir_id).nth(0)
151 && let Node::Stmt(stmt) = node
152 && let StmtKind::Semi(e) = stmt.kind
153 && e.hir_id == expr.hir_id
154 && let Some(arg_span) = arg.span.find_ancestor_inside(expr.span)
155 {
156 UseLetUnderscoreIgnoreSuggestion::Suggestion {
157 start_span: expr.span.shrink_to_lo().until(arg_span),
158 end_span: arg_span.shrink_to_hi().until(expr.span.shrink_to_hi()),
159 }
160 } else {
161 UseLetUnderscoreIgnoreSuggestion::Note
162 }
163 };
164 match fn_name {
165 sym::mem_drop if arg_ty.is_ref() && !drop_is_single_call_in_arm => {
166 cx.emit_span_lint(
167 DROPPING_REFERENCES,
168 expr.span,
169 DropRefDiag { arg_ty, label: arg.span, sugg: let_underscore_ignore_sugg() },
170 );
171 }
172 sym::mem_forget if arg_ty.is_ref() => {
173 cx.emit_span_lint(
174 FORGETTING_REFERENCES,
175 expr.span,
176 ForgetRefDiag {
177 arg_ty,
178 label: arg.span,
179 sugg: let_underscore_ignore_sugg(),
180 },
181 );
182 }
183 sym::mem_drop if is_copy && !drop_is_single_call_in_arm => {
184 cx.emit_span_lint(
185 DROPPING_COPY_TYPES,
186 expr.span,
187 DropCopyDiag {
188 arg_ty,
189 label: arg.span,
190 sugg: let_underscore_ignore_sugg(),
191 },
192 );
193 }
194 sym::mem_forget if is_copy => {
195 cx.emit_span_lint(
196 FORGETTING_COPY_TYPES,
197 expr.span,
198 ForgetCopyDiag {
199 arg_ty,
200 label: arg.span,
201 sugg: let_underscore_ignore_sugg(),
202 },
203 );
204 }
205 sym::mem_drop
206 if let ty::Adt(adt, _) = arg_ty.kind()
207 && adt.is_manually_drop() =>
208 {
209 cx.emit_span_lint(
210 UNDROPPED_MANUALLY_DROPS,
211 expr.span,
212 UndroppedManuallyDropsDiag {
213 arg_ty,
214 label: arg.span,
215 suggestion: UndroppedManuallyDropsSuggestion {
216 start_span: arg.span.shrink_to_lo(),
217 end_span: arg.span.shrink_to_hi(),
218 },
219 },
220 );
221 }
222 _ => return,
223 };
224 }
225 }
226}
227
228// Dropping returned value of a function, as in the following snippet is considered idiomatic, see
229// rust-lang/rust-clippy#9482 for examples.
230//
231// ```
232// match <var> {
233// <pat> => drop(fn_with_side_effect_and_returning_some_value()),
234// ..
235// }
236// ```
237fn is_single_call_in_arm<'tcx>(
238 cx: &LateContext<'tcx>,
239 arg: &'tcx Expr<'_>,
240 drop_expr: &'tcx Expr<'_>,
241) -> bool {
242 if arg.can_have_side_effects() {
243 if let Node::Arm(Arm { body, .. }) = cx.tcx.parent_hir_node(drop_expr.hir_id) {
244 return body.hir_id == drop_expr.hir_id;
245 }
246 }
247 false
248}