Skip to content

A new lint to remove unneeded arg referencing in a format! #10851

Open
@nyurik

Description

@nyurik

What it does

Passing a reference to a value instead of the value itself to the format! macro causes unneeded double referencing that is not compiled away, as described in my stackoverflow question, and demonstrated in the 1.69 assembly output. The issue seem to be that reference format dispatch is not inlined by llvm. While this might be some issue with the Rust compiler itself, or an issue that is hard/impossible to solve in a general case, I think we could introduce a lint in the mean time to suggest it to improve readability and performance.

Lint Name

unneeded_format_arg_ref

Category

No response

Advantage

  • Improve runtime performance
  • Remove unneeded value referencing
  • Allow variable inlining, e.g. format!("{}", &var) --> format!("{var}")

Drawbacks

Uncertain if there could ever be an edge case where formatting &var and var would produce different result, e.g. if format wants to print the address of a variable?

Example

format("{}", &foo.bar)

Could be written as:

format("{}", foo.bar)

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