Skip to content
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

Add #[must_use] to from_value conversions #89753

Merged
merged 1 commit into from
Oct 11, 2021

Conversation

jkugelman
Copy link
Contributor

I added two methods to the list myself. Clippy did not flag them because they take mut args, but neither modifies their argument.

core::str           const unsafe fn from_utf8_unchecked_mut(v: &mut [u8]) -> &mut str;
std::ffi::CString   unsafe fn from_raw(ptr: *mut c_char) -> CString;

I put a custom note on from_raw:

#[must_use = "call `drop(from_raw(ptr))` if you intend to drop the `CString`"]
pub unsafe fn from_raw(ptr: *mut c_char) -> CString {

Parent issue: #89692

r? @joshtriplett

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 10, 2021
@jkugelman jkugelman force-pushed the must-use-from_value-conversions branch from 3fe1c3b to 4967c6a Compare October 10, 2021 22:35
@rust-log-analyzer

This comment has been minimized.

@jkugelman jkugelman force-pushed the must-use-from_value-conversions branch from 4967c6a to cf2bcd1 Compare October 10, 2021 23:00
@joshtriplett joshtriplett added the relnotes Marks issues that should be documented in the release notes of the next release. label Oct 11, 2021
@joshtriplett
Copy link
Member

If we find any significant number of crates that are using one of these methods to convert to a type with Drop as a means of freeing it, we should consider reverting the addition of must_use to that method to avoid excessive disruption. Such a code pattern would be unusual but legitimate. We're allowed to add warnings to existing code, but we typically take into account how many warnings we're adding to the ecosystem.

@joshtriplett
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Oct 11, 2021

📌 Commit cf2bcd1 has been approved by joshtriplett

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 11, 2021
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Oct 11, 2021
…rsions, r=joshtriplett

Add #[must_use] to from_value conversions

I added two methods to the list myself. Clippy did not flag them because they take `mut` args, but neither modifies their argument.

```rust
core::str           const unsafe fn from_utf8_unchecked_mut(v: &mut [u8]) -> &mut str;
std::ffi::CString   unsafe fn from_raw(ptr: *mut c_char) -> CString;
```

I put a custom note on `from_raw`:

```rust
#[must_use = "call `drop(from_raw(ptr))` if you intend to drop the `CString`"]
pub unsafe fn from_raw(ptr: *mut c_char) -> CString {
```

Parent issue: rust-lang#89692

r? `@joshtriplett`
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 11, 2021
…laumeGomez

Rollup of 7 pull requests

Successful merges:

 - rust-lang#89655 (bootstrap: don't use `--merges` to look for commit hashes for downloading artifacts)
 - rust-lang#89726 (Add #[must_use] to alloc constructors)
 - rust-lang#89729 (Add #[must_use] to core and std constructors)
 - rust-lang#89743 (Fix RUSTC_LOG handling)
 - rust-lang#89753 (Add #[must_use] to from_value conversions)
 - rust-lang#89754 (Cleanup .item-table CSS)
 - rust-lang#89761 (:arrow_up: rust-analyzer)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 96ffc74 into rust-lang:master Oct 11, 2021
@rustbot rustbot added this to the 1.57.0 milestone Oct 11, 2021
@jkugelman jkugelman deleted the must-use-from_value-conversions branch October 11, 2021 17:55
@BurntSushi
Copy link
Member

Is there any particular reason why this was added to CString::from_raw but not, say, Box::from_raw?

@jkugelman
Copy link
Contributor Author

@BurntSushi No, I don't believe so. I don't remember excluding it. I'm guessing I simply didn't catch Box::from_raw.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
relnotes Marks issues that should be documented in the release notes of the next release. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants