Skip to content

Don't split link destinations when wrap_comments is enabled - #7197

Open
AlexeyL54 wants to merge 1 commit into
rust-lang:mainfrom
AlexeyL54:comment-7177-dont-split-link-destinations
Open

AlexeyL54 wants to merge 1 commit into
rust-lang:mainfrom
AlexeyL54:comment-7177-dont-split-link-destinations

Conversation

@AlexeyL54

@AlexeyL54 AlexeyL54 commented Oct 19, 2026 •

Copy link
Copy Markdown
  • I did not use an LLM to create a change in this PR.
  • I used an LLM to create a change in this PR, and I have explained below how it was used.

Description

Fix splitting lines containing intra-doc link destinations.

Fixes #7177

Problem

Rustfmt splits lines inside intra-doc link destinations that contain @ (e.g. fn@super::foo) or / (e.g. some/path/foo.html), producing broken intra-doc links.

Solution

Added a call to a new function in should_wrap_comment that returns true if the line contains a link destination.

Use of LLM

I used LLM only for asking questions about the codebase and wrote code by hand. No agents.

@rustbot rustbot added the S-waiting-on-review Status: awaiting review from the assignee but also interested parties. label Oct 19, 2026
@jieyouxu jieyouxu self-assigned this Oct 19, 2026
@@ -0,0 +1,24 @@
//@ rustfmt-config: issue-7177.toml

@ytmimi ytmimi Oct 19, 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.

Why do we need the issue-7177.toml if it's just setting error_on_unformatted=false? Can't you do that with the inline config directives?

View changes since the review

@jieyouxu jieyouxu Oct 20, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We don't need error_on_unformatted=false since that's the default value anyway.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I'll remove issue-7177.toml and unnecessary inline directives.

I initially added the config to prevent the test from failing with error[internal]: line formatted, but exceeded maximum width when I had both source and target. Now I see there was no need for that.

@@ -0,0 +1,24 @@
//@ rustfmt-config: issue-7177.toml
//@ rustfmt-wrap_comments: true
//@ rustfmt-unstable_features: true

@ytmimi ytmimi Oct 19, 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.

It shouldn't be necessary to set unstable_features in tests.

View changes since the review

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Removed rustfmt-unstable_features: true directive.

@ytmimi ytmimi Oct 19, 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.

If there's no difference between the source and the target then we only need the target file for the idempotence test.

View changes since the review

Comment thread src/comment.rs
Comment on lines +985 to +1005
/// Returns `true` if the given string includes link destination.
/// The pattern is (e.g. `](<disambiguator>@...)`, `](some/path/...)`)
fn is_doc_link(s: &str) -> bool {
let mut rest = s;

while let Some(start) = rest.find("](") {
let after = &rest[start + 2..];

if let Some(end) = after.find(')') {
let dest = &after[..end];

if dest.contains('@') || dest.contains('/') {
return true;
}
rest = &after[end + 1..];
} else {
break;
}
}
false
}

@ytmimi ytmimi Oct 19, 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.

As mentioned in #7177 (comment), we probably want to be extra conservative and not break for any inline link

View changes since the review

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for the feedback. I'll update is_doc_link to be more conservative: instead of checking for @ and / inside ](...), it will skip wrapping for any line containing ](...).

Is it okay to also skip wrapping for cases that are not links according to the CommonMark spec, e.g. [a](<b)c, [a](<b)c>, [a](<b>c)? These would be false positives for ](...) detection, but skipping them seems safe and consistent with the conservative approach.

@ytmimi ytmimi added S-waiting-on-author Status: 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 Oct 19, 2026
@jieyouxu jieyouxu added the UO-wrap_comments Unstable option: wrap_comments label Oct 20, 2026
Comment thread src/comment.rs
Comment on lines +845 to +847
// 4) No URLs were found in the comment
// 5) No doc links were found (e.g. `](fn@...)`, `](some/path/...)`)
// 6) The comment is not a table item

@jieyouxu jieyouxu Oct 20, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should also update Configurations.md if we change the behavior here

View changes since the review

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Updated Configurations.md to mention that wrapping is also skipped for lines containing intra-doc links.

@@ -0,0 +1,24 @@
//@ rustfmt-config: issue-7177.toml

@jieyouxu jieyouxu Oct 20, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We don't need error_on_unformatted=false since that's the default value anyway.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. UO-wrap_comments Unstable option: wrap_comments

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wrap_comments splits inside a link destination containing @ or /, breaking the link

4 participants