Rust-clippy: Suggestion for removing `to_string()` to `format!()`

Created on 29 Aug 2019  路  5Comments  路  Source: rust-lang/rust-clippy

Considering this code:

let s = format!("Hello, {}", "world").to_string();

This to_string() isn't needed because format! returns String type.
It'd be nice for Clippy to tell us it's needless.

E-medium L-enhancement L-lint good-first-issue hacktoberfest

Most helpful comment

redundant_clone contains general check for calling to_string() on String (just because it can be seen as cloning). It might better be extracted into another lint.

https://github.com/rust-lang/rust-clippy/blob/4c8a941dafff56a2485258b85b33ba33e66f6258/tests/ui/redundant_clone.stderr#L1-L12

All 5 comments

redundant_clone contains general check for calling to_string() on String (just because it can be seen as cloning). It might better be extracted into another lint.

https://github.com/rust-lang/rust-clippy/blob/4c8a941dafff56a2485258b85b33ba33e66f6258/tests/ui/redundant_clone.stderr#L1-L12

Yes, what @sinkuu said, this is already linted:

#[warn(clippy::redundant_clone)]
fn main() {
    let _s = format!("Hello, {}", "world").to_string();
}

Playground

But the redundant_clone lint is in the nursery group, because https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone

So yeah maybe this could be extracted in a more restricted lint, that can be warn-by-default.

E-medium if someone wants to attempt to fix this lint in itself.

Hey. Can I take this?

@Shubhamchinda go ahead!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

BenjaminGill-Metaswitch picture BenjaminGill-Metaswitch  路  22Comments

felix91gr picture felix91gr  路  41Comments

Manishearth picture Manishearth  路  20Comments

davemilter picture davemilter  路  18Comments

kevincox picture kevincox  路  17Comments