Skip to content

Commit 78e36d9

Browse files
committed
Auto merge of #10996 - Centri3:mem_forget, r=xFrednet
Lint `mem_forget` if any fields are `Drop` Closes #9298 I think this way of doing it (`needs_drop`) should be fine. --- changelog: Enhancement: [`mem_forget`]: Now lints on types with fields that implement `Drop` [#10996](#10996)
2 parents a30ca62 + a5ae904 commit 78e36d9

File tree

6 files changed

+64
-58
lines changed

6 files changed

+64
-58
lines changed

clippy_lints/src/declared_lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
140140
crate::double_parens::DOUBLE_PARENS_INFO,
141141
crate::drop_forget_ref::DROP_NON_DROP_INFO,
142142
crate::drop_forget_ref::FORGET_NON_DROP_INFO,
143+
crate::drop_forget_ref::MEM_FORGET_INFO,
143144
crate::drop_forget_ref::UNDROPPED_MANUALLY_DROPS_INFO,
144145
crate::duplicate_mod::DUPLICATE_MOD_INFO,
145146
crate::else_if_without_else::ELSE_IF_WITHOUT_ELSE_INFO,
@@ -310,7 +311,6 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
310311
crate::matches::TRY_ERR_INFO,
311312
crate::matches::WILDCARD_ENUM_MATCH_ARM_INFO,
312313
crate::matches::WILDCARD_IN_OR_PATTERNS_INFO,
313-
crate::mem_forget::MEM_FORGET_INFO,
314314
crate::mem_replace::MEM_REPLACE_OPTION_WITH_NONE_INFO,
315315
crate::mem_replace::MEM_REPLACE_WITH_DEFAULT_INFO,
316316
crate::mem_replace::MEM_REPLACE_WITH_UNINIT_INFO,

clippy_lints/src/drop_forget_ref.rs

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_hir::{Arm, Expr, ExprKind, LangItem, Node};
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_session::{declare_lint_pass, declare_tool_lint};
88
use rustc_span::sym;
9+
use std::borrow::Cow;
910

