Skip to content

Commit 6f89468

Browse files
committed
Remove 'unnecessary long for for link' warning
This also makes the implementation slightly more efficient by only compiling the regex once. See #81764 (comment) for why this was removed; essentially the benefit didn't seem great enough to deserve a lint.
1 parent fe1baa6 commit 6f89468

File tree

3 files changed

+44
-80
lines changed

3 files changed

+44
-80
lines changed

src/librustdoc/passes/non_autolinks.rs

+22-38
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,30 @@ use crate::core::DocContext;
44
use crate::fold::DocFolder;
55
use crate::html::markdown::opts;
66
use core::ops::Range;
7-
use pulldown_cmark::{Event, LinkType, Parser, Tag};
7+
use pulldown_cmark::{Event, Parser, Tag};
88
use regex::Regex;
99
use rustc_errors::Applicability;
10+
use std::lazy::SyncLazy;
11+
use std::mem;
1012

1113
crate const CHECK_NON_AUTOLINKS: Pass = Pass {
1214
name: "check-non-autolinks",
1315
run: check_non_autolinks,
1416
description: "detects URLs that could be linkified",
1517
};
1618

17-
const URL_REGEX: &str = concat!(
18-
r"https?://", // url scheme
19-
r"([-a-zA-Z0-9@:%._\+~#=]{2,256}\.)+", // one or more subdomains
20-
r"[a-zA-Z]{2,63}", // root domain
21-
r"\b([-a-zA-Z0-9@:%_\+.~#?&/=]*)" // optional query or url fragments
22-
);
19+
const URL_REGEX: SyncLazy<Regex> = SyncLazy::new(|| {
20+
Regex::new(concat!(
21+
r"https?://", // url scheme
22+
r"([-a-zA-Z0-9@:%._\+~#=]{2,256}\.)+", // one or more subdomains
23+
r"[a-zA-Z]{2,63}", // root domain
24+
r"\b([-a-zA-Z0-9@:%_\+.~#?&/=]*)" // optional query or url fragments
25+
))
26+
.expect("failed to build regex")
27+
});
2328

2429
struct NonAutolinksLinter<'a, 'tcx> {
2530
cx: &'a mut DocContext<'tcx>,
26-
regex: Regex,
2731
}
2832

