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

Lint suggestion: assume_init_mut passed to drop_in_place #13860

Open
clarfonthey opened this issue Dec 20, 2024 · 0 comments
Open

Lint suggestion: assume_init_mut passed to drop_in_place #13860

clarfonthey opened this issue Dec 20, 2024 · 0 comments
Labels
A-lint Area: New lints

Comments

@clarfonthey
Copy link
Contributor

clarfonthey commented Dec 20, 2024

What it does

Since I found a few of these while working on rust-lang/rust#129259, this feels like a decent lint.

Assuming that this will be worked on after that PR is merged, it should detect both MaybeUninit::assume_init_mut and <[MaybeUninit<_>]>::assume_init_mut.

Advantage

  • Removes need to import drop_in_place in many cases
  • Fewer method calls = better codegen
  • Probably easier to read due to being shorter

Drawbacks

It could be difficult to detect all cases, e.g. binding the assume_init_mut value to a variable and then calling drop_in_place.

Example

Essentially, it would look for calls of the form:

ptr::drop_in_place(x.assume_init_mut())

and replace them with:

x.assume_init_drop()

We probably also want to cover the simple case of:

let y = x.assume_init_mut();
ptr::drop_in_place(y);

And… well, if y is used after that, this would be UB anyway, so, I'm going to assume that it's okay to replace this with:

x.assume_init_drop()
@clarfonthey clarfonthey added the A-lint Area: New lints label Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

1 participant