1011
declare_clippy_lint! {
1112
/// ### What it does
@@ -76,6 +77,27 @@ declare_clippy_lint! {
7677
"use of safe `std::mem::drop` function to drop a std::mem::ManuallyDrop, which will not drop the inner value"
7778
}
7879

80+
declare_clippy_lint! {
81+
/// ### What it does
82+
/// Checks for usage of `std::mem::forget(t)` where `t` is
83+
/// `Drop` or has a field that implements `Drop`.
84+
///
85+
/// ### Why is this bad?
86+
/// `std::mem::forget(t)` prevents `t` from running its
87+
/// destructor, possibly causing leaks.
88+
///
89+
/// ### Example
90+
/// ```rust
91+
/// # use std::mem;
92+
/// # use std::rc::Rc;
93+
/// mem::forget(Rc::new(55))
94+
/// ```
95+
#[clippy::version = "pre 1.29.0"]
96+
pub MEM_FORGET,
97+
restriction,
98+
"`mem::forget` usage on `Drop` types, likely to cause memory leaks"
99+
}
100+
79101
const DROP_NON_DROP_SUMMARY: &str = "call to `std::mem::drop` with a value that does not implement `Drop`. \
80102
Dropping such a type only extends its contained lifetimes";
81103
const FORGET_NON_DROP_SUMMARY: &str = "call to `std::mem::forget` with a value that does not implement `Drop`. \
@@ -84,7 +106,8 @@ const FORGET_NON_DROP_SUMMARY: &str = "call to `std::mem::forget` with a value t
84106
declare_lint_pass!(DropForgetRef => [
85107
DROP_NON_DROP,
86108
FORGET_NON_DROP,
87-
UNDROPPED_MANUALLY_DROPS
109+
UNDROPPED_MANUALLY_DROPS,
110+
MEM_FORGET,
88111
]);
89112

90113
impl<'tcx> LateLintPass<'tcx> for DropForgetRef {
@@ -97,7 +120,7 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetRef {
97120
let arg_ty = cx.typeck_results().expr_ty(arg);
98121
let is_copy = is_copy(cx, arg_ty);
99122
let drop_is_single_call_in_arm = is_single_call_in_arm(cx, arg, expr);
100-
let (lint, msg) = match fn_name {
123+
let (lint, msg, note_span) = match fn_name {
101124
// early return for uplifted lints: dropping_references, dropping_copy_types, forgetting_references, forgetting_copy_types
102125
sym::mem_drop if arg_ty.is_ref() && !drop_is_single_call_in_arm => return,
103126
sym::mem_forget if arg_ty.is_ref() => return,
@@ -121,19 +144,34 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetRef {
121144
|| drop_is_single_call_in_arm
122145
) =>
123146
{
124-
(DROP_NON_DROP, DROP_NON_DROP_SUMMARY)
125-
},
126-
sym::mem_forget if !arg_ty.needs_drop(cx.tcx, cx.param_env) => {
127-
(FORGET_NON_DROP, FORGET_NON_DROP_SUMMARY)
147+
(DROP_NON_DROP, DROP_NON_DROP_SUMMARY.into(), Some(arg.span))
128148
},
149+
sym::mem_forget => {
150+
if arg_ty.needs_drop(cx.tcx, cx.param_env) {
151+
(
152+
MEM_FORGET,
153+
Cow::Owned(format!(
154+
"usage of `mem::forget` on {}",
155+
if arg_ty.ty_adt_def().map_or(false, |def| def.has_dtor(cx.tcx)) {
156+
"`Drop` type"
157+
} else {
158+
"type with `Drop` fields"
159+
}
160+
)),
161+
None,
162+
)
163+
} else {
164+
(FORGET_NON_DROP, FORGET_NON_DROP_SUMMARY.into(), Some(arg.span))
165+
}
166+
}
129167
_ => return,
130168
};
131169
span_lint_and_note(
132170
cx,
133171
lint,
134172
expr.span,
135-
msg,
136-
Some(arg.span),
173+
&msg,
174+
note_span,
137175
&format!("argument has type `{arg_ty}`"),
138176
);
139177
}

clippy_lints/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,6 @@ mod manual_strip;
196196
mod map_unit_fn;
197197
mod match_result_ok;
198198
mod matches;
199-
mod mem_forget;
200199
mod mem_replace;
201200
mod methods;
202201
mod min_ident_chars;
@@ -744,7 +743,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
744743
let missing_docs_in_crate_items = conf.missing_docs_in_crate_items;
745744
store.register_late_pass(move |_| Box::new(doc::DocMarkdown::new(doc_valid_idents.clone())));
746745
store.register_late_pass(|_| Box::new(neg_multiply::NegMultiply));
747-
store.register_late_pass(|_| Box::new(mem_forget::MemForget));
748746
store.register_late_pass(|_| Box::new(let_if_seq::LetIfSeq));
749747
store.register_late_pass(|_| Box::new(mixed_read_write_in_expression::EvalOrderDependence));
750748
store.register_late_pass(move |_| Box::new(missing_doc::MissingDoc::new(missing_docs_in_crate_items)));

clippy_lints/src/mem_forget.rs

Lines changed: 0 additions & 46 deletions
This file was deleted.

tests/ui/mem_forget.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,8 @@ fn main() {
1919
let eight: Vec<i32> = vec![8];
2020
forgetSomething(eight);
2121

22+
let string = String::new();
23+
std::mem::forget(string);
24+
2225
std::mem::forget(7);
2326
}

tests/ui/mem_forget.stderr

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,32 @@ error: usage of `mem::forget` on `Drop` type
44
LL | memstuff::forget(six);
55
| ^^^^^^^^^^^^^^^^^^^^^
66
|
7+
= note: argument has type `std::sync::Arc<i32>`
78
= note: `-D clippy::mem-forget` implied by `-D warnings`
89

910
error: usage of `mem::forget` on `Drop` type
1011
--> $DIR/mem_forget.rs:17:5
1112
|
1213
LL | std::mem::forget(seven);
1314
| ^^^^^^^^^^^^^^^^^^^^^^^
15+
|
16+
= note: argument has type `std::rc::Rc<i32>`
1417

1518
error: usage of `mem::forget` on `Drop` type
1619
--> $DIR/mem_forget.rs:20:5
1720
|
1821
LL | forgetSomething(eight);
1922
| ^^^^^^^^^^^^^^^^^^^^^^
23+
|
24+
= note: argument has type `std::vec::Vec<i32>`
25+
26+
error: usage of `mem::forget` on type with `Drop` fields
27+
--> $DIR/mem_forget.rs:23:5
28+
|
29+
LL | std::mem::forget(string);
30+
| ^^^^^^^^^^^^^^^^^^^^^^^^
31+
|
32+
= note: argument has type `std::string::String`
2033

21-
error: aborting due to 3 previous errors
34+
error: aborting due to 4 previous errors
2235

0 commit comments

Comments
 (0)