2933
impl<'a, 'tcx> NonAutolinksLinter<'a, 'tcx> {
@@ -33,8 +37,9 @@ impl<'a, 'tcx> NonAutolinksLinter<'a, 'tcx> {
3337
range: Range<usize>,
3438
f: &impl Fn(&DocContext<'_>, &str, &str, Range<usize>),
3539
) {
40+
trace!("looking for raw urls in {}", text);
3641
// For now, we only check "full" URLs (meaning, starting with "http://" or "https://").
37-
for match_ in self.regex.find_iter(&text) {
42+
for match_ in URL_REGEX.find_iter(&text) {
3843
let url = match_.as_str();
3944
let url_range = match_.range();
4045
f(
@@ -48,7 +53,7 @@ impl<'a, 'tcx> NonAutolinksLinter<'a, 'tcx> {
4853
}
4954

5055
crate fn check_non_autolinks(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
51-
NonAutolinksLinter::new(cx).fold_crate(krate)
56+
NonAutolinksLinter { cx }.fold_crate(krate)
5257
}
5358

5459
impl<'a, 'tcx> DocFolder for NonAutolinksLinter<'a, 'tcx> {
@@ -82,37 +87,16 @@ impl<'a, 'tcx> DocFolder for NonAutolinksLinter<'a, 'tcx> {
8287

8388
while let Some((event, range)) = p.next() {
8489
match event {
85-
Event::Start(Tag::Link(kind, _, _)) => {
86-
let ignore = matches!(kind, LinkType::Autolink | LinkType::Email);
87-
let mut title = String::new();
88-
89-
while let Some((event, range)) = p.next() {
90-
match event {
91-
Event::End(Tag::Link(_, url, _)) => {
92-
// NOTE: links cannot be nested, so we don't need to
93-
// check `kind`
94-
if url.as_ref() == title && !ignore && self.regex.is_match(&url)
95-
{
96-
report_diag(
97-
self.cx,
98-
"unneeded long form for URL",
99-
&url,
100-
range,
101-
);
102-
}
103-
break;
104-
}
105-
Event::Text(s) if !ignore => title.push_str(&s),
106-
_ => {}
107-
}
108-
}
109-
}
11090
Event::Text(s) => self.find_raw_urls(&s, range, &report_diag),
111-
Event::Start(Tag::CodeBlock(_)) => {
112-
// We don't want to check the text inside the code blocks.
91+
// We don't want to check the text inside code blocks or links.
92+
Event::Start(tag @ (Tag::CodeBlock(_) | Tag::Link(..))) => {
11393
while let Some((event, _)) = p.next() {
11494
match event {
115-
Event::End(Tag::CodeBlock(_)) => break,
95+
Event::End(end)
96+
if mem::discriminant(&end) == mem::discriminant(&tag) =>
97+
{
98+
break;
99+
}
116100
_ => {}
117101
}
118102
}

src/test/rustdoc-ui/url-improvements.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
#![deny(rustdoc::non_autolinks)]
22

3-
/// [http://aa.com](http://aa.com)
4-
//~^ ERROR unneeded long form for URL
5-
/// [http://bb.com]
6-
//~^ ERROR unneeded long form for URL
7-
///
8-
/// [http://bb.com]: http://bb.com
9-
///
10-
/// [http://c.com][http://c.com]
11-
pub fn a() {}
12-
133
/// https://somewhere.com
144
//~^ ERROR this URL is not a hyperlink
155
/// https://somewhere.com/a
@@ -54,6 +44,8 @@ pub fn c() {}
5444
///
5545
/// ```
5646
/// This link should not be linted: http://example.com
47+
///
48+
/// Nor this one: <http://example.com> or this one: [x](http://example.com)
5749
/// ```
5850
///
5951
/// [should_not.lint](should_not.lint)
+20-32
Original file line numberDiff line numberDiff line change
@@ -1,122 +1,110 @@
1-
error: unneeded long form for URL
1+
error: this URL is not a hyperlink
22
--> $DIR/url-improvements.rs:3:5
33
|
4-
LL | /// [http://aa.com](http://aa.com)
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<http://aa.com>`
4+
LL | /// https://somewhere.com
5+
| ^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com>`
66
|
77
note: the lint level is defined here
88
--> $DIR/url-improvements.rs:1:9
99
|
1010
LL | #![deny(rustdoc::non_autolinks)]
1111
| ^^^^^^^^^^^^^^^^^^^^^^
1212

13-
error: unneeded long form for URL
14-
--> $DIR/url-improvements.rs:5:5
15-
|
16-
LL | /// [http://bb.com]
17-
| ^^^^^^^^^^^^^^^ help: use an automatic link instead: `<http://bb.com>`
18-
1913
error: this URL is not a hyperlink
20-
--> $DIR/url-improvements.rs:13:5
21-
|
22-
LL | /// https://somewhere.com
23-
| ^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com>`
24-
25-
error: this URL is not a hyperlink
26-
--> $DIR/url-improvements.rs:15:5
14+
--> $DIR/url-improvements.rs:5:5
2715
|
2816
LL | /// https://somewhere.com/a
2917
| ^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com/a>`
3018

3119
error: this URL is not a hyperlink
32-
--> $DIR/url-improvements.rs:17:5
20+
--> $DIR/url-improvements.rs:7:5
3321
|
3422
LL | /// https://www.somewhere.com
3523
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://www.somewhere.com>`
3624

3725
error: this URL is not a hyperlink
38-
--> $DIR/url-improvements.rs:19:5
26+
--> $DIR/url-improvements.rs:9:5
3927
|
4028
LL | /// https://www.somewhere.com/a
4129
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://www.somewhere.com/a>`
4230

4331
error: this URL is not a hyperlink
44-
--> $DIR/url-improvements.rs:21:5
32+
--> $DIR/url-improvements.rs:11:5
4533
|
4634
LL | /// https://subdomain.example.com
4735
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://subdomain.example.com>`
4836

4937
error: this URL is not a hyperlink
50-
--> $DIR/url-improvements.rs:23:5
38+
--> $DIR/url-improvements.rs:13:5
5139
|
5240
LL | /// https://somewhere.com?
5341
| ^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com?>`
5442

5543
error: this URL is not a hyperlink
56-
--> $DIR/url-improvements.rs:25:5
44+
--> $DIR/url-improvements.rs:15:5
5745
|
5846
LL | /// https://somewhere.com/a?
5947
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com/a?>`
6048

6149
error: this URL is not a hyperlink
62-
--> $DIR/url-improvements.rs:27:5
50+
--> $DIR/url-improvements.rs:17:5
6351
|
6452
LL | /// https://somewhere.com?hello=12
6553
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com?hello=12>`
6654

6755
error: this URL is not a hyperlink
68-
--> $DIR/url-improvements.rs:29:5
56+
--> $DIR/url-improvements.rs:19:5
6957
|
7058
LL | /// https://somewhere.com/a?hello=12
7159
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com/a?hello=12>`
7260

7361
error: this URL is not a hyperlink
74-
--> $DIR/url-improvements.rs:31:5
62+
--> $DIR/url-improvements.rs:21:5
7563
|
7664
LL | /// https://example.com?hello=12#xyz
7765
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://example.com?hello=12#xyz>`
7866

7967
error: this URL is not a hyperlink
80-
--> $DIR/url-improvements.rs:33:5
68+
--> $DIR/url-improvements.rs:23:5
8169
|
8270
LL | /// https://example.com/a?hello=12#xyz
8371
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://example.com/a?hello=12#xyz>`
8472

8573
error: this URL is not a hyperlink
86-
--> $DIR/url-improvements.rs:35:5
74+
--> $DIR/url-improvements.rs:25:5
8775
|
8876
LL | /// https://example.com#xyz
8977
| ^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://example.com#xyz>`
9078

9179
error: this URL is not a hyperlink
92-
--> $DIR/url-improvements.rs:37:5
80+
--> $DIR/url-improvements.rs:27:5
9381
|
9482
LL | /// https://example.com/a#xyz
9583
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://example.com/a#xyz>`
9684

9785
error: this URL is not a hyperlink
98-
--> $DIR/url-improvements.rs:39:5
86+
--> $DIR/url-improvements.rs:29:5
9987
|
10088
LL | /// https://somewhere.com?hello=12&bye=11
10189
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com?hello=12&bye=11>`
10290

10391
error: this URL is not a hyperlink
104-
--> $DIR/url-improvements.rs:41:5
92+
--> $DIR/url-improvements.rs:31:5
10593
|
10694
LL | /// https://somewhere.com/a?hello=12&bye=11
10795
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com/a?hello=12&bye=11>`
10896

10997
error: this URL is not a hyperlink
110-
--> $DIR/url-improvements.rs:43:5
98+
--> $DIR/url-improvements.rs:33:5
11199
|
112100
LL | /// https://somewhere.com?hello=12&bye=11#xyz
113101
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com?hello=12&bye=11#xyz>`
114102

115103
error: this URL is not a hyperlink
116-
--> $DIR/url-improvements.rs:45:10
104+
--> $DIR/url-improvements.rs:35:10
117105
|
118106
LL | /// hey! https://somewhere.com/a?hello=12&bye=11#xyz
119107
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com/a?hello=12&bye=11#xyz>`
120108

121-
error: aborting due to 19 previous errors
109+
error: aborting due to 17 previous errors
122110

0 commit comments

Comments
 (0)