Skip to content

Conversation

@nyurik
Copy link
Contributor

@nyurik nyurik commented Feb 7, 2026

This matches cargo fmt behavior but for format!(...) -style macro calls.

To make this PR easier to review, it moves all linting of the clippy code to #16533

Suggest removing an unnecessary trailing comma before the closing parenthesis in single-line format-like macro invocations (e.g. println!, format!, write!). The lint currently only runs on format-like macros because it relies on format-argument parsing; arbitrary user macros are not supported to avoid incorrect suggestions.

  • Lint is in the style group (allow-by-default)
  • Single-line only: multi-line macro invocations are not linted
  • Machine-applicable fix: removes the trailing comma
// catches this
debug!("Foo={}", 1,);

// suggests this
debug!("Foo={}", 1);

Closes #13965

changelog: [unnecessary_trailing_comma]: new lint to remove trailing commas in a single-line format macro usage

@rustbot rustbot added needs-fcp S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Feb 7, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 7, 2026

r? @Jarcho

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

@rustbot

This comment has been minimized.

@nyurik nyurik force-pushed the unnecessary_trailing_comma branch from 46ad29b to 0f7b216 Compare February 7, 2026 08:36
@rustbot

This comment has been minimized.

@nyurik nyurik force-pushed the unnecessary_trailing_comma branch from 0f7b216 to f3f0517 Compare February 7, 2026 08:37
@github-actions
Copy link

github-actions bot commented Feb 7, 2026

Lintcheck changes for d6d1b45

Lint Added Removed Changed
clippy::unnecessary_trailing_comma 17 0 0

This comment will be updated if you push new changes

@nyurik nyurik force-pushed the unnecessary_trailing_comma branch from f3f0517 to f180a19 Compare February 7, 2026 09:07
@rustbot
Copy link
Collaborator

rustbot commented Feb 7, 2026

Some changes occurred in clippy_lints/src/doc

cc @notriddle

let sm = self.cx.sess().source_map();
let span = self.macro_call.span.source_callsite();
if !sm.is_multiline(span)
&& let span = sm.span_extend_to_prev_char_before(span.shrink_to_hi(), ')', false)
Copy link
Contributor

Choose a reason for hiding this comment

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

This means println!["hi",], which is allowed, won't lint.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thx, great catch, totally forgot!!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thx @notriddle , fixed

Copy link
Contributor

Choose a reason for hiding this comment

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

This still doesn't work right in all cases. This test case gives an incorrect "correction":

    println!["Foo(,)", ]; //~ unnecessary_trailing_comma

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@notriddle thx for catching all the edge cases! It took me a bit longer than anticipated. Seem to work now with all the new test cases

@rustbot

This comment has been minimized.

@samueltardieu
Copy link
Member

The style category is warn-by-default. There is a risk that this lint is too opinionated for a warn-by-default category. Also, we don't usually lint for formatting as this is handled by rustfmt.

@nyurik
Copy link
Contributor Author

nyurik commented Feb 8, 2026

@samueltardieu this lint is essentially the missing piece for a single line macro calls that cargo fmt already does for regular function calls. I don't think anyone would ever want to have println!("...",) -- most of these are simply accidental leftovers for when multiline formats get reformatted as a single line. The only reason cargo fmt cannot do it is that it requires type resolution to ensure correct detection. Note that --fix should work perfectly for this lint. I honestly think this is a "style" as anything could possibly get.

@Jarcho
Copy link
Contributor

Jarcho commented Feb 9, 2026

pull bot pushed a commit to Mattlk13/rust-clippy that referenced this pull request Feb 9, 2026
…g#16533)

This noop PR makes the new comma-removing lint PR less noisy, allowing
it focus only on the new functionality

See rust-lang#16530

changelog: none
@nyurik nyurik force-pushed the unnecessary_trailing_comma branch from ccd5535 to 1f35186 Compare February 9, 2026 21:35
@rustbot

This comment has been minimized.

JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Feb 10, 2026
…n, r=mati865

style: remove unneeded trailing commas

Make format-like macro calls look similar to what `cargo fmt` does automatically - remove trailing commas. When removing a comma, I also inlined some variables for consistency and clarity.

I'm working on a [clippy lint](rust-lang/rust-clippy#16530) to make this process automatic.
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Feb 10, 2026
…n, r=mati865

style: remove unneeded trailing commas

Make format-like macro calls look similar to what `cargo fmt` does automatically - remove trailing commas. When removing a comma, I also inlined some variables for consistency and clarity.

I'm working on a [clippy lint](rust-lang/rust-clippy#16530) to make this process automatic.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 10, 2026
…n, r=mati865

style: remove unneeded trailing commas

Make format-like macro calls look similar to what `cargo fmt` does automatically - remove trailing commas. When removing a comma, I also inlined some variables for consistency and clarity.

I'm working on a [clippy lint](rust-lang/rust-clippy#16530) to make this process automatic.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 11, 2026
…n, r=mati865

style: remove unneeded trailing commas

Make format-like macro calls look similar to what `cargo fmt` does automatically - remove trailing commas. When removing a comma, I also inlined some variables for consistency and clarity.

I'm working on a [clippy lint](rust-lang/rust-clippy#16530) to make this process automatic.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 11, 2026
…n, r=mati865

style: remove unneeded trailing commas

Make format-like macro calls look similar to what `cargo fmt` does automatically - remove trailing commas. When removing a comma, I also inlined some variables for consistency and clarity.

I'm working on a [clippy lint](rust-lang/rust-clippy#16530) to make this process automatic.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 11, 2026
…n, r=mati865

style: remove unneeded trailing commas

Make format-like macro calls look similar to what `cargo fmt` does automatically - remove trailing commas. When removing a comma, I also inlined some variables for consistency and clarity.

I'm working on a [clippy lint](rust-lang/rust-clippy#16530) to make this process automatic.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 11, 2026
…n, r=mati865

style: remove unneeded trailing commas

Make format-like macro calls look similar to what `cargo fmt` does automatically - remove trailing commas. When removing a comma, I also inlined some variables for consistency and clarity.

I'm working on a [clippy lint](rust-lang/rust-clippy#16530) to make this process automatic.
Suggest removing an unnecessary trailing comma before the closing
parenthesis in single-line format-like macro invocations (e.g.
println!, format!, write!). The lint currently only runs on
format-like macros because it relies on format-argument parsing;
arbitrary user macros are not supported to avoid incorrect
suggestions.

- Lint is in the `style` group (allow-by-default)
- Single-line only: multi-line macro invocations are not linted
- Machine-applicable fix: removes the trailing comma
@nyurik nyurik force-pushed the unnecessary_trailing_comma branch from 1f35186 to d6d1b45 Compare February 11, 2026 20:44
@rustbot
Copy link
Collaborator

rustbot commented Feb 11, 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.

rust-timer added a commit to rust-lang/rust that referenced this pull request Feb 12, 2026
Rollup merge of #152372 - nyurik:clippy-rustc_trait_selection, r=mati865

style: remove unneeded trailing commas

Make format-like macro calls look similar to what `cargo fmt` does automatically - remove trailing commas. When removing a comma, I also inlined some variables for consistency and clarity.

I'm working on a [clippy lint](rust-lang/rust-clippy#16530) to make this process automatic.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-fcp S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Suggest to remove unneeded trailing comma

5 participants