rustdoc/passes/lint/
bare_urls.rs

1//! Detects links that are not linkified, e.g., in Markdown such as `Go to https://example.com/.`
2//! Suggests wrapping the link with angle brackets: `Go to <https://example.com/>.` to linkify it.
3
4use core::ops::Range;
5use std::mem;
6use std::sync::LazyLock;
7
8use pulldown_cmark::{Event, Parser, Tag};
9use regex::Regex;
10use rustc_errors::Applicability;
11use rustc_hir::HirId;
12use rustc_resolve::rustdoc::source_span_for_markdown_range;
13use tracing::trace;
14
15use crate::clean::*;
16use crate::core::DocContext;
17use crate::html::markdown::main_body_opts;
18
19pub(super) fn visit_item(cx: &DocContext<'_>, item: &Item, hir_id: HirId, dox: &str) {
20    let report_diag = |cx: &DocContext<'_>, msg: &'static str, range: Range<usize>| {
21        let maybe_sp = source_span_for_markdown_range(cx.tcx, dox, &range, &item.attrs.doc_strings)
22            .map(|(sp, _)| sp);
23        let sp = maybe_sp.unwrap_or_else(|| item.attr_span(cx.tcx));
24        cx.tcx.node_span_lint(crate::lint::BARE_URLS, hir_id, sp, |lint| {
25            lint.primary_message(msg)
26                .note("bare URLs are not automatically turned into clickable links");
27            // The fallback of using the attribute span is suitable for
28            // highlighting where the error is, but not for placing the < and >
29            if let Some(sp) = maybe_sp {
30                lint.multipart_suggestion(
31                    "use an automatic link instead",
32                    vec![
33                        (sp.shrink_to_lo(), "<".to_string()),
34                        (sp.shrink_to_hi(), ">".to_string()),
35                    ],
36                    Applicability::MachineApplicable,
37                );
38            }
39        });
40    };
41
42    let mut p = Parser::new_ext(dox, main_body_opts()).into_offset_iter();
43
44    while let Some((event, range)) = p.next() {
45        match event {
46            Event::Text(s) => find_raw_urls(cx, &s, range, &report_diag),
47            // We don't want to check the text inside code blocks or links.
48            Event::Start(tag @ (Tag::CodeBlock(_) | Tag::Link { .. })) => {
49                for (event, _) in p.by_ref() {
50                    match event {
51                        Event::End(end)
52                            if mem::discriminant(&end) == mem::discriminant(&tag.to_end()) =>
53                        {
54                            break;
55                        }
56                        _ => {}
57                    }
58                }
59            }
60            _ => {}
61        }
62    }
63}
64
65static URL_REGEX: LazyLock<Regex> = LazyLock::new(|| {
66    Regex::new(concat!(
67        r"https?://",                          // url scheme
68        r"([-a-zA-Z0-9@:%._\+~#=]{2,256}\.)+", // one or more subdomains
69        r"[a-zA-Z]{2,63}",                     // root domain
70        r"\b([-a-zA-Z0-9@:%_\+.~#?&/=]*)"      // optional query or url fragments
71    ))
72    .expect("failed to build regex")
73});
74
75fn find_raw_urls(
76    cx: &DocContext<'_>,
77    text: &str,
78    range: Range<usize>,
79    f: &impl Fn(&DocContext<'_>, &'static str, Range<usize>),
80) {
81    trace!("looking for raw urls in {text}");
82    // For now, we only check "full" URLs (meaning, starting with "http://" or "https://").
83    for match_ in URL_REGEX.find_iter(text) {
84        let url_range = match_.range();
85        f(
86            cx,
87            "this URL is not a hyperlink",
88            Range { start: range.start + url_range.start, end: range.start + url_range.end },
89        );
90    }
91}