Skip to content

Commit b6f194b

Browse files
committed
move to drop_forget_ref
1 parent d8d5996 commit b6f194b

File tree

5 files changed

+64
-63
lines changed

5 files changed

+64
-63
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: 40 additions & 6 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 {
@@ -121,18 +144,29 @@ 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())
128148
},
149+
sym::mem_forget => {
150+
if arg_ty.needs_drop(cx.tcx, cx.param_env) {
151+
(MEM_FORGET, Cow::Owned(format!(
152+
"usage of `mem::forget` on {}",
153+
if arg_ty.ty_adt_def().map_or(false, |def| def.has_dtor(cx.tcx)) {
154+
"`Drop` type"
155+
} else {
156+
"type with `Drop` fields"
157+
}
158+
)))
159+
} else {
160+
(FORGET_NON_DROP, FORGET_NON_DROP_SUMMARY.into())
161+
}
162+
}
129163
_ => return,
130164
};
131165
span_lint_and_note(
132166
cx,
133167
lint,
134168
expr.span,
135-
msg,
169+
&msg,
136170
Some(arg.span),
137171
&format!("argument has type `{arg_ty}`"),
138172
);

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 & 54 deletions
This file was deleted.

tests/ui/mem_forget.stderr

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,48 @@ error: usage of `mem::forget` on `Drop` type
44
LL | memstuff::forget(six);
55
| ^^^^^^^^^^^^^^^^^^^^^
66
|
7+
note: argument has type `std::sync::Arc<i32>`
8+
--> $DIR/mem_forget.rs:14:22
9+
|
10+
LL | memstuff::forget(six);
11+
| ^^^
712
= note: `-D clippy::mem-forget` implied by `-D warnings`
813

914
error: usage of `mem::forget` on `Drop` type
1015
--> $DIR/mem_forget.rs:17:5
1116
|
1217
LL | std::mem::forget(seven);
1318
| ^^^^^^^^^^^^^^^^^^^^^^^
19+
|
20+
note: argument has type `std::rc::Rc<i32>`
21+
--> $DIR/mem_forget.rs:17:22
22+
|
23+
LL | std::mem::forget(seven);
24+
| ^^^^^
1425

1526
error: usage of `mem::forget` on `Drop` type
1627
--> $DIR/mem_forget.rs:20:5
1728
|
1829
LL | forgetSomething(eight);
1930
| ^^^^^^^^^^^^^^^^^^^^^^
31+
|
32+
note: argument has type `std::vec::Vec<i32>`
33+
--> $DIR/mem_forget.rs:20:21
34+
|
35+
LL | forgetSomething(eight);
36+
| ^^^^^
2037

2138
error: usage of `mem::forget` on type with `Drop` fields
2239
--> $DIR/mem_forget.rs:23:5
2340
|
2441
LL | std::mem::forget(string);
2542
| ^^^^^^^^^^^^^^^^^^^^^^^^
43+
|
44+
note: argument has type `std::string::String`
45+
--> $DIR/mem_forget.rs:23:22
46+
|
47+
LL | std::mem::forget(string);
48+
| ^^^^^^
2649

2750
error: aborting due to 4 previous errors
2851

0 commit comments

Comments
 (0)