Skip to content

Suggest String::clone and forbid String::to_string #14173

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

Closed
MarkusTieger opened this issue Feb 7, 2025 · 4 comments
Closed

Suggest String::clone and forbid String::to_string #14173

MarkusTieger opened this issue Feb 7, 2025 · 4 comments
Labels
A-lint Area: New lints

Comments

@MarkusTieger
Copy link

MarkusTieger commented Feb 7, 2025

What it does

We should add a lint that causes a warning if the to_string method is used on a String / &String, (not on a &str ofc).

Advantage

The Clone trait is the more commonly used method. Afaik there is no disadvantage of using it instead of ToString.

Drawbacks

None afaik

Example

// 1)
let variable1 = String::new();
let variable2 = variable1.to_string();

// 2)
let variable1 = Some(&variable1);
let variable2 = variable1.map(String::to_string);

Could be written as:

// 1)
let variable1 = String::new();
let variable2 = variable1.clone();

// 2)
let variable1 = Some(String::new());
let variable2 = variable1.cloned();
@MarkusTieger MarkusTieger added the A-lint Area: New lints label Feb 7, 2025
@Centri3
Copy link
Member

Centri3 commented Feb 8, 2025

This already exists in the form of string_to_string.

@MarkusTieger
Copy link
Author

MarkusTieger commented Feb 8, 2025

This already exists in the form of string_to_string.

Hmm, I see. I would be interested in knowing why this lint isn't enabled by default.
(btw "2)", doesn't get picked up by string_to_string in my example)

@smoelius
Copy link
Contributor

smoelius commented Feb 8, 2025

Maybe string_to_string should be merged into implicit_clone?

More people would benefit from it if it were pedantic.

@MarkusTieger
Copy link
Author

Maybe string_to_string should be merged into implicit_clone?

More people would benefit from it if it were pedantic.

Makes sense

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

3 participants