Skip to content

New lint: Prefer mem::take over Vec::split_off(0) #4953

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

Open
tesuji opened this issue Dec 25, 2019 · 4 comments · May be fixed by #14814
Open

New lint: Prefer mem::take over Vec::split_off(0) #4953

tesuji opened this issue Dec 25, 2019 · 4 comments · May be fixed by #14814
Assignees
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy S-needs-discussion Status: Needs further discussion before merging or work can be started

Comments

@tesuji
Copy link
Contributor

tesuji commented Dec 25, 2019

let mut vec = vec![1,2,3];
let vec2 = vec.split_off(0);

Should be written as

let vec2  = std::mem::take(&mut vec);
@tesuji
Copy link
Contributor Author

tesuji commented Dec 25, 2019

Oh, wait. The semantics between two functions are different.
Vec::split_off leave the caller with same capacity while mem::take leave call with default value
(in this case an empty Vec).

@basil-cow
Copy link
Contributor

I think vec.drain(..).collect() better represents the intention behind vec.split_off(0) (assuming it doesn't change capacity, which I don't think it is but seems like it's not specified either).

@camsteffen camsteffen added good-first-issue These issues are a good way to get started with Clippy A-lint Area: New lints S-needs-discussion Status: Needs further discussion before merging or work can be started labels Jan 26, 2021
@Zalathar

This comment was marked as resolved.

@andre-digiexam
Copy link

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints good-first-issue These issues are a good way to get started with Clippy S-needs-discussion Status: Needs further discussion before merging or work can be started
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants