-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
CString lifetime warning incorrectly fires #78691
Comments
This is the edge case I mentioned in #75671 (comment). @matklad and few others said
|
I would like if this were fixed. I find the comment about language lawyers uncharitable. Emitting a scary warning like this in correct code just puts even more of a needless barrier toward someone becoming a language lawyer. The relevant piece of the language (temporaries drop at the end of the statement) is quite simple — understanding why the linter is actively misleading you about the correctness of your code is a way bigger source of confusion than the language itself. If someone wants to keep a lint for that case because they find the code confusing, that lint belongs in Clippy. I believe the uplift to rustc was only meant for known bad code, such as |
We discussed this in the @rust-lang/lang meeting today. We agreed that we shouldn't lint on this case; the goal of the lint is to catch known-wrong cases, particularly cases that we've seen commonly used by mistake. We shouldn't have false positives, and it's fine to rely on a temporary lasting until the end of the statement. We do want to make sure that, in improving this lint to avoid the false positves, we don't stop warning on some true positives. Here are the cases we want to cover, which we'd like to see in a test case:
|
Looks like when using the question mark(aka try operator) it correctly doesn't fire: some_function( CString::new("this correctly doesn't warn")?.as_ptr() );
some_function( CString::new("this warns but it shouldn't").unwrap().as_ptr() ); |
I tried this code:
I get:
However since the CString is not deallocated until after the function call the code is fine and the warning should not trigger.
See https://rust.godbolt.org/z/vjrvGn for the assembly.
The text was updated successfully, but these errors were encountered: