Skip to content

rustdoc's automatic link suggestion is syntactically invalid when the link is in a #[doc] attribute #135851

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Fulgen301 opened this issue Jan 21, 2025 · 3 comments · May be fixed by #136400
Open
Assignees
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@Fulgen301
Copy link
Contributor

Fulgen301 commented Jan 21, 2025

#[doc = "https://rust-lang.org"]
pub fn doc_attribute() {}

/// https://rust-lang.org
pub fn doc_comment() {}

Running rustdoc on that code produces the following output:

warning: this URL is not a hyperlink
 --> issue.rs:1:9
  |
1 | #[doc = "https://rust-lang.org"]
  |         ^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: bare URLs are not automatically turned into clickable links
  = note: `#[warn(rustdoc::bare_urls)]` on by default
help: use an automatic link instead
  |
1 | #[doc = <"https://rust-lang.org">]
  |         +                       +

warning: this URL is not a hyperlink
 --> issue.rs:4:5
  |
4 | /// https://rust-lang.org
  |     ^^^^^^^^^^^^^^^^^^^^^
  |
  = note: bare URLs are not automatically turned into clickable links
help: use an automatic link instead
  |
4 | /// <https://rust-lang.org>
  |     +                     +

warning: 2 warnings emitted

The replacement suggestion is correct for the link in the doc comment, but syntactically incorrect for the one in the #[doc] attribute - it should suggest #[doc = "<https://rust-lang.org>"] instead.

The bug happens on both stable (rustdoc 1.84.0 (9fc6b4312 2025-01-07)) and nightly (rustdoc 1.86.0-nightly (f3d1d47fd 2025-01-20)).

@Fulgen301 Fulgen301 added the C-bug Category: This is a bug. label Jan 21, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 21, 2025
@Fulgen301 Fulgen301 changed the title rustdoc's automatic link suggestion is invalid syntax when the link is in a #[doc] attribute rustdoc's automatic link suggestion is syntactically invalid when the link is in a #[doc] attribute Jan 21, 2025
@fmease fmease added T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. C-bug Category: This is a bug. labels Jan 22, 2025
@linyihai
Copy link
Contributor

Hi, I maybe found the outmost codes that thriggered this issue

pub(super) fn visit_item(cx: &DocContext<'_>, item: &Item, hir_id: HirId, dox: &str) {
let report_diag = |cx: &DocContext<'_>, msg: &'static str, range: Range<usize>| {
let sp = source_span_for_markdown_range(cx.tcx, dox, &range, &item.attrs.doc_strings)
.unwrap_or_else(|| item.attr_span(cx.tcx));
cx.tcx.node_span_lint(crate::lint::BARE_URLS, hir_id, sp, |lint| {
lint.primary_message(msg)
.note("bare URLs are not automatically turned into clickable links")
.multipart_suggestion(
"use an automatic link instead",
vec![
(sp.shrink_to_lo(), "<".to_string()),
(sp.shrink_to_hi(), ">".to_string()),
],
Applicability::MachineApplicable,
);
});
};

For the raw doc, the sp includes the double quotation marks(like "https:://somewhere.com", not https:://somewhere.com). I did a hack solution to drop the double quotation marks

            let mut sp = sp;
            if let Some(frame) = &item.attrs.doc_strings.first()
                && frame.kind == DocFragmentKind::RawDoc
            {
                sp = sp.with_lo(sp.lo() + BytePos(1));
                sp = sp.with_hi(sp.hi() - BytePos(1));
            }

I'm not sure it's deserve to submit a PR bases on this hack solution.

@lolbinarycat lolbinarycat self-assigned this Feb 1, 2025
@lolbinarycat
Copy link
Contributor

I belive this is because of the limitations of source_span_for_markdown_range

@lolbinarycat
Copy link
Contributor

oh yeah the current system is extremely broken if there's even a single #[doc] attribute.

lolbinarycat added a commit to lolbinarycat/rust that referenced this issue Feb 1, 2025
bors added a commit to rust-lang-ci/rust that referenced this issue Apr 2, 2025
…1, r=<try>

Improve handling of rustdoc lints when used with raw doc fragments.

1. `rustdoc::bare_urls` no longer outputs incoherent suggestions if `source_span_for_markdown_range` returns None, instead outputting no suggestion
2. `source_span_for_markdown_range` has one more heuristic, so it will return `None` less often.
3. add ui test to make sure we don't emit nonsense suggestions.

fixes rust-lang#135851
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants