rustdoc/passes/lint/
bare_urls.rs1use 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<'_>,
21 msg: &'static str,
22 range: Range<usize>,
23 without_brackets: Option<&str>| {
24 let maybe_sp = source_span_for_markdown_range(cx.tcx, dox, &range, &item.attrs.doc_strings)
25 .map(|(sp, _)| sp);
26 let sp = maybe_sp.unwrap_or_else(|| item.attr_span(cx.tcx));
27 cx.tcx.node_span_lint(crate::lint::BARE_URLS, hir_id, sp, |lint| {
28 lint.primary_message(msg)
29 .note("bare URLs are not automatically turned into clickable links");
30 if let Some(sp) = maybe_sp {
33 if let Some(without_brackets) = without_brackets {
34 lint.multipart_suggestion(
35 "use an automatic link instead",
36 vec![(sp, format!("<{without_brackets}>"))],
37 Applicability::MachineApplicable,
38 );
39 } else {
40 lint.multipart_suggestion(
41 "use an automatic link instead",
42 vec![
43 (sp.shrink_to_lo(), "<".to_string()),
44 (sp.shrink_to_hi(), ">".to_string()),
45 ],
46 Applicability::MachineApplicable,
47 );
48 }
49 }
50 });
51 };
52
53 let mut p = Parser::new_ext(dox, main_body_opts()).into_offset_iter();
54
55 while let Some((event, range)) = p.next() {
56 match event {
57 Event::Text(s) => find_raw_urls(cx, dox, &s, range, &report_diag),
58 Event::Start(tag @ (Tag::CodeBlock(_) | Tag::Link { .. })) => {
60 for (event, _) in p.by_ref() {
61 match event {
62 Event::End(end)
63 if mem::discriminant(&end) == mem::discriminant(&tag.to_end()) =>
64 {
65 break;
66 }
67 _ => {}
68 }
69 }
70 }
71 _ => {}
72 }
73 }
74}
75
76static URL_REGEX: LazyLock<Regex> = LazyLock::new(|| {
77 Regex::new(concat!(
78 r"https?://", r"([-a-zA-Z0-9@:%._\+~#=]{2,256}\.)+", r"[a-zA-Z]{2,63}", r"\b([-a-zA-Z0-9@:%_\+.~#?&/=]*)", ))
83 .expect("failed to build regex")
84});
85
86fn find_raw_urls(
87 cx: &DocContext<'_>,
88 dox: &str,
89 text: &str,
90 range: Range<usize>,
91 f: &impl Fn(&DocContext<'_>, &'static str, Range<usize>, Option<&str>),
92) {
93 trace!("looking for raw urls in {text}");
94 for match_ in URL_REGEX.find_iter(text) {
96 let mut url_range = match_.range();
97 url_range.start += range.start;
98 url_range.end += range.start;
99 let mut without_brackets = None;
100 if dox[..url_range.start].ends_with('[')
103 && url_range.end <= dox.len()
104 && dox[url_range.end..].starts_with(']')
105 {
106 url_range.start -= 1;
107 url_range.end += 1;
108 without_brackets = Some(match_.as_str());
109 }
110 f(cx, "this URL is not a hyperlink", url_range, without_brackets);
111 }
112}