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

[unnecessary_to_owned]: catch to_owned on byte slice to create temporary &str #11656

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

y21
Copy link
Member

@y21 y21 commented Oct 10, 2023

Closes #11648

Detects the pattern &String::from_utf8(bytes.to_vec()).unwrap() and suggests core::str::from_utf8(bytes).unwrap(), which avoids the unnecessary intermediate allocation.

I decided to put this in the existing unnecessary_to_owned lint (rather than creating a new lint) for a few reasons:

  • we get to use some of its logic (for example, recognizing any of the functions in the to_owned family, e.g. to_vec)
  • the actual inefficient operation that can be avoided here is the call to .to_vec(), so this is in a way similar to the other cases caught by unnecessary_to_owned, just through a bunch of type conversions
  • we can make this more "generic" later and catch other cases, so imo it's best not to tie this lint specifically to the String type

changelog: [unnecessary_to_owned]: catch &String::from_utf8(bytes.to_vec()).unwrap() and suggest core::str::from_utf8(bytes).unwrap()

@rustbot
Copy link
Collaborator

rustbot commented Oct 10, 2023

r? @giraffate

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Oct 10, 2023
@bors
Copy link
Collaborator

bors commented Oct 21, 2023

☔ The latest upstream changes (presumably #11694) made this pull request unmergeable. Please resolve the merge conflicts.

@xFrednet
Copy link
Member

(I claimed a few PRs, let's give this one to the lottery)

r? clippy

@rustbot rustbot assigned Jarcho and unassigned giraffate Mar 31, 2024
@y21 y21 force-pushed the unnecessary_string_from_utf8 branch from bd46706 to 1412560 Compare March 31, 2024 20:47
@y21 y21 force-pushed the unnecessary_string_from_utf8 branch from 1412560 to 0449a36 Compare March 31, 2024 20:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
6 participants