about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2024-07-03 20:00:04 +0000
committerEsteban Küber <esteban@kuber.com.ar>2024-07-04 00:40:04 +0000
commit140392b0418825c77ba68ab693dc18494ab27405 (patch)
tree709e9c143fa3bb79c3aa2cb9fbd0bd8793fb8d24
parent1cfd47fe0b78f48a04ac8fce792a406b638da40b (diff)
downloadrust-140392b0418825c77ba68ab693dc18494ab27405.tar.gz
rust-140392b0418825c77ba68ab693dc18494ab27405.zip
Adjust rustdoc automatic link suggestion
Use more accurate spans for multipart suggestion.
-rw-r--r--src/librustdoc/passes/lint/bare_urls.rs36
-rw-r--r--tests/rustdoc-ui/diagnostic-width.stderr6
-rw-r--r--tests/rustdoc-ui/include-str-bare-urls.stderr6
-rw-r--r--tests/rustdoc-ui/lints/bare-urls.stderr102
-rw-r--r--tests/rustdoc-ui/lints/renamed-lint-still-applies.stderr6
5 files changed, 117 insertions, 39 deletions
diff --git a/src/librustdoc/passes/lint/bare_urls.rs b/src/librustdoc/passes/lint/bare_urls.rs
index 8f68f6ff476..a148f046f94 100644
--- a/src/librustdoc/passes/lint/bare_urls.rs
+++ b/src/librustdoc/passes/lint/bare_urls.rs
@@ -19,22 +19,22 @@ pub(super) fn visit_item(cx: &DocContext<'_>, item: &Item) {
     };
     let dox = item.doc_value();
     if !dox.is_empty() {
-        let report_diag =
-            |cx: &DocContext<'_>, msg: &'static str, url: &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")
-                        .span_suggestion(
-                            sp,
-                            "use an automatic link instead",
-                            format!("<{url}>"),
-                            Applicability::MachineApplicable,
-                        );
-                });
-            };
+        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,
+                    );
+            });
+        };
 
         let mut p = Parser::new_ext(&dox, main_body_opts()).into_offset_iter();
 
@@ -74,17 +74,15 @@ fn find_raw_urls(
     cx: &DocContext<'_>,
     text: &str,
     range: Range<usize>,
-    f: &impl Fn(&DocContext<'_>, &'static str, &str, Range<usize>),
+    f: &impl Fn(&DocContext<'_>, &'static str, Range<usize>),
 ) {
     trace!("looking for raw urls in {text}");
     // For now, we only check "full" URLs (meaning, starting with "http://" or "https://").
     for match_ in URL_REGEX.find_iter(text) {
-        let url = match_.as_str();
         let url_range = match_.range();
         f(
             cx,
             "this URL is not a hyperlink",
-            url,
             Range { start: range.start + url_range.start, end: range.start + url_range.end },
         );
     }
diff --git a/tests/rustdoc-ui/diagnostic-width.stderr b/tests/rustdoc-ui/diagnostic-width.stderr
index c1cc4898ac5..d8c4934a576 100644
--- a/tests/rustdoc-ui/diagnostic-width.stderr
+++ b/tests/rustdoc-ui/diagnostic-width.stderr
@@ -2,7 +2,7 @@ error: this URL is not a hyperlink
   --> $DIR/diagnostic-width.rs:4:41
    |
 LL | ... a http://link.com
-   |       ^^^^^^^^^^^^^^^ help: use an automatic link instead: `<http://link.com>`
+   |       ^^^^^^^^^^^^^^^
    |
    = note: bare URLs are not automatically turned into clickable links
 note: the lint level is defined here
@@ -10,6 +10,10 @@ note: the lint level is defined here
    |
 LL | ...ny(rustdoc::bare_url...
    |       ^^^^^^^^^^^^^^^^^^
+help: use an automatic link instead
+   |
+LL | /// This is a long line that contains a <http://link.com>
+   |                                         +               +
 
 error: aborting due to 1 previous error
 
diff --git a/tests/rustdoc-ui/include-str-bare-urls.stderr b/tests/rustdoc-ui/include-str-bare-urls.stderr
index a4234196b23..53da2411874 100644
--- a/tests/rustdoc-ui/include-str-bare-urls.stderr
+++ b/tests/rustdoc-ui/include-str-bare-urls.stderr
@@ -2,7 +2,7 @@ error: this URL is not a hyperlink
   --> $DIR/auxiliary/include-str-bare-urls.md:1:11
    |
 LL | HEADS UP! https://example.com MUST SHOW UP IN THE STDERR FILE!
-   |           ^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://example.com>`
+   |           ^^^^^^^^^^^^^^^^^^^
    |
    = note: bare URLs are not automatically turned into clickable links
 note: the lint level is defined here
@@ -10,6 +10,10 @@ note: the lint level is defined here
    |
 LL | #![deny(rustdoc::bare_urls)]
    |         ^^^^^^^^^^^^^^^^^^
+help: use an automatic link instead
+   |
+LL | HEADS UP! <https://example.com> MUST SHOW UP IN THE STDERR FILE!
+   |           +                   +
 
 error: aborting due to 1 previous error
 
diff --git a/tests/rustdoc-ui/lints/bare-urls.stderr b/tests/rustdoc-ui/lints/bare-urls.stderr
index ccf52cd0b93..ddfc387eaf6 100644
--- a/tests/rustdoc-ui/lints/bare-urls.stderr
+++ b/tests/rustdoc-ui/lints/bare-urls.stderr
@@ -2,7 +2,7 @@ error: this URL is not a hyperlink
   --> $DIR/bare-urls.rs:5:5
    |
 LL | /// https://somewhere.com
-   |     ^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com>`
+   |     ^^^^^^^^^^^^^^^^^^^^^
    |
    = note: bare URLs are not automatically turned into clickable links
 note: the lint level is defined here
@@ -10,134 +10,202 @@ note: the lint level is defined here
    |
 LL | #![deny(rustdoc::bare_urls)]
    |         ^^^^^^^^^^^^^^^^^^
+help: use an automatic link instead
+   |
+LL | /// <https://somewhere.com>
+   |     +                     +
 
 error: this URL is not a hyperlink
   --> $DIR/bare-urls.rs:7:5
    |
 LL | /// https://somewhere.com/a
-   |     ^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com/a>`
+   |     ^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: bare URLs are not automatically turned into clickable links
+help: use an automatic link instead
+   |
+LL | /// <https://somewhere.com/a>
+   |     +                       +
 
 error: this URL is not a hyperlink
   --> $DIR/bare-urls.rs:9:5
    |
 LL | /// https://www.somewhere.com
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://www.somewhere.com>`
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: bare URLs are not automatically turned into clickable links
+help: use an automatic link instead
+   |
+LL | /// <https://www.somewhere.com>
+   |     +                         +
 
 error: this URL is not a hyperlink
   --> $DIR/bare-urls.rs:11:5
    |
 LL | /// https://www.somewhere.com/a
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://www.somewhere.com/a>`
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: bare URLs are not automatically turned into clickable links
+help: use an automatic link instead
+   |
+LL | /// <https://www.somewhere.com/a>
+   |     +                           +
 
 error: this URL is not a hyperlink
   --> $DIR/bare-urls.rs:13:5
    |
 LL | /// https://subdomain.example.com
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://subdomain.example.com>`
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: bare URLs are not automatically turned into clickable links
+help: use an automatic link instead
+   |
+LL | /// <https://subdomain.example.com>
+   |     +                             +
 
 error: this URL is not a hyperlink
   --> $DIR/bare-urls.rs:15:5
    |
 LL | /// https://somewhere.com?
-   |     ^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com?>`
+   |     ^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: bare URLs are not automatically turned into clickable links
+help: use an automatic link instead
+   |
+LL | /// <https://somewhere.com?>
+   |     +                      +
 
 error: this URL is not a hyperlink
   --> $DIR/bare-urls.rs:17:5
    |
 LL | /// https://somewhere.com/a?
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com/a?>`
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: bare URLs are not automatically turned into clickable links
+help: use an automatic link instead
+   |
+LL | /// <https://somewhere.com/a?>
+   |     +                        +
 
 error: this URL is not a hyperlink
   --> $DIR/bare-urls.rs:19:5
    |
 LL | /// https://somewhere.com?hello=12
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com?hello=12>`
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: bare URLs are not automatically turned into clickable links
+help: use an automatic link instead
+   |
+LL | /// <https://somewhere.com?hello=12>
+   |     +                              +
 
 error: this URL is not a hyperlink
   --> $DIR/bare-urls.rs:21:5
    |
 LL | /// https://somewhere.com/a?hello=12
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com/a?hello=12>`
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: bare URLs are not automatically turned into clickable links
+help: use an automatic link instead
+   |
+LL | /// <https://somewhere.com/a?hello=12>
+   |     +                                +
 
 error: this URL is not a hyperlink
   --> $DIR/bare-urls.rs:23:5
    |
 LL | /// https://example.com?hello=12#xyz
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://example.com?hello=12#xyz>`
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: bare URLs are not automatically turned into clickable links
+help: use an automatic link instead
+   |
+LL | /// <https://example.com?hello=12#xyz>
+   |     +                                +
 
 error: this URL is not a hyperlink
   --> $DIR/bare-urls.rs:25:5
    |
 LL | /// https://example.com/a?hello=12#xyz
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://example.com/a?hello=12#xyz>`
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: bare URLs are not automatically turned into clickable links
+help: use an automatic link instead
+   |
+LL | /// <https://example.com/a?hello=12#xyz>
+   |     +                                  +
 
 error: this URL is not a hyperlink
   --> $DIR/bare-urls.rs:27:5
    |
 LL | /// https://example.com#xyz
-   |     ^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://example.com#xyz>`
+   |     ^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: bare URLs are not automatically turned into clickable links
+help: use an automatic link instead
+   |
+LL | /// <https://example.com#xyz>
+   |     +                       +
 
 error: this URL is not a hyperlink
   --> $DIR/bare-urls.rs:29:5
    |
 LL | /// https://example.com/a#xyz
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://example.com/a#xyz>`
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: bare URLs are not automatically turned into clickable links
+help: use an automatic link instead
+   |
+LL | /// <https://example.com/a#xyz>
+   |     +                         +
 
 error: this URL is not a hyperlink
   --> $DIR/bare-urls.rs:31:5
    |
 LL | /// https://somewhere.com?hello=12&bye=11
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com?hello=12&bye=11>`
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: bare URLs are not automatically turned into clickable links
+help: use an automatic link instead
+   |
+LL | /// <https://somewhere.com?hello=12&bye=11>
+   |     +                                     +
 
 error: this URL is not a hyperlink
   --> $DIR/bare-urls.rs:33:5
    |
 LL | /// https://somewhere.com/a?hello=12&bye=11
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com/a?hello=12&bye=11>`
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: bare URLs are not automatically turned into clickable links
+help: use an automatic link instead
+   |
+LL | /// <https://somewhere.com/a?hello=12&bye=11>
+   |     +                                       +
 
 error: this URL is not a hyperlink
   --> $DIR/bare-urls.rs:35:5
    |
 LL | /// https://somewhere.com?hello=12&bye=11#xyz
-   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com?hello=12&bye=11#xyz>`
+   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: bare URLs are not automatically turned into clickable links
+help: use an automatic link instead
+   |
+LL | /// <https://somewhere.com?hello=12&bye=11#xyz>
+   |     +                                         +
 
 error: this URL is not a hyperlink
   --> $DIR/bare-urls.rs:37:10
    |
 LL | /// hey! https://somewhere.com/a?hello=12&bye=11#xyz
-   |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com/a?hello=12&bye=11#xyz>`
+   |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: bare URLs are not automatically turned into clickable links
+help: use an automatic link instead
+   |
+LL | /// hey! <https://somewhere.com/a?hello=12&bye=11#xyz>
+   |          +                                           +
 
 error: aborting due to 17 previous errors
 
diff --git a/tests/rustdoc-ui/lints/renamed-lint-still-applies.stderr b/tests/rustdoc-ui/lints/renamed-lint-still-applies.stderr
index ee9b67cb91b..88807dfb495 100644
--- a/tests/rustdoc-ui/lints/renamed-lint-still-applies.stderr
+++ b/tests/rustdoc-ui/lints/renamed-lint-still-applies.stderr
@@ -29,7 +29,7 @@ error: this URL is not a hyperlink
   --> $DIR/renamed-lint-still-applies.rs:9:5
    |
 LL | //! http://example.com
-   |     ^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<http://example.com>`
+   |     ^^^^^^^^^^^^^^^^^^
    |
    = note: bare URLs are not automatically turned into clickable links
 note: the lint level is defined here
@@ -37,6 +37,10 @@ note: the lint level is defined here
    |
 LL | #![deny(rustdoc::non_autolinks)]
    |         ^^^^^^^^^^^^^^^^^^^^^^
+help: use an automatic link instead
+   |
+LL | //! <http://example.com>
+   |     +                  +
 
 error: aborting due to 2 previous errors; 2 warnings emitted