Skip to content

Commit 5a36fe2

Browse files
Improve suggestion in case a bare URL is surrounded by brackets
1 parent be8de5d commit 5a36fe2

File tree

1 file changed

+40
-13
lines changed

1 file changed

+40
-13
lines changed

src/librustdoc/passes/lint/bare_urls.rs

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ use crate::core::DocContext;
1717
use crate::html::markdown::main_body_opts;
1818

1919
pub(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>| {
20+
let report_diag = |cx: &DocContext<'_>,
21+
msg: &'static str,
22+
range: Range<usize>,
23+
without_brackets: Option<&str>| {
2124
let maybe_sp = source_span_for_markdown_range(cx.tcx, dox, &range, &item.attrs.doc_strings)
2225
.map(|(sp, _)| sp);
2326
let sp = maybe_sp.unwrap_or_else(|| item.attr_span(cx.tcx));
@@ -27,14 +30,22 @@ pub(super) fn visit_item(cx: &DocContext<'_>, item: &Item, hir_id: HirId, dox: &
2730
// The fallback of using the attribute span is suitable for
2831
// highlighting where the error is, but not for placing the < and >
2932
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-
);
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+
}
3849
}
3950
});
4051
};
@@ -43,7 +54,7 @@ pub(super) fn visit_item(cx: &DocContext<'_>, item: &Item, hir_id: HirId, dox: &
4354

4455
while let Some((event, range)) = p.next() {
4556
match event {
46-
Event::Text(s) => find_raw_urls(cx, &s, range, &report_diag),
57+
Event::Text(s) => find_raw_urls(cx, dox, &s, range, &report_diag),
4758
// We don't want to check the text inside code blocks or links.
4859
Event::Start(tag @ (Tag::CodeBlock(_) | Tag::Link { .. })) => {
4960
for (event, _) in p.by_ref() {
@@ -67,25 +78,41 @@ static URL_REGEX: LazyLock<Regex> = LazyLock::new(|| {
6778
r"https?://", // url scheme
6879
r"([-a-zA-Z0-9@:%._\+~#=]{2,256}\.)+", // one or more subdomains
6980
r"[a-zA-Z]{2,63}", // root domain
70-
r"\b([-a-zA-Z0-9@:%_\+.~#?&/=]*)" // optional query or url fragments
81+
r"\b([-a-zA-Z0-9@:%_\+.~#?&/=]*)", // optional query or url fragments
7182
))
7283
.expect("failed to build regex")
7384
});
7485

7586
fn find_raw_urls(
7687
cx: &DocContext<'_>,
88+
whole_text: &str,
7789
text: &str,
7890
range: Range<usize>,
79-
f: &impl Fn(&DocContext<'_>, &'static str, Range<usize>),
91+
f: &impl Fn(&DocContext<'_>, &'static str, Range<usize>, Option<&str>),
8092
) {
8193
trace!("looking for raw urls in {text}");
8294
// For now, we only check "full" URLs (meaning, starting with "http://" or "https://").
8395
for match_ in URL_REGEX.find_iter(text) {
8496
let url_range = match_.range();
97+
let mut without_brackets = None;
98+
let mut extra_range = 0;
99+
// If the whole text is contained inside `[]`, then we need to replace the brackets and
100+
// not just add `<>`.
101+
if whole_text[..range.start + url_range.start].ends_with('[')
102+
&& range.start + url_range.end <= whole_text.len()
103+
&& whole_text[range.start + url_range.end..].starts_with(']')
104+
{
105+
extra_range = 1;
106+
without_brackets = Some(match_.as_str());
107+
}
85108
f(
86109
cx,
87110
"this URL is not a hyperlink",
88-
Range { start: range.start + url_range.start, end: range.start + url_range.end },
111+
Range {
112+
start: range.start + url_range.start - extra_range,
113+
end: range.start + url_range.end + extra_range,
114+
},
115+
without_brackets,
89116
);
90117
}
91118
}

0 commit comments

Comments
 (0)