about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2020-10-17 23:58:46 +0200
committerGuillaume Gomez <guillaume1.gomez@gmail.com>2020-11-05 10:22:08 +0100
commit1fb404bebe9e7d228859a26b7e40340438d0c427 (patch)
tree2e28133acdab3396d3b6de78cfd455f2ab863f7a
parentfce2be0ea7b90fb9ba1a1704454b36307c9b8c07 (diff)
downloadrust-1fb404bebe9e7d228859a26b7e40340438d0c427.tar.gz
rust-1fb404bebe9e7d228859a26b7e40340438d0c427.zip
Don't check for URLs inside codeblocks
-rw-r--r--src/librustdoc/passes/url_improvements.rs50
-rw-r--r--src/test/rustdoc-ui/url-improvements.rs4
-rw-r--r--src/test/rustdoc-ui/url-improvements.stderr40
3 files changed, 54 insertions, 40 deletions
diff --git a/src/librustdoc/passes/url_improvements.rs b/src/librustdoc/passes/url_improvements.rs
index d191a89948a..a5d555eb19d 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);
+                        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),
+                                _ => {}
+                            }
                         }
-                        title.clear();
-                        ignore = false;
                     }
-                    Event::Text(s) if in_link => {
-                        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,
+                                _ => {}
+                            }
                         }
                     }
-                    Event::Text(s) => self.find_raw_urls(&s, range, &report_diag),
                     _ => {}
                 }
             }
diff --git a/src/test/rustdoc-ui/url-improvements.rs b/src/test/rustdoc-ui/url-improvements.rs
index 761ec31feca..81fd0ba7d51 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 7ef287dfd11..e8ed2331dd8 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: `<http://a.com>`
    |
 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: `<http://b.com>`
 
 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: `<https://somewhere.com>`
 
 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: `<https://somewhere.com/a>`
 
 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: `<https://www.somewhere.com>`
 
 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: `<https://www.somewhere.com/a>`
 
 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: `<https://subdomain.example.com>`
 
 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: `<https://somewhere.com?>`
 
 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: `<https://somewhere.com/a?>`
 
 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: `<https://somewhere.com?hello=12>`
 
 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: `<https://somewhere.com/a?hello=12>`
 
 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: `<https://example.com?hello=12#xyz>`
 
 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: `<https://example.com/a?hello=12#xyz>`
 
 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: `<https://example.com#xyz>`
 
 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: `<https://example.com/a#xyz>`
 
 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: `<https://somewhere.com?hello=12&bye=11>`
 
 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: `<https://somewhere.com/a?hello=12&bye=11>`
 
 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: `<https://somewhere.com?hello=12&bye=11#xyz>`
 
 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: `<https://somewhere.com/a?hello=12&bye=11#xyz>`