Skip to content

wrap_comments breaks local links #4130

Closed
@Emoun

Description

@Emoun

When wrap_comments=true, crate-local links in docs might get split over two lines, breaking them.

Where rustfmt won't break doc links that start with e.g. http, links starting with ../ are not recognized as links and may be split over two lines. Example:

/// [some_local_link](../super_package/module1/module2/module3/module4/struct.SomeStruct.html)
fn some_fn() {}

Running rustfmt results in the following:

/// [some_local_link](../super_package/module1/module2/module3/module4/struct.
/// SomeStruct.html)
fn some_fn() {}

This crate-local link will no longer work when rustdoc is run.

I have no prior experience with the rustfmt codebase, but I think the issue is with the following function:

// Path: rustfmt/rustfmt-core/rustfmt-lib/src/comments.rs

/// Returns `true` if the given string MAY include URLs or alike.
fn has_url(s: &str) -> bool {
    // This function may return false positive, but should get its job done in most cases.
    s.contains("https://") || s.contains("http://") || s.contains("ftp://") || s.contains("file://")
}

It seems to me to be the function that is responsible for URL recognition. It is clear it doesn't take crate-local links into account.
Looking at related issues, it seems rustfmt/rustfmt-core/rustfmt-lib/src/string::detect_url() has the same job, and might already take local links into account. Maybe use it instead?

I'd be willing to try and create a test and fix PR if you give the go-ahead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions