Skip to content

Commit 1fb404b

Browse files
Don't check for URLs inside codeblocks
1 parent fce2be0 commit 1fb404b

File tree

3 files changed

+54
-40
lines changed

3 files changed

+54
-40
lines changed

src/librustdoc/passes/url_improvements.rs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -90,33 +90,43 @@ impl<'a, 'tcx> DocFolder for UrlImprovementsLinter<'a, 'tcx> {
9090
});
9191
};
9292

93-
let p = Parser::new_ext(&dox, opts()).into_offset_iter();
93+
let mut p = Parser::new_ext(&dox, opts()).into_offset_iter();
9494

95-
let mut title = String::new();
96-
let mut in_link = false;
97-
let mut ignore = false;
98-
99-
for (event, range) in p {
95+
while let Some((event, range)) = p.next() {
10096
match event {
10197
Event::Start(Tag::Link(kind, _, _)) => {
102-
in_link = true;
103-
ignore = matches!(kind, LinkType::Autolink | LinkType::Email);
104-
}
105-
Event::End(Tag::Link(_, url, _)) => {
106-
in_link = false;
107-
// NOTE: links cannot be nested, so we don't need to check `kind`
108-
if url.as_ref() == title && !ignore {
109-
report_diag(self.cx, "unneeded long form for URL", &url, range);
98+
let ignore = matches!(kind, LinkType::Autolink | LinkType::Email);
99+
let mut title = String::new();
100+
101+
while let Some((event, range)) = p.next() {
102+
match event {
103+
Event::End(Tag::Link(_, url, _)) => {
104+
// NOTE: links cannot be nested, so we don't need to check `kind`
105+
if url.as_ref() == title && !ignore {
106+
report_diag(
107+
self.cx,
108+
"unneeded long form for URL",
109+
&url,
110+
range,
111+
);
112+
}
113+
break;
114+
}
115+
Event::Text(s) if !ignore => title.push_str(&s),
116+
_ => {}
117+
}
110118
}
111-
title.clear();
112-
ignore = false;
113119
}
114-
Event::Text(s) if in_link => {
115-
if !ignore {
116-
title.push_str(&s);
120+
Event::Text(s) => self.find_raw_urls(&s, range, &report_diag),
121+
Event::Start(Tag::CodeBlock(_)) => {
122+
// We don't want to check the text inside the code blocks.
123+
while let Some((event, _)) = p.next() {
124+
match event {
125+
Event::End(Tag::CodeBlock(_)) => break,
126+
_ => {}
127+
}
117128
}
118129
}
119-
Event::Text(s) => self.find_raw_urls(&s, range, &report_diag),
120130
_ => {}
121131
}
122132
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ pub fn c() {}
5151
/// [b]
5252
///
5353
/// [b]: http://b.com
54+
///
55+
/// ```
56+
/// This link should not be linted: http://example.com
57+
/// ```
5458
pub fn everything_is_fine_here() {}
5559

5660
#[allow(url_improvements)]

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,119 @@
11
error: unneeded long form for URL
2-
--> $DIR/automatic-links.rs:3:5
2+
--> $DIR/url-improvements.rs:3:5
33
|
44
LL | /// [http://a.com](http://a.com)
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<http://a.com>`
66
|
77
note: the lint level is defined here
8-
--> $DIR/automatic-links.rs:1:9
8+
--> $DIR/url-improvements.rs:1:9
99
|
1010
LL | #![deny(url_improvements)]
1111
| ^^^^^^^^^^^^^^^^
1212

1313
error: unneeded long form for URL
14-
--> $DIR/automatic-links.rs:5:5
14+
--> $DIR/url-improvements.rs:5:5
1515
|
1616
LL | /// [http://b.com]
1717
| ^^^^^^^^^^^^^^ help: use an automatic link instead: `<http://b.com>`
1818

1919
error: this URL is not a hyperlink
20-
--> $DIR/automatic-links.rs:13:5
20+
--> $DIR/url-improvements.rs:13:5
2121
|
2222
LL | /// https://somewhere.com
2323
| ^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com>`
2424

2525
error: this URL is not a hyperlink
26-
--> $DIR/automatic-links.rs:15:5
26+
--> $DIR/url-improvements.rs:15:5
2727
|
2828
LL | /// https://somewhere.com/a
2929
| ^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com/a>`
3030

3131
error: this URL is not a hyperlink
32-
--> $DIR/automatic-links.rs:17:5
32+
--> $DIR/url-improvements.rs:17:5
3333
|
3434
LL | /// https://www.somewhere.com
3535
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://www.somewhere.com>`
3636

3737
error: this URL is not a hyperlink
38-
--> $DIR/automatic-links.rs:19:5
38+
--> $DIR/url-improvements.rs:19:5
3939
|
4040
LL | /// https://www.somewhere.com/a
4141
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://www.somewhere.com/a>`
4242

4343
error: this URL is not a hyperlink
44-
--> $DIR/automatic-links.rs:21:5
44+
--> $DIR/url-improvements.rs:21:5
4545
|
4646
LL | /// https://subdomain.example.com
4747
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://subdomain.example.com>`
4848

4949
error: this URL is not a hyperlink
50-
--> $DIR/automatic-links.rs:23:5
50+
--> $DIR/url-improvements.rs:23:5
5151
|
5252
LL | /// https://somewhere.com?
5353
| ^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com?>`
5454

5555
error: this URL is not a hyperlink
56-
--> $DIR/automatic-links.rs:25:5
56+
--> $DIR/url-improvements.rs:25:5
5757
|
5858
LL | /// https://somewhere.com/a?
5959
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com/a?>`
6060

6161
error: this URL is not a hyperlink
62-
--> $DIR/automatic-links.rs:27:5
62+
--> $DIR/url-improvements.rs:27:5
6363
|
6464
LL | /// https://somewhere.com?hello=12
6565
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com?hello=12>`
6666

6767
error: this URL is not a hyperlink
68-
--> $DIR/automatic-links.rs:29:5
68+
--> $DIR/url-improvements.rs:29:5
6969
|
7070
LL | /// https://somewhere.com/a?hello=12
7171
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com/a?hello=12>`
7272

7373
error: this URL is not a hyperlink
74-
--> $DIR/automatic-links.rs:31:5
74+
--> $DIR/url-improvements.rs:31:5
7575
|
7676
LL | /// https://example.com?hello=12#xyz
7777
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://example.com?hello=12#xyz>`
7878

7979
error: this URL is not a hyperlink
80-
--> $DIR/automatic-links.rs:33:5
80+
--> $DIR/url-improvements.rs:33:5
8181
|
8282
LL | /// https://example.com/a?hello=12#xyz
8383
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://example.com/a?hello=12#xyz>`
8484

8585
error: this URL is not a hyperlink
86-
--> $DIR/automatic-links.rs:35:5
86+
--> $DIR/url-improvements.rs:35:5
8787
|
8888
LL | /// https://example.com#xyz
8989
| ^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://example.com#xyz>`
9090

9191
error: this URL is not a hyperlink
92-
--> $DIR/automatic-links.rs:37:5
92+
--> $DIR/url-improvements.rs:37:5
9393
|
9494
LL | /// https://example.com/a#xyz
9595
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://example.com/a#xyz>`
9696

9797
error: this URL is not a hyperlink
98-
--> $DIR/automatic-links.rs:39:5
98+
--> $DIR/url-improvements.rs:39:5
9999
|
100100
LL | /// https://somewhere.com?hello=12&bye=11
101101
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com?hello=12&bye=11>`
102102

103103
error: this URL is not a hyperlink
104-
--> $DIR/automatic-links.rs:41:5
104+
--> $DIR/url-improvements.rs:41:5
105105
|
106106
LL | /// https://somewhere.com/a?hello=12&bye=11
107107
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com/a?hello=12&bye=11>`
108108

109109
error: this URL is not a hyperlink
110-
--> $DIR/automatic-links.rs:43:5
110+
--> $DIR/url-improvements.rs:43:5
111111
|
112112
LL | /// https://somewhere.com?hello=12&bye=11#xyz
113113
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com?hello=12&bye=11#xyz>`
114114

115115
error: this URL is not a hyperlink
116-
--> $DIR/automatic-links.rs:45:10
116+
--> $DIR/url-improvements.rs:45:10
117117
|
118118
LL | /// hey! https://somewhere.com/a?hello=12&bye=11#xyz
119119
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com/a?hello=12&bye=11#xyz>`

0 commit comments

Comments
 (0)