Fix spans in all cases in doc_markdown

This commit is contained in:
mcarton 2017-05-30 19:28:44 +02:00
parent aaf9bce905
commit 349b45bb99
2 changed files with 86 additions and 111 deletions

View File

@ -4,7 +4,7 @@ use rustc::lint::*;
use syntax::ast;
use syntax::codemap::{Span, BytePos};
use syntax_pos::Pos;
use utils::{span_lint, snippet_opt};
use utils::span_lint;
/// **What it does:** Checks for the presence of `_`, `::` or camel-case words
/// outside ticks in documentation.
@ -81,7 +81,7 @@ impl<'a> Iterator for Parser<'a> {
/// `syntax::parse::lexer::comments::strip_doc_comment_decoration` because we need to keep track of
/// the spans but this function is inspired from the later.
#[allow(cast_possible_truncation)]
pub fn strip_doc_comment_decoration(comment: String, span: Span) -> (String, Vec<(usize, Span)>) {
pub fn strip_doc_comment_decoration(comment: &str, span: Span) -> (String, Vec<(usize, Span)>) {
// one-line comments lose their prefix
const ONELINERS: &'static [&'static str] = &["///!", "///", "//!", "//"];
for prefix in ONELINERS {
@ -104,7 +104,8 @@ pub fn strip_doc_comment_decoration(comment: String, span: Span) -> (String, Vec
let offset = line.as_ptr() as usize - comment.as_ptr() as usize;
debug_assert_eq!(offset as u32 as usize, offset);
sizes.push((line.len(), Span { lo: span.lo + BytePos(offset as u32), ..span }));
// +1 for the newline
sizes.push((line.len()+1, Span { lo: span.lo + BytePos(offset as u32), ..span }));
}
return (doc.to_string(), sizes);
@ -121,7 +122,7 @@ pub fn check_attrs<'a>(cx: &EarlyContext, valid_idents: &[String], attrs: &'a [a
if attr.is_sugared_doc {
if let Some(ref current) = attr.value_str() {
let current = current.to_string();
let (current, current_spans) = strip_doc_comment_decoration(current, attr.span);
let (current, current_spans) = strip_doc_comment_decoration(&current, attr.span);
spans.extend_from_slice(&current_spans);
doc.push_str(&current);
}
@ -144,7 +145,11 @@ pub fn check_attrs<'a>(cx: &EarlyContext, valid_idents: &[String], attrs: &'a [a
let y_offset = y.0;
match (x.1, y.1) {
(Text(x), Text(y)) => Ok((x_offset, Text((x.into_owned() + &y).into()))),
(Text(x), Text(y)) => {
let mut x = x.into_owned();
x.push_str(&y);
Ok((x_offset, Text(x.into())))
}
(x, y) => Err(((x_offset, x), (y_offset, y))),
}
});
@ -163,18 +168,15 @@ fn check_doc<'a, Events: Iterator<Item=(usize, pulldown_cmark::Event<'a>)>>(
let mut in_code = false;
println!("{:?}", spans);
for (offset, event) in docs {
println!("{:?}, {:?}", offset, event);
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
Html(_html) | InlineHtml(_html) => (), // HTML is weird, just ignore it
FootnoteReference(footnote) => (), // TODO
SoftBreak => (),
HardBreak => (),
Text(text) => {
FootnoteReference(text) | Text(text) => {
if !in_code {
let index = match spans.binary_search_by(|c| c.0.cmp(&offset)) {
Ok(o) => o,
@ -183,15 +185,12 @@ fn check_doc<'a, Events: Iterator<Item=(usize, pulldown_cmark::Event<'a>)>>(
let (begin, span) = spans[index];
println!("raw: {:?}, {}, {}, {:?}", snippet_opt(cx, span), offset, begin, span);
// Adjust for the begining of the current `Event`
let span = Span {
lo: span.lo + BytePos::from_usize(offset - begin),
..span
};
println!("adjusted: {:?}", snippet_opt(cx, span));
check_text(cx, valid_idents, &text, span);
}
},

View File

@ -30,19 +30,19 @@ error: you should put `Foo::some_fun` between ticks in the documentation
|
= note: `-D doc-markdown` implied by `-D warnings`
error: you should put `is::a::global:path` between ticks in the documentation
--> doc.rs:11:13
error: you should put `a::global:path` between ticks in the documentation
--> doc.rs:11:15
|
11 | /// Here be ::is::a::global:path.
| ^^^^^^^^^^^^^^^^^^^^
11 | /// Here be ::a::global:path.
| ^^^^^^^^^^^^^^
|
= note: `-D doc-markdown` implied by `-D warnings`
error: you should put `NotInCodeBlock` between ticks in the documentation
--> doc.rs:12:21
--> doc.rs:12:22
|
12 | /// That's not code ~NotInCodeBlock~.
| ^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^
|
= note: `-D doc-markdown` implied by `-D warnings`
@ -78,154 +78,130 @@ error: you should put `be_sure_we_got_to_the_end_of_it` between ticks in the doc
|
= note: `-D doc-markdown` implied by `-D warnings`
error: you should put `ß_foo` between ticks in the documentation
--> doc.rs:57:5
|
57 | /// ß_foo
| ^^^^^
|
= note: `-D doc-markdown` implied by `-D warnings`
error: you should put `_foo` between ticks in the documentation
--> doc.rs:58:5
|
58 | /// _foo
| ^^^^^
|
= note: `-D doc-markdown` implied by `-D warnings`
error: you should put `foo_ß` between ticks in the documentation
--> doc.rs:61:5
|
61 | /// foo_ß
| ^^^^^
|
= note: `-D doc-markdown` implied by `-D warnings`
error: you should put `foo_` between ticks in the documentation
--> doc.rs:62:5
|
62 | /// foo_
| ^^^^^
|
= note: `-D doc-markdown` implied by `-D warnings`
error: you should put `be_sure_we_got_to_the_end_of_it` between ticks in the documentation
--> doc.rs:77:5
|
77 | /// be_sure_we_got_to_the_end_of_it
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D doc-markdown` implied by `-D warnings`
error: you should put `link_with_underscores` between ticks in the documentation
--> doc.rs:81:22
--> doc.rs:52:22
|
81 | /// This test has [a link_with_underscores][chunked-example] inside it. See #823.
52 | /// This test has [a link_with_underscores][chunked-example] inside it. See #823.
| ^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D doc-markdown` implied by `-D warnings`
error: you should put `inline_link2` between ticks in the documentation
--> doc.rs:84:21
--> doc.rs:55:21
|
84 | /// It can also be [inline_link2].
55 | /// It can also be [inline_link2].
| ^^^^^^^^^^^^
|
= note: `-D doc-markdown` implied by `-D warnings`
error: you should put `be_sure_we_got_to_the_end_of_it` between ticks in the documentation
--> doc.rs:94:5
--> doc.rs:65:5
|
94 | /// be_sure_we_got_to_the_end_of_it
65 | /// be_sure_we_got_to_the_end_of_it
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D doc-markdown` implied by `-D warnings`
error: you should put `CamelCaseThing` between ticks in the documentation
--> doc.rs:107:22
--> doc.rs:73:8
|
73 | /// ## CamelCaseThing
| ^^^^^^^^^^^^^^
|
= note: `-D doc-markdown` implied by `-D warnings`
error: you should put `CamelCaseThing` between ticks in the documentation
--> doc.rs:76:7
|
76 | /// # CamelCaseThing
| ^^^^^^^^^^^^^^
|
= note: `-D doc-markdown` implied by `-D warnings`
error: you should put `CamelCaseThing` between ticks in the documentation
--> doc.rs:78:22
|
78 | /// Not a title #897 CamelCaseThing
| ^^^^^^^^^^^^^^
|
= note: `-D doc-markdown` implied by `-D warnings`
error: you should put `be_sure_we_got_to_the_end_of_it` between ticks in the documentation
--> doc.rs:79:5
|
79 | /// be_sure_we_got_to_the_end_of_it
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D doc-markdown` implied by `-D warnings`
error: you should put `be_sure_we_got_to_the_end_of_it` between ticks in the documentation
--> doc.rs:86:5
|
86 | /// be_sure_we_got_to_the_end_of_it
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D doc-markdown` implied by `-D warnings`
error: you should put `be_sure_we_got_to_the_end_of_it` between ticks in the documentation
--> doc.rs:99:5
|
99 | /// be_sure_we_got_to_the_end_of_it
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D doc-markdown` implied by `-D warnings`
error: you should put `FooBar` between ticks in the documentation
--> doc.rs:110:42
|
107 | /// Not a title #897 CamelCaseThing
| ^^^^^^^^^^^^^^
110 | /** E.g. serialization of an empty list: FooBar
| ^^^^^^
|
= note: `-D doc-markdown` implied by `-D warnings`
error: you should put `be_sure_we_got_to_the_end_of_it` between ticks in the documentation
--> doc.rs:108:5
|
108 | /// be_sure_we_got_to_the_end_of_it
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D doc-markdown` implied by `-D warnings`
error: you should put `be_sure_we_got_to_the_end_of_it` between ticks in the documentation
error: you should put `BarQuz` between ticks in the documentation
--> doc.rs:115:5
|
115 | /// be_sure_we_got_to_the_end_of_it
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D doc-markdown` implied by `-D warnings`
error: you should put `be_sure_we_got_to_the_end_of_it` between ticks in the documentation
--> doc.rs:128:5
|
128 | /// be_sure_we_got_to_the_end_of_it
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D doc-markdown` implied by `-D warnings`
error: you should put `FooBar` between ticks in the documentation
--> doc.rs:139:42
|
139 | /** E.g. serialization of an empty list: FooBar
| ^^^^^^
|
= note: `-D doc-markdown` implied by `-D warnings`
error: you should put `BarQuz` between ticks in the documentation
--> doc.rs:144:5
|
144 | And BarQuz too.
115 | And BarQuz too.
| ^^^^^^
|
= note: `-D doc-markdown` implied by `-D warnings`
error: you should put `be_sure_we_got_to_the_end_of_it` between ticks in the documentation
--> doc.rs:145:1
--> doc.rs:116:1
|
145 | be_sure_we_got_to_the_end_of_it
116 | be_sure_we_got_to_the_end_of_it
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D doc-markdown` implied by `-D warnings`
error: you should put `FooBar` between ticks in the documentation
--> doc.rs:150:42
--> doc.rs:121:42
|
150 | /** E.g. serialization of an empty list: FooBar
121 | /** E.g. serialization of an empty list: FooBar
| ^^^^^^
|
= note: `-D doc-markdown` implied by `-D warnings`
error: you should put `BarQuz` between ticks in the documentation
--> doc.rs:155:5
--> doc.rs:126:5
|
155 | And BarQuz too.
126 | And BarQuz too.
| ^^^^^^
|
= note: `-D doc-markdown` implied by `-D warnings`
error: you should put `be_sure_we_got_to_the_end_of_it` between ticks in the documentation
--> doc.rs:156:1
--> doc.rs:127:1
|
156 | be_sure_we_got_to_the_end_of_it
127 | be_sure_we_got_to_the_end_of_it
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D doc-markdown` implied by `-D warnings`
error: you should put `be_sure_we_got_to_the_end_of_it` between ticks in the documentation
--> doc.rs:167:5
--> doc.rs:138:5
|
167 | /// be_sure_we_got_to_the_end_of_it
138 | /// be_sure_we_got_to_the_end_of_it
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D doc-markdown` implied by `-D warnings`