Skip to content

Lint suggestion: nested format! #7667

Closed
@jplatte

Description

@jplatte

What it does

Warn on format! within the arguments of another macro that does formatting such as format! itself, write! or println!.

Suggests replacing the inner format! call with format_args! or inlining it entirely (see example).

Categories (optional)

  • Kind: style / perf

The recommended code is both shorter and avoids a temporary allocation.

Drawbacks

There could be situations when the outer macro is format_args! where lifetimes don't work out with the format! changed to format_args! or inlined entirely. I'm not sure how hard it would be to detect these cases.

Example

println!("error: {}", format!("something failed at {}", Location::caller()));

Could be written as:

println!("error: {}", format_args!("something failed at {}", Location::caller()));

or inlined entirely (when the argument is used exactly once like here):

println!("error: something failed at {}", Location::caller());

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions