Don't check for URLs inside codeblocks

This commit is contained in:
Guillaume Gomez 2020-10-17 23:58:46 +02:00
parent fce2be0ea7
commit 1fb404bebe
3 changed files with 55 additions and 41 deletions

View File

@ -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,
_ => {}
}
}
}
_ => {}
}
}

View File

@ -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)]

View File

@ -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>`