Skip to content

fix: handle empty /**/ in block_to_line assist - #22073

Merged
ChayimFriedman2 merged 1 commit into
rust-lang:masterfrom
Albab-Hasan:fix-22071-empty-block-comment-panic
Apr 17, 2026
Merged

ChayimFriedman2 merged 1 commit into
rust-lang:masterfrom
Albab-Hasan:fix-22071-empty-block-comment-panic

Conversation

@Albab-Hasan

Copy link
Copy Markdown
Contributor

block_to_line sliced &text[prefix.len()..text.len()-2], which panicked on /**/ since its prefix matches the entire comment (len 4) and text.len() - 2 = 2, producing the invalid range [4..2].

use strip_prefix/strip_suffix and emit a plain // when the body is empty after trimming.

fixes #22071

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

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

I prefer to fix CommentKind::prefix() so it will only include the prefix, aka. /*, and not the full /**/. It seems to work for all places using it.

View changes since this review

`Comment::prefix` iterated `CommentKind::BY_PREFIX` forward, so `/**/`
matched the `/**/` entry itself and returned the full 4-char string.
`block_to_line` then computed `&text[4..text.len()-2]` = `&text[4..2]`
and panicked.

Delegate `Comment::prefix` to `CommentKind::prefix`, which already
iterates in reverse and returns the canonical short form (`/*` for
non-doc block comments). The `/**/` and `/***` entries in `BY_PREFIX`
are still needed for `from_text` to classify them as non-doc blocks.

fixes rust-lang#22071
@Albab-Hasan
Albab-Hasan force-pushed the fix-22071-empty-block-comment-panic branch from c1f0e60 to beacd8a Compare April 17, 2026 10:37
@Albab-Hasan

Copy link
Copy Markdown
Contributor Author

@ChayimFriedman2 done

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

@ChayimFriedman2
ChayimFriedman2 added this pull request to the merge queue Apr 17, 2026
Merged via the queue into rust-lang:master with commit a52fa8d Apr 17, 2026
18 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 17, 2026
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.

block_to_line (convert block comment to line comments) panics on empty /**/

3 participants