fix UB on TypedArray::as_typed_aray #244
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
tldr: the code inside
bool::then_some
is always executed, but the code in the closure frombool::then
is only executed if is true.This is UB, because the
bool::then_some
is a function that takes a bool and a generic value, because of that, the value inside of it is always evaluated, independent if the bool is true/false.it's clear if the desugarize the syntax, the code bellow is equivalent to the function
as_typed_array
:The solution is simply change it to
bool::then
, because the parameter it's a closure, it's lazy evaluated, and only executed if the condition is true.As mentioned at https://github.com/rust-lang/rust/blob/5cb2e7dfc362662b0036faad3bab88d73027fd05/src/tools/clippy/clippy_lints/src/transmute/mod.rs#L468-L520
And presented at https://youtu.be/hBjQ3HqCfxs?si=nrIz6ZHnxEHO6Pik&t=53