Repository navigation
Keep comments inside parens when suggesting to remove unused delims - #165036
Conversation
|
r? @adwinwhite rustbot has assigned @adwinwhite. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
Reopening after a conversation with the author |
This comment has been minimized.
This comment has been minimized.
|
Hi @adwinwhite can you have a look at this! Lemme know if any change is needed! Thanks |
| 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| { |
There was a problem hiding this comment.
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? 🤔
There was a problem hiding this comment.
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.
| 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 }; |
There was a problem hiding this comment.
We can check whether the spans are empty and obtain gap directly?
There was a problem hiding this comment.
Done, gap is taken from the span now.
|
@rustbot author |
|
Reminder, once the PR becomes ready for a review, use |
c5abcf1 to
7b55ab6
Compare
|
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. |
Sure, I'll re-request a review as soon as the CI passes! |
| LL | if ( | ||
| | _________^ | ||
| LL | | true | ||
| | | ________^____^ | ||
| | ||________| | ||
| | | | ||
| LL | | ) {} | ||
| | |_____^ |
There was a problem hiding this comment.
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.
| let (/* d */ _x) = 1; | ||
| //~^ WARN unnecessary parentheses around pattern | ||
|
|
||
| foo::<{ /* e */ 7 }>(); |
There was a problem hiding this comment.
We need more test cases for braces? Like one-line closure body.
| 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| { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Yeah that sounds good, I'll do exactly that
This comment has been minimized.
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
|
🔨 4 commits were squashed into 7c2e5a5. |
c89ce74 to
7c2e5a5
Compare
…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
…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
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)
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
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)
View all comments
When there's a comment between a paren and the inner expression,
unused_parenssuggests removingit 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