Skip to content

Keep comments inside parens when suggesting to remove unused delims - #165036

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
yashaggarwal85d:fix-unused-delims-comments
Nov 9, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
yashaggarwal85d:fix-unused-delims-comments

Conversation

@yashaggarwal85d

@yashaggarwal85d yashaggarwal85d commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

When there's a comment between a paren and the inner expression, unused_parens suggests removing
it along with the paren, and with line breaks the spans cover the whole block. This points at the
delimiters only in that case, and only removes the delimiter on a side that has a comment.

Fixes #164722

FYI, tests are written with the help of AI. I reviewed and they are working as expected. Thanks

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 27, 2026
@rustbot

rustbot commented Oct 27, 2026

Copy link
Copy Markdown
Collaborator

r? @adwinwhite

rustbot has assigned @adwinwhite.
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: compiler
  • compiler expanded to 77 candidates
  • Random selection from 20 candidates

@oli-obk oli-obk closed this Oct 28, 2026
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 28, 2026
@oli-obk

oli-obk commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Reopening after a conversation with the author

@oli-obk oli-obk reopened this Oct 29, 2026
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 29, 2026
@rust-log-analyzer

This comment has been minimized.

@yashaggarwal85d

yashaggarwal85d commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi @adwinwhite can you have a look at this! Lemme know if any change is needed! Thanks

@adwinwhite adwinwhite 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.

Could you also fix the CI error?

View changes since this review

Comment thread compiler/rustc_lint/src/unused.rs Outdated
let sm = cx.sess().source_map();
// If there is a comment or a line break between a delimiter and the inner value,
// point at the delimiter alone, and never suggest removing the comment.
let shrink = |span: Span, open: bool, keep_newlines: bool| {

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.

Why do we need keep_newlines? It's better to keep spans in main message and help consistent?

The rule is if we have comments, we keep newlines/other whilespaces? 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, dropped it. Now each side shrinks to just the delimiter only if there's a comment, same spans for the message and the help.

Comment thread compiler/rustc_lint/src/unused.rs Outdated
Comment on lines +399 to +404
let (delim, gap) = if open {
(snip.starts_with(['(', '{']), snip.get(1..))
} else {
(snip.ends_with([')', '}']), snip.get(..snip.len().saturating_sub(1)))
};
let Some(gap) = gap.filter(|_| delim) else { return span };

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.

We can check whether the spans are empty and obtain gap directly?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, gap is taken from the span now.

@adwinwhite

Copy link
Copy Markdown
Contributor

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 2, 2026
@rustbot

rustbot commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

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

@yashaggarwal85d
yashaggarwal85d force-pushed the fix-unused-delims-comments branch from c5abcf1 to 7b55ab6 Compare November 2, 2026 04:38
@rustbot

rustbot commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main 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.

@yashaggarwal85d

Copy link
Copy Markdown
Contributor Author

Could you also fix the CI error?

View changes since this review

Sure, I'll re-request a review as soon as the CI passes!

Comment on lines +49 to +56
LL | if (
| _________^
LL | | true
| | ________^____^
| ||________|
| |
LL | | ) {}
| |_____^

@adwinwhite adwinwhite Nov 3, 2026 •

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, I get the newline thing now.

I guess the rules are

  • We display single letter span in warning, regardless of whether there's a comment.
  • We shrink the span to a single letter in help/autofix, if there's a comment.

View changes since the review

let (/* d */ _x) = 1;
//~^ WARN unnecessary parentheses around pattern

foo::<{ /* e */ 7 }>();

@adwinwhite adwinwhite Nov 3, 2026 •

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.

We need more test cases for braces? Like one-line closure body.

View changes since the review

Comment thread compiler/rustc_lint/src/unused.rs Outdated
let sm = cx.sess().source_map();
// Returns the delimiter itself, and whether there is a comment between it and the
// inner value.
let delim = |span: Span, open: bool| {

@adwinwhite adwinwhite Nov 4, 2026 •

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.

Using Option combinators below makes the code quite unreadable. And the delim naming is confusing.
How about using previous let shrink = |span: Span, open: bool, in_help: bool| -> Span?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah that sounds good, I'll do exactly that

@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 (such as code changes or more information) from the author. labels Nov 7, 2026
@adwinwhite

Copy link
Copy Markdown
Contributor

Thanks!

@bors rollup
@bors r+ squash

@rust-bors

This comment has been minimized.

* Keep comments inside parens when suggesting to remove unused delims
* Only shrink unused delim spans when there is a comment
* Always point at the delimiters alone in unused delims warnings
* Use a single shrink closure for unused delim spans
@rust-bors

rust-bors Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

🔨 4 commits were squashed into 7c2e5a5.

@rust-bors

rust-bors Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 7c2e5a5 has been tentatively approved by adwinwhite

It will be put into the queue for this repository once PR CI succeeds.

@rust-bors
rust-bors Bot force-pushed the fix-unused-delims-comments branch from c89ce74 to 7c2e5a5 Compare November 9, 2026 02:11
@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 9, 2026
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 9, 2026
…comments, r=adwinwhite

Keep comments inside parens when suggesting to remove unused delims

When there's a comment between a paren and the inner expression, `unused_parens` suggests removing
it along with the paren, and with line breaks the spans cover the whole block. This points at the
delimiters only in that case, and only removes the delimiter on a side that has a comment.

Fixes rust-lang#164722

FYI, tests are written with the help of AI. I reviewed and they are working as expected. Thanks
Zalathar added a commit to Zalathar/rust that referenced this pull request Nov 9, 2026
…comments, r=adwinwhite

Keep comments inside parens when suggesting to remove unused delims

When there's a comment between a paren and the inner expression, `unused_parens` suggests removing
it along with the paren, and with line breaks the spans cover the whole block. This points at the
delimiters only in that case, and only removes the delimiter on a side that has a comment.

Fixes rust-lang#164722

FYI, tests are written with the help of AI. I reviewed and they are working as expected. Thanks
rust-bors Bot pushed a commit that referenced this pull request Nov 9, 2026
Rollup of 7 pull requests

Successful merges:

 - #165660 (rustdoc: suggest reference definitions for unresolved links)
 - #163749 (Refactor enum layout calculation)
 - #165036 (Keep comments inside parens when suggesting to remove unused delims)
 - #165658 (get rid of InterpCx::get_alloc_bytes_unchecked_raw(_mut))
 - #165664 (Fix unused imports in VEXos filesystem)
 - #165666 (Error on `macro_use` attribute on decl macro)
 - #165667 (Bump deps for `citool` and main workspace)
@rust-bors
rust-bors Bot merged commit 3521ea2 into rust-lang:main Nov 9, 2026
14 checks passed
@rustbot rustbot added this to the 1.102.0 milestone Nov 9, 2026
rust-bors Bot pushed a commit that referenced this pull request Nov 9, 2026
Rollup merge of #165036 - yashaggarwal85d:fix-unused-delims-comments, r=adwinwhite

Keep comments inside parens when suggesting to remove unused delims

When there's a comment between a paren and the inner expression, `unused_parens` suggests removing
it along with the paren, and with line breaks the spans cover the whole block. This points at the
delimiters only in that case, and only removes the delimiter on a side that has a comment.

Fixes #164722

FYI, tests are written with the help of AI. I reviewed and they are working as expected. Thanks
programskillforverification pushed a commit to programskillforverification/miri that referenced this pull request Nov 10, 2026
Rollup of 7 pull requests

Successful merges:

 - rust-lang/rust#165660 (rustdoc: suggest reference definitions for unresolved links)
 - rust-lang/rust#163749 (Refactor enum layout calculation)
 - rust-lang/rust#165036 (Keep comments inside parens when suggesting to remove unused delims)
 - rust-lang/rust#165658 (get rid of InterpCx::get_alloc_bytes_unchecked_raw(_mut))
 - rust-lang/rust#165664 (Fix unused imports in VEXos filesystem)
 - rust-lang/rust#165666 (Error on `macro_use` attribute on decl macro)
 - rust-lang/rust#165667 (Bump deps for `citool` and main workspace)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Poor diagnostic for unused_parens around if condition with comments inside

5 participants