-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Explore possible suggestions for dangling_pointers_from_temporaries
lint
#132283
Comments
Maybe the error could be extended, to also say something along the lines of |
That would already be a huge improvement over the current diagnostic. Regarding the diagnostic, I think something like this should do it. error: a dangling pointer will be produced because the temporary `CString` will be dropped
--> $DIR/calls.rs:32:29
|
LL | let ptr = cstring().as_ptr();
| --------- ^^^^^^ this pointer will immediately be invalid
| |
| this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
|
= note: pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
+ = help: you must make sure that the variable you bind the `CString` to lives at least as long as the pointer returned by the call to `as_ptr`
+ = help: in particular, if this pointer is returned from the current function, binding the `CString` inside the function will not suffice
= help: for more information, see <https://doc.rust-lang.org/reference/destructors.html> Note: I've slightly adjusted the wording to refer to the thing directly, instead of "it" or "the pointer" |
I like it! The part I am most confused about is the (existing?) "pointers do not have a lifetime" -- it seems not directly related to the immediately next sentence? And it's not like the lifetime of a reference would magically make the referent live longer or so. I onder if it would be better to just remove that part entirely? And the part that indicates that "something referencing something" would influence the lifetime of anything. In fact I'd reword the entire first line:
|
Sure, looks got to me. Let's see if someone wants to do it. Steps:
Starting points can be found on the rustc-dev-guide. Feel free to reach out (to me or others) here or in our Zulip. @rustbot labels +E-easy +E-mentor |
Could I please be assigned this issue? I think this would be a great learning opportunity for me! |
Sure. @rustbot assign @Anthony-Eid |
@Anthony-Eid you can even do that yourself, by writing @ |
Originally posted by @GrigorenkoPV in #128985 (comment)
The text was updated successfully, but these errors were encountered: