Rust-clippy: Suggest into_owned instead of to_string for Cow<str>

Created on 21 Jan 2018  路  6Comments  路  Source: rust-lang/rust-clippy

Note serde-rs/json#405.

We could also lint to_vec on Cow<[T]> and probably others.

E-medium L-lint T-middle

Most helpful comment

Basically, this is a specific case of the more general "did you mean to move instead of clone this" set of errors.

All 6 comments

Note that this only applies when the Cow being inspected is owned by the caller. E.g. this lint would suggest code that doesn't compile when the caller has a &Cow. This shouldn't happen too often anyway, because the whole point of a Cow is that the user doesn't need to keep in his/her head whether it's a reference or an owned value. Actually, while we're here, may I also suggest a lint against passing Cow by reference?

See additional discussions in the duplicate issue https://github.com/rust-lang/rust-clippy/issues/5987

Note that the claim that String: From<Cow<'_, str>> allocates is no longer true

@Kixunil it is not String: From<Cow<str>> that is of concern here; instead, it is only Cow: ToString which has no choice but to allocate.

Ah, I misread that code in the serde issue.

Basically, this is a specific case of the more general "did you mean to move instead of clone this" set of errors.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Manishearth picture Manishearth  路  26Comments

Manishearth picture Manishearth  路  18Comments

Manishearth picture Manishearth  路  20Comments

Shnatsel picture Shnatsel  路  23Comments

mikerite picture mikerite  路  17Comments