Description
I wonder if it would make sense to suggest introducing a second binding in those simple
let
cases, aka.let str1 = String::with_capacity(MAX_PATH); let str1 = str1.as_mut_ptr();After looking at the "regressed" code from crater, I can say that a lot of the time this suggestion will not fix the problem, and will just result in code like
let vec = some_iter.collect(); let ptr = vec.as_ptr(); return ptr;or similar where the pointer still dangles eventually, even though we binded the temporary for now.
Sometimes it would make sense to suggest things like
.into_raw_parts()
instead of.as_ptr()
, but then the user has to remember to do::from_raw_parts()
if they want to avoid leaking...Most of the places where this lint fires are not trivial to fix and actually require the person writing the code to be more cautious & knowledgeable about what they are doing, which can only be achieved through more experience of writing
unsafe
, as well as some punches from the compiler, miri, or sanitizers.I think that not providing this suggestion in places where it would help is less bad than improperly providing it in places where it wouldn't help but would just further obscure and hide the issue.
Originally posted by @GrigorenkoPV in #128985 (comment)