Merge pull request #1838 from rust-lang-nursery/fix-doc
Don't lint autolinks in `doc_markdown`
This commit is contained in:
commit
ebc9891fc6
6 changed files with 97 additions and 2 deletions
|
|
@ -5,6 +5,7 @@ use syntax::ast;
|
|||
use syntax::codemap::{BytePos, Span};
|
||||
use syntax_pos::Pos;
|
||||
use utils::span_lint;
|
||||
use url::Url;
|
||||
|
||||
/// **What it does:** Checks for the presence of `_`, `::` or camel-case words
|
||||
/// outside ticks in documentation.
|
||||
|
|
@ -195,16 +196,26 @@ fn check_doc<'a, Events: Iterator<Item = (usize, pulldown_cmark::Event<'a>)>>(
|
|||
use pulldown_cmark::Tag::*;
|
||||
|
||||
let mut in_code = false;
|
||||
let mut in_link = None;
|
||||
|
||||
for (offset, event) in docs {
|
||||
match event {
|
||||
Start(CodeBlock(_)) | Start(Code) => in_code = true,
|
||||
End(CodeBlock(_)) | End(Code) => in_code = false,
|
||||
Start(_tag) | End(_tag) => (), // We don't care about other tags
|
||||
Start(Link(link, _)) => in_link = Some(link),
|
||||
End(Link(_, _)) => in_link = None,
|
||||
Start(_tag) | End(_tag) => (), // We don't care about other tags
|
||||
Html(_html) | InlineHtml(_html) => (), // HTML is weird, just ignore it
|
||||
SoftBreak => (),
|
||||
HardBreak => (),
|
||||
FootnoteReference(text) | Text(text) => {
|
||||
if Some(&text) == in_link.as_ref() {
|
||||
// Probably a link of the form `<http://example.com>`
|
||||
// Which are represented as a link to "http://example.com" with
|
||||
// text "http://example.com" by pulldown-cmark
|
||||
continue;
|
||||
}
|
||||
|
||||
if !in_code {
|
||||
let index = match spans.binary_search_by(|c| c.0.cmp(&offset)) {
|
||||
Ok(o) => o,
|
||||
|
|
@ -270,6 +281,18 @@ fn check_word(cx: &EarlyContext, word: &str, span: Span) {
|
|||
s != "_" && !s.contains("\\_") && s.contains('_')
|
||||
}
|
||||
|
||||
if let Ok(url) = Url::parse(word) {
|
||||
// try to get around the fact that `foo::bar` parses as a valid URL
|
||||
if !url.cannot_be_a_base() {
|
||||
span_lint(cx,
|
||||
DOC_MARKDOWN,
|
||||
span,
|
||||
"you should put bare URLs between `<`/`>` or make a proper Markdown link");
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if has_underscore(word) || word.contains("::") || is_camel_case(word) {
|
||||
span_lint(
|
||||
cx,
|
||||
|
|
|
|||
|
|
@ -51,6 +51,7 @@ extern crate lazy_static;
|
|||
|
||||
extern crate itertools;
|
||||
extern crate pulldown_cmark;
|
||||
extern crate url;
|
||||
|
||||
macro_rules! declare_restriction_lint {
|
||||
{ pub $name:tt, $description:tt } => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue