Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mem_forget checks if a type implements Drop #9298

Closed
kadiwa4 opened this issue Aug 7, 2022 · 0 comments · Fixed by #10996
Closed

mem_forget checks if a type implements Drop #9298

kadiwa4 opened this issue Aug 7, 2022 · 0 comments · Fixed by #10996
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages

Comments

@kadiwa4
Copy link
Contributor

kadiwa4 commented Aug 7, 2022

Description

mem_forget currently only triggers if the type passed to mem::forget implements Drop.

However, if a type doesn't implement Drop, then that doesn't mean it lacks destructor code. For instance, String and HashMap currently don't implement it.

Instead, core::mem::needs_drop::<T>() is usually used to check if dropping a T invokes a destructor (the compiler-internal equivalent is rustc_middle::ty::Ty::needs_drop). It is already being used for (drop|forget)_non_drop and let_underscore_drop, but it is actually intended for optimization purposes only; it might spuriously return true. I don't know if it is the right thing to use here.

Version

No response

Additional Labels

@rustbot label: +C-enhancement

@rustbot rustbot added the C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages label Aug 7, 2022
bors added a commit that referenced this issue Jun 25, 2023
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)
@bors bors closed this as completed in 78e36d9 Jun 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants