diff --git a/src/librustdoc/passes/url_improvements.rs b/src/librustdoc/passes/url_improvements.rs index d191a89948ae..a5d555eb19dc 100644 --- a/src/librustdoc/passes/url_improvements.rs +++ b/src/librustdoc/passes/url_improvements.rs @@ -90,33 +90,43 @@ impl<'a, 'tcx> DocFolder for UrlImprovementsLinter<'a, 'tcx> { }); }; - let p = Parser::new_ext(&dox, opts()).into_offset_iter(); + let mut p = Parser::new_ext(&dox, opts()).into_offset_iter(); - let mut title = String::new(); - let mut in_link = false; - let mut ignore = false; - - for (event, range) in p { + while let Some((event, range)) = p.next() { match event { Event::Start(Tag::Link(kind, _, _)) => { - in_link = true; - ignore = matches!(kind, LinkType::Autolink | LinkType::Email); - } - Event::End(Tag::Link(_, url, _)) => { - in_link = false; - // NOTE: links cannot be nested, so we don't need to check `kind` - if url.as_ref() == title && !ignore { - report_diag(self.cx, "unneeded long form for URL", &url, range); - } - title.clear(); - ignore = false; - } - Event::Text(s) if in_link => { - if !ignore { - title.push_str(&s); + let ignore = matches!(kind, LinkType::Autolink | LinkType::Email); + let mut title = String::new(); + + while let Some((event, range)) = p.next() { + match event { + Event::End(Tag::Link(_, url, _)) => { + // NOTE: links cannot be nested, so we don't need to check `kind` + if url.as_ref() == title && !ignore { + report_diag( + self.cx, + "unneeded long form for URL", + &url, + range, + ); + } + break; + } + Event::Text(s) if !ignore => title.push_str(&s), + _ => {} + } } } Event::Text(s) => self.find_raw_urls(&s, range, &report_diag), + Event::Start(Tag::CodeBlock(_)) => { + // We don't want to check the text inside the code blocks. + while let Some((event, _)) = p.next() { + match event { + Event::End(Tag::CodeBlock(_)) => break, + _ => {} + } + } + } _ => {} } } diff --git a/src/test/rustdoc-ui/url-improvements.rs b/src/test/rustdoc-ui/url-improvements.rs index 761ec31feca9..81fd0ba7d512 100644 --- a/src/test/rustdoc-ui/url-improvements.rs +++ b/src/test/rustdoc-ui/url-improvements.rs @@ -51,6 +51,10 @@ pub fn c() {} /// [b] /// /// [b]: http://b.com +/// +/// ``` +/// This link should not be linted: http://example.com +/// ``` pub fn everything_is_fine_here() {} #[allow(url_improvements)] diff --git a/src/test/rustdoc-ui/url-improvements.stderr b/src/test/rustdoc-ui/url-improvements.stderr index 7ef287dfd11a..e8ed2331dd84 100644 --- a/src/test/rustdoc-ui/url-improvements.stderr +++ b/src/test/rustdoc-ui/url-improvements.stderr @@ -1,119 +1,119 @@ error: unneeded long form for URL - --> $DIR/automatic-links.rs:3:5 + --> $DIR/url-improvements.rs:3:5 | LL | /// [http://a.com](http://a.com) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `` | note: the lint level is defined here - --> $DIR/automatic-links.rs:1:9 + --> $DIR/url-improvements.rs:1:9 | LL | #![deny(url_improvements)] | ^^^^^^^^^^^^^^^^ error: unneeded long form for URL - --> $DIR/automatic-links.rs:5:5 + --> $DIR/url-improvements.rs:5:5 | LL | /// [http://b.com] | ^^^^^^^^^^^^^^ help: use an automatic link instead: `` error: this URL is not a hyperlink - --> $DIR/automatic-links.rs:13:5 + --> $DIR/url-improvements.rs:13:5 | LL | /// https://somewhere.com | ^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `` error: this URL is not a hyperlink - --> $DIR/automatic-links.rs:15:5 + --> $DIR/url-improvements.rs:15:5 | LL | /// https://somewhere.com/a | ^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `` error: this URL is not a hyperlink - --> $DIR/automatic-links.rs:17:5 + --> $DIR/url-improvements.rs:17:5 | LL | /// https://www.somewhere.com | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `` error: this URL is not a hyperlink - --> $DIR/automatic-links.rs:19:5 + --> $DIR/url-improvements.rs:19:5 | LL | /// https://www.somewhere.com/a | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `` error: this URL is not a hyperlink - --> $DIR/automatic-links.rs:21:5 + --> $DIR/url-improvements.rs:21:5 | LL | /// https://subdomain.example.com | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `` error: this URL is not a hyperlink - --> $DIR/automatic-links.rs:23:5 + --> $DIR/url-improvements.rs:23:5 | LL | /// https://somewhere.com? | ^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `` error: this URL is not a hyperlink - --> $DIR/automatic-links.rs:25:5 + --> $DIR/url-improvements.rs:25:5 | LL | /// https://somewhere.com/a? | ^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `` error: this URL is not a hyperlink - --> $DIR/automatic-links.rs:27:5 + --> $DIR/url-improvements.rs:27:5 | LL | /// https://somewhere.com?hello=12 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `` error: this URL is not a hyperlink - --> $DIR/automatic-links.rs:29:5 + --> $DIR/url-improvements.rs:29:5 | LL | /// https://somewhere.com/a?hello=12 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `` error: this URL is not a hyperlink - --> $DIR/automatic-links.rs:31:5 + --> $DIR/url-improvements.rs:31:5 | LL | /// https://example.com?hello=12#xyz | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `` error: this URL is not a hyperlink - --> $DIR/automatic-links.rs:33:5 + --> $DIR/url-improvements.rs:33:5 | LL | /// https://example.com/a?hello=12#xyz | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `` error: this URL is not a hyperlink - --> $DIR/automatic-links.rs:35:5 + --> $DIR/url-improvements.rs:35:5 | LL | /// https://example.com#xyz | ^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `` error: this URL is not a hyperlink - --> $DIR/automatic-links.rs:37:5 + --> $DIR/url-improvements.rs:37:5 | LL | /// https://example.com/a#xyz | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `` error: this URL is not a hyperlink - --> $DIR/automatic-links.rs:39:5 + --> $DIR/url-improvements.rs:39:5 | LL | /// https://somewhere.com?hello=12&bye=11 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `` error: this URL is not a hyperlink - --> $DIR/automatic-links.rs:41:5 + --> $DIR/url-improvements.rs:41:5 | LL | /// https://somewhere.com/a?hello=12&bye=11 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `` error: this URL is not a hyperlink - --> $DIR/automatic-links.rs:43:5 + --> $DIR/url-improvements.rs:43:5 | LL | /// https://somewhere.com?hello=12&bye=11#xyz | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `` error: this URL is not a hyperlink - --> $DIR/automatic-links.rs:45:10 + --> $DIR/url-improvements.rs:45:10 | LL | /// hey! https://somewhere.com/a?hello=12&bye=11#xyz | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: ``