Skip to content

Redundant to_string? #7729

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
smoelius opened this issue Sep 27, 2021 · 3 comments
Closed

Redundant to_string? #7729

smoelius opened this issue Sep 27, 2021 · 3 comments

Comments

@smoelius
Copy link
Contributor

I was surprised that there does not seem to be a lint to catch the following. Is it possible that it exists and I am somehow not triggering it?

use camino::Utf8PathBuf;

fn main() {
    let path = Utf8PathBuf::from("src/main.rs");
    println!("{}", path.to_string()); // to_string is unnecessary since Utf8PathBuf implements Display
    println!("{}", path);
}
@camsteffen
Copy link
Contributor

camsteffen commented Sep 27, 2021

Related to #7667, and probably should be implemented together. I'd call this to_string_in_format_args.

smoelius added a commit to smoelius/rust-clippy that referenced this issue Sep 30, 2021
smoelius added a commit to smoelius/rust-clippy that referenced this issue Sep 30, 2021
smoelius added a commit to smoelius/rust-clippy that referenced this issue Sep 30, 2021
smoelius added a commit to smoelius/rust-clippy that referenced this issue Sep 30, 2021
smoelius added a commit to smoelius/rust-clippy that referenced this issue Sep 30, 2021
smoelius added a commit to smoelius/rust-clippy that referenced this issue Sep 30, 2021
smoelius added a commit to smoelius/rust-clippy that referenced this issue Sep 30, 2021
smoelius added a commit to smoelius/rust-clippy that referenced this issue Oct 2, 2021
smoelius added a commit to smoelius/rust-clippy that referenced this issue Oct 3, 2021
smoelius added a commit to smoelius/rust-clippy that referenced this issue Oct 7, 2021
smoelius added a commit to smoelius/rust-clippy that referenced this issue Oct 7, 2021
smoelius added a commit to smoelius/rust-clippy that referenced this issue Oct 7, 2021
smoelius added a commit to smoelius/rust-clippy that referenced this issue Oct 8, 2021
smoelius added a commit to smoelius/rust-clippy that referenced this issue Oct 14, 2021
smoelius added a commit to smoelius/rust-clippy that referenced this issue Oct 14, 2021
smoelius added a commit to smoelius/rust-clippy that referenced this issue Oct 14, 2021
smoelius added a commit to smoelius/rust-clippy that referenced this issue Oct 15, 2021
bors added a commit that referenced this issue Oct 15, 2021
Add `format_in_format_args` and `to_string_in_format_args` lints

Fixes #7667 and #7729

I put these in `perf` since that was one of `@jplatte's` suggestions, and `redundant_clone` (which I consider to be similar) lives there as well.

However, I am open to changing the category or anything else.

r? `@camsteffen`

changelog: Add `format_in_format_args` and `to_string_in_format_args` lints
@akanalytics
Copy link

akanalytics commented May 18, 2022

Is it true that
a) println!("{:<20}", struct.to_string());
gives the same result as
b) println!("{:<20}", struct);

?

Doesnt it depend on how the formatter is written.
Line a) uses the default "{}" display to render a string then renders the string using the formatting rules :<20
Whereas b) uses the formatting rules of the struct's display, not the string's display trait impl.

But clippy seems to still flag this.

@camsteffen
Copy link
Contributor

@akanalytics Feel free to file a bug if you found a false positive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants