Skip to content

Add useless_borrows_in_formatting lint#16523

Merged
samueltardieu merged 1 commit into
rust-lang:masterfrom
nyurik:ref_in_format
Apr 19, 2026
Merged

Add useless_borrows_in_formatting lint#16523
samueltardieu merged 1 commit into
rust-lang:masterfrom
nyurik:ref_in_format

Conversation

@nyurik
Copy link
Copy Markdown
Contributor

@nyurik nyurik commented Feb 6, 2026

View all comments

Fixes #10851

This is a workaround for the current compiler limitation that results in a ~6% performance degradation.

This lint detects format macros where an argument is passed with an explicit & references, e.g. println!("{}", &s)

  • Lint runs for both Display ({}) and Debug ({:?}) placeholders when the inner type is Sized and implements the corresponding trait.
  • Applies to the main value argument and to width/precision arguments (e.g. format!("{0:1$.2$}", &v1, &v2, &v3)).
  • Suggests removing the redundant & with MachineApplicable fix.
  • Skip when the argument comes from expansion or a proc macro.

changelog: [useless_borrows_in_formatting]: detect redundant & in format macro arguments

P.S. I think this lint should go into perf category once the code is reviewed

@rustbot rustbot added needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Feb 6, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Feb 6, 2026

r? @samueltardieu

rustbot has assigned @samueltardieu.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • People who recently interacted with files modified in this PR: 7 candidates
  • 7 candidates expanded to 7 candidates
  • Random selection from Jarcho, dswij, llogiq, samueltardieu

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Feb 6, 2026

Lintcheck changes for aaa5bd2

Lint Added Removed Changed
clippy::useless_borrows_in_formatting 17 0 0

This comment will be updated if you push new changes

Copy link
Copy Markdown
Member

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer if rustc would fix this to inline the generated function, but even then that wouldn't apply to older versions, and removing the useless reference is cleaner, so I think this lint is a good idea.

I'd like to see tests with macros (println! in a macro itself, arguments in a macro, etc.) to ensure this is solid, especially if this becomes a warn-by-default lint. Also, expressions with parentheses such as &(…) or blocks such as &{…}.

View changes since this review

Comment thread clippy_lints/src/format_args.rs Outdated
Comment thread clippy_lints/src/format_args.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Feb 10, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Feb 10, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot

This comment has been minimized.

@nyurik
Copy link
Copy Markdown
Contributor Author

nyurik commented Feb 10, 2026

Thx @samueltardieu, fixed

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Feb 10, 2026
@nyurik nyurik requested a review from samueltardieu February 10, 2026 20:30
@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@samueltardieu samueltardieu added the lint-nominated Create an FCP-thread on Zulip for this PR label Mar 6, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 6, 2026

This lint has been nominated for inclusion.

A FCP topic has been created on Zulip.

Copy link
Copy Markdown
Member

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please rename the lint into useless_borrows_in_formatting, and also rename the files implementing the lint and the test files accordingly?

It would also be great if you could split this into two commits:

  • The first one with the result of this lint applied to the current Clippy sources
  • The second one with the lint itself

This way, the lint implementation stays isolated inside one commit (and can be reverted for example if there are any problems, or better identified in case of a bisection), and at any point the tree stays CI-green.

View changes since this review

Comment thread clippy_lints/src/format_args.rs Outdated
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Mar 17, 2026
@rustbot

This comment has been minimized.

@nyurik
Copy link
Copy Markdown
Contributor Author

nyurik commented Apr 17, 2026

@samueltardieu thx, I updated the lint and moved the unrelated changes to #16872

@nyurik nyurik requested a review from samueltardieu April 17, 2026 02:05
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties and removed S-waiting-on-author Status: This is awaiting some action from the author. (Use `@rustbot ready` to update this status) labels Apr 17, 2026
@samueltardieu
Copy link
Copy Markdown
Member

If you had made it a commit in the same PR, then this PR would have been green.

Can you rebase once the other PR is merged into master (should be ok in a few minutes)?

github-merge-queue Bot pushed a commit that referenced this pull request Apr 17, 2026
minor code cleanup getting ready for the `useless_borrows_in_formatting`
lint

See #16523

r? @samueltardieu

---

changelog: none
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 17, 2026

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@nyurik
Copy link
Copy Markdown
Contributor Author

nyurik commented Apr 17, 2026

thx @samueltardieu , i misunderstood your comment - I usually squash all PRs into a single commit in all my projects, so I thought you wanted multiple PRs, not multiple commits in the same PR. Now rebased onto main, and should pass.

Copy link
Copy Markdown
Member

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Latest nit: can you also perform the renaming in the PR title, PR initial message and commit message before this gets merged? There should no longer be a mention of redundant_ref_in_format_args.

View changes since this review

@nyurik nyurik changed the title Add redundant_ref_in_format_args lint Add useless_borrows_in_formatting lint Apr 17, 2026
@nyurik
Copy link
Copy Markdown
Contributor Author

nyurik commented Apr 17, 2026

done

@samueltardieu
Copy link
Copy Markdown
Member

done

Not in the commit message (which does not require the changelog btw, only the PR description does).

Detect format macros where an argument
is passed with an explicit `&` even though the formatter already takes
references, e.g. `println!("{}", &s)` when `s: &str`.

Some original micro-benchmarks showed ~6% performance improvement when redundant refs are removed.

- Lint runs for both Display (`{}`) and Debug (`{:?}`) placeholders when
  the inner type is Sized and implements the corresponding trait.
- Applies to the main value argument and to width/precision arguments
  (e.g. `format!("{0:1$.2$}", &v1, &v2, &v3)`).
- Suggests removing the redundant `&` with MachineApplicable fix.
- Skip when the argument comes from expansion or a proc macro.
@nyurik
Copy link
Copy Markdown
Contributor Author

nyurik commented Apr 17, 2026

ah, right, thx, fixed

Copy link
Copy Markdown
Member

@samueltardieu samueltardieu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Latest nit: can you also perform the renaming in the PR title, PR initial message and commit message before this gets merged? There should no longer be a mention of redundant_ref_in_format_args.

View changes since this review

@samueltardieu samueltardieu added this pull request to the merge queue Apr 19, 2026
Merged via the queue into rust-lang:master with commit 0c71cf8 Apr 19, 2026
11 of 13 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Apr 19, 2026
@nyurik nyurik deleted the ref_in_format branch April 19, 2026 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lint-nominated Create an FCP-thread on Zulip for this PR needs-fcp PRs that add, remove, or rename lints and need an FCP

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants