Skip to content
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

format!("{}", X) -> X.to_string() #8238

Closed
mark-summerfield opened this issue Jan 7, 2022 · 3 comments
Closed

format!("{}", X) -> X.to_string() #8238

mark-summerfield opened this issue Jan 7, 2022 · 3 comments
Labels
A-lint Area: New lints

Comments

@mark-summerfield
Copy link

What it does

Since many types support to_string() if format! is being used just to produce a string version then surely calling to_string() is better?

Lint Name

Convert an item to a string

Category

style

Advantage

I assume that this would be more efficient. It looks nicer too.

Drawbacks

No response

Example

    let year = Local::today().year();
    let year = if year == 2021 {
        format!("{}", year)
    } else {
        format!("2021-{}", year - 2000)
    };

Could be written as:

    let year = Local::today().year();
    let year = if year == 2021 {
        year.to_string()
    } else {
        format!("2021-{}", year - 2000)
    };
@mark-summerfield mark-summerfield added the A-lint Area: New lints label Jan 7, 2022
@giraffate
Copy link
Contributor

Do you mean the useless_format lint?

@mark-summerfield
Copy link
Author

No, it is different. The useless_format hint is where the X is a string; I'm talking about when it isn't a string but does have a to_string() method, e.g., an i32.

@camsteffen
Copy link
Contributor

Duplicate of #3156

@camsteffen camsteffen marked this as a duplicate of #3156 Jan 7, 2022
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