Skip to content

Warn if user calls .to_string() multiple times on variable. #12152

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
Tanguille opened this issue Jan 15, 2024 · 1 comment
Open

Warn if user calls .to_string() multiple times on variable. #12152

Tanguille opened this issue Jan 15, 2024 · 1 comment
Labels
A-lint Area: New lints

Comments

@Tanguille
Copy link

What it does

Warn that .to_string() is only required once. Even with pedantic clippy this doesn't give any warning.

Advantage

It can happen to anyone if you process a lot of code that you accidently call .to_string() more then once on a variable. It takes up useless characters in your file and looks sloppy. This looks like something clippy could fix automatically with the --fix argument.

Drawbacks

I can't think of any since I don't know any situation where it would make sense to call .to_string() twice in a variable.

Example

 let uri = req.uri().path().to_string().to_string();

Could be written as:

 let uri = req.uri().path().to_string();
@Tanguille Tanguille added the A-lint Area: New lints label Jan 15, 2024
@y21
Copy link
Member

y21 commented Jan 15, 2024

This lint exists: https://rust-lang.github.io/rust-clippy/master/index.html#/redundant_clone
With #![warn(clippy::redundant_clone)]:

warning: redundant clone
 --> src/main.rs:6:9
  |
6 |         .to_string()
  |         ^^^^^^^^^^^^ help: remove this
  |
note: this value is dropped without further use
 --> src/main.rs:4:5
  |
4 | /     x
5 | |         .to_string()
6 | |         .to_string()
  | |________^

The lint is currently opt-in because it has way too many false positives, see: https://github.com/rust-lang/rust-clippy/issues?q=is%3Aissue+redundant_clone

The PR #11364 reworks it.

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

2 participants