-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 against MaybeUninit::uninit().assume_init() #4272
Comments
Sigh. Just noticed this isn't always UB. The docs even use this pattern to construct a I suppose more generally one can say that if all fields of a type are themselves |
ZST fields may be absolutely non-OK – for example if they are uninhabited (the poster child here is the |
Even though (I reckon the line between valid and invalid types of zero-size is pretty clear, with no gray area in-between) |
For now I think it'd be enough to lint if the target type isn't uninit-compatible, which I define as |
Having a MaybeUninit of an uninhabitable type should be a forbidden lint, btw. |
I'm taking this. It's been far too long since I wrote my last lint. |
There is an open pull request to warn against those in |
I spotted this in PyO3: (PyO3/pyo3#536)
GitHub search brings up a total of 62 matches even for a fairly restrictive search query: https://github.com/search?q=%22uninit%28%29%3A%3Aassume_init%28%29%22&type=Code
Problem: It seems people are using this as an easy way out of the deprecation warnings for
std::mem::uninitialized
, failing to understand the reason why it was deprecated (it is, to my current understanding, universally UB, always!). The correct solution is to useMaybeUninit<T>
where you were formerly using T until the value is known to be initialized. TheMaybeUninit<T>
docs even give examples for what are by far the two most common use cases (out pointers and partially-initialized arrays).Proposal: There should either be a warn-by-default or an error-by-default lint against immediate calls to
assume_init()
onMaybeUninit::uninit()
with no other intervening operations.Possible extension: We can try going a step further and adjust our suggestions based on the type:
T
is uninhabited, we can instead recommend the use ofstd::hint::unreachable_unchecked
.T = [A; N]
, we can specifically recommend that the user constructs a[MaybeUninit<A>; N]
. (as this is often a key piece of the correct solution that the user might not think of on their own, and without it they may simply invent some other way to silence the warning improperly)The text was updated successfully, but these errors were encountered: