Skip to content

fix(useless_format): improve suggestion - #16595

Merged
ada4a merged 1 commit into
rust-lang:masterfrom
nyurik:useless_format_str
Sep 7, 2026
Merged

fix(useless_format): improve suggestion#16595
ada4a merged 1 commit into
rust-lang:masterfrom
nyurik:useless_format_str

Conversation

@nyurik

@nyurik nyurik commented Feb 20, 2026

Copy link
Copy Markdown
Contributor

View all comments

Fix #3361

Handled test case:

fn literal(lit: &str) {}
let lit = "hello";

let _: &str = &format!("hello");  /* => */  let _: &str = "hello";
literal(&format!("hello"));       /* => */  literal("hello");
literal(&format!("{}", lit));     /* => */  literal(lit);
literal(&format!("{lit}"));       /* => */  literal(lit);

This pull request improves the useless_format lint in Clippy by making its suggestions when format! is immediately borrowed as a string slice. The changes also refactor how lint messages and suggestions are generated for better clarity and maintainability.

Improvements to lint suggestions:

  • The lint now detects when format! is immediately borrowed as &format!("literal") and suggests using the string literal directly, instead of calling .to_string(). This provides a more idiomatic and efficient suggestion.
  • The suggestion logic is updated so that only immutable borrows (&format!) are handled with the new message; mutable borrows (&mut format!) still suggest .to_string().

changelog: [useless_format]: for &format!("literal") suggest "literal" as a replacement

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Feb 20, 2026
@rustbot

rustbot commented Feb 20, 2026

Copy link
Copy Markdown
Collaborator

r? @llogiq

rustbot has assigned @llogiq.
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:

  • Owners of files modified in this PR: 7 candidates
  • 7 candidates expanded to 7 candidates
  • Random selection from Jarcho, dswij, llogiq, samueltardieu

@llogiq llogiq left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks like a solid improvement. I just have a few small nits, otherwise fine to merge.

View changes since this review

Comment thread clippy_lints/src/format.rs Outdated
Comment thread clippy_lints/src/format.rs Outdated
Comment thread tests/ui/format.rs
@nyurik
nyurik force-pushed the useless_format_str branch from 61470b0 to 3a22fa5 Compare February 22, 2026 18:16
@rustbot

This comment has been minimized.

@nyurik

nyurik commented Feb 22, 2026

Copy link
Copy Markdown
Contributor Author

thanks @llogiq, fixed

@llogiq

llogiq commented Feb 22, 2026

Copy link
Copy Markdown
Contributor

Thank you!

@llogiq
llogiq added this pull request to the merge queue Feb 22, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Feb 22, 2026
@nyurik

nyurik commented Feb 22, 2026

Copy link
Copy Markdown
Contributor Author

@llogiq seems like merge queue is being ... resistive

@nyurik
nyurik force-pushed the useless_format_str branch from 3a22fa5 to fcd1e55 Compare March 5, 2026 16:30
@rustbot

This comment has been minimized.

@nyurik

nyurik commented Mar 5, 2026

Copy link
Copy Markdown
Contributor Author

@llogiq could you try to add it to the merge queue again?

@llogiq
llogiq enabled auto-merge March 5, 2026 16:50
@ada4a

ada4a commented Mar 14, 2026

Copy link
Copy Markdown
Contributor

Hm, but clippy lints are supposed to compose, no? If a user runs cargo clippy --fix, then useless_format and needless_conversion will trigger in sequence, achieving the same result while allowing each lint to only think about its thing

auto-merge was automatically disabled April 17, 2026 03:59

Head branch was pushed to by a user without write access

@nyurik
nyurik force-pushed the useless_format_str branch from fcd1e55 to aec946c Compare April 17, 2026 03:59
@rustbot

This comment has been minimized.

@nyurik

nyurik commented Apr 21, 2026

Copy link
Copy Markdown
Contributor Author

@ada4a yes, but clippy is frequently used via rust analyzer, so people would see a suggestion in the IDE, click "fix", and the fix would be flagged again as another fix - i.e. annoying :)

@llogiq friendly ping, or let me know if someone else should review this
cc: @samueltardieu

@llogiq

llogiq commented Apr 24, 2026

Copy link
Copy Markdown
Contributor

I think @ada4a is right about the composability issue. Can we fix it within this PR or should we wait for a followup?

@nyurik

nyurik commented Apr 24, 2026

Copy link
Copy Markdown
Contributor Author

@llogiq I am not certain how this PR should be modified... If clippy makes a suggestion, I would think the suggestion should be "as good as possible", rather than relying on a chain of lints, esp if some lints in that chain might have been disabled for various reasons.

@nyurik

nyurik commented May 1, 2026

Copy link
Copy Markdown
Contributor Author

@llogiq friendly ping - what should be done here?

@nyurik
nyurik force-pushed the useless_format_str branch from aec946c to 533a114 Compare May 1, 2026 15:33
@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@nyurik
nyurik force-pushed the useless_format_str branch from 533a114 to 729a91e Compare May 23, 2026 16:44
@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@nyurik
nyurik force-pushed the useless_format_str branch from 729a91e to e2f0fe5 Compare June 5, 2026 02:49
@rustbot

This comment has been minimized.

@rustbot

This comment has been minimized.

@ada4a

ada4a commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Actually, I've changed my mind: in general, lints should be composable, and shouldn't be overcomplicated by trying to predict and handle subsequent fixes -- but in simple cases like this, doing a little extra work to clearly improve the suggestion and avoid another round of cargo clippy is obviously worth it.

So I'd say this PR would be good to merge as-is, after a rebase.

@nyurik
nyurik force-pushed the useless_format_str branch from e2f0fe5 to fcf0942 Compare August 29, 2026 15:40
@rustbot

rustbot commented Aug 29, 2026

Copy link
Copy Markdown
Collaborator

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

nyurik commented Aug 29, 2026

Copy link
Copy Markdown
Contributor Author

@ada4a thx, rebased

r? @ada4a

@rustbot rustbot assigned ada4a and unassigned llogiq Aug 29, 2026
@rustbot

rustbot commented Aug 29, 2026

Copy link
Copy Markdown
Collaborator

ada4a is not on the review rotation at the moment.
They may take a while to respond.

@CommanderStorm

Copy link
Copy Markdown
Contributor

r? clippy
Think this is ready with no objections..

@rustbot rustbot assigned flip1995 and unassigned ada4a Sep 6, 2026

@ada4a ada4a left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry, seems like I got distracted while approving this :p

Thanks for the reminder, @CommanderStorm ^^

View changes since this review

@ada4a
ada4a added this pull request to the merge queue Sep 7, 2026
Merged via the queue into rust-lang:master with commit 09b4e11 Sep 7, 2026
11 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Sep 7, 2026
@nyurik
nyurik deleted the useless_format_str branch September 7, 2026 13:34
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

Successfully merging this pull request may close these issues.

useless_format: .to_string() suggested where format! could be omitted

6 participants