Skip to content

Commit 8c0e038

Browse files
committed
Auto merge of rust-lang#4029 - phansch:update_pulldown_cmark, r=oli-obk
Update pulldown_cmark to 0.5 We now no longer have to use our own wrapper around `Parser` and can use the new `OffsetIter`. changelog: none
2 parents dbaa6f0 + 32e877c commit 8c0e038

File tree

2 files changed

+24
-41
lines changed

2 files changed

+24
-41
lines changed

clippy_lints/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ serde = "1.0"
2828
serde_derive = "1.0"
2929
toml = "0.4"
3030
unicode-normalization = "0.1"
31-
pulldown-cmark = "0.2"
31+
pulldown-cmark = "0.5.0"
3232
url = "1.7.0"
3333
if_chain = "0.1.3"
3434
smallvec = { version = "0.6.5", features = ["union"] }

clippy_lints/src/doc.rs

+23-40
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use pulldown_cmark;
44
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass};
55
use rustc::{declare_tool_lint, impl_lint_pass};
66
use rustc_data_structures::fx::FxHashSet;
7+
use std::ops::Range;
78
use syntax::ast;
89
use syntax::source_map::{BytePos, Span};
910
use syntax_pos::Pos;
@@ -57,25 +58,6 @@ impl EarlyLintPass for DocMarkdown {
5758
}
5859
}
5960

60-
struct Parser<'a> {
61-
parser: pulldown_cmark::Parser<'a>,
62-
}
63-
64-
impl<'a> Parser<'a> {
65-
fn new(parser: pulldown_cmark::Parser<'a>) -> Self {
66-
Self { parser }
67-
}
68-
}
69-
70-
impl<'a> Iterator for Parser<'a> {
71-
type Item = (usize, pulldown_cmark::Event<'a>);
72-
73-
fn next(&mut self) -> Option<Self::Item> {
74-
let offset = self.parser.get_offset();
75-
self.parser.next().map(|event| (offset, event))
76-
}
77-
}
78-
7961
/// Cleanup documentation decoration (`///` and such).
8062
///
8163
/// We can't use `syntax::attr::AttributeMethods::with_desugared_doc` or
@@ -159,30 +141,31 @@ pub fn check_attrs<'a>(cx: &EarlyContext<'_>, valid_idents: &FxHashSet<String>,
159141
}
160142

161143
if !doc.is_empty() {
162-
let parser = Parser::new(pulldown_cmark::Parser::new(&doc));
163-
let parser = parser.coalesce(|x, y| {
144+
let parser = pulldown_cmark::Parser::new(&doc).into_offset_iter();
145+
// Iterate over all `Events` and combine consecutive events into one
146+
let events = parser.coalesce(|previous, current| {
164147
use pulldown_cmark::Event::*;
165148

166-
let x_offset = x.0;
167-
let y_offset = y.0;
149+
let previous_range = previous.1;
150+
let current_range = current.1;
168151

169-
match (x.1, y.1) {
170-
(Text(x), Text(y)) => {
171-
let mut x = x.into_owned();
172-
x.push_str(&y);
173-
Ok((x_offset, Text(x.into())))
152+
match (previous.0, current.0) {
153+
(Text(previous), Text(current)) => {
154+
let mut previous = previous.to_string();
155+
previous.push_str(&current);
156+
Ok((Text(previous.into()), previous_range))
174157
},
175-
(x, y) => Err(((x_offset, x), (y_offset, y))),
158+
(previous, current) => Err(((previous, previous_range), (current, current_range))),
176159
}
177160
});
178-
check_doc(cx, valid_idents, parser, &spans);
161+
check_doc(cx, valid_idents, events, &spans);
179162
}
180163
}
181164

182-
fn check_doc<'a, Events: Iterator<Item = (usize, pulldown_cmark::Event<'a>)>>(
165+
fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize>)>>(
183166
cx: &EarlyContext<'_>,
184167
valid_idents: &FxHashSet<String>,
185-
docs: Events,
168+
events: Events,
186169
spans: &[(usize, Span)],
187170
) {
188171
use pulldown_cmark::Event::*;
@@ -191,15 +174,15 @@ fn check_doc<'a, Events: Iterator<Item = (usize, pulldown_cmark::Event<'a>)>>(
191174
let mut in_code = false;
192175
let mut in_link = None;
193176

194-
for (offset, event) in docs {
177+
for (event, range) in events {
195178
match event {
196-
Start(CodeBlock(_)) | Start(Code) => in_code = true,
197-
End(CodeBlock(_)) | End(Code) => in_code = false,
198-
Start(Link(link, _)) => in_link = Some(link),
199-
End(Link(_, _)) => in_link = None,
179+
Start(CodeBlock(_)) => in_code = true,
180+
End(CodeBlock(_)) => in_code = false,
181+
Start(Link(_, url, _)) => in_link = Some(url),
182+
End(Link(..)) => in_link = None,
200183
Start(_tag) | End(_tag) => (), // We don't care about other tags
201184
Html(_html) | InlineHtml(_html) => (), // HTML is weird, just ignore it
202-
SoftBreak | HardBreak => (),
185+
SoftBreak | HardBreak | TaskListMarker(_) | Code(_) => (),
203186
FootnoteReference(text) | Text(text) => {
204187
if Some(&text) == in_link.as_ref() {
205188
// Probably a link of the form `<http://example.com>`
@@ -209,15 +192,15 @@ fn check_doc<'a, Events: Iterator<Item = (usize, pulldown_cmark::Event<'a>)>>(
209192
}
210193

211194
if !in_code {
212-
let index = match spans.binary_search_by(|c| c.0.cmp(&offset)) {
195+
let index = match spans.binary_search_by(|c| c.0.cmp(&range.start)) {
213196
Ok(o) => o,
214197
Err(e) => e - 1,
215198
};
216199

217200
let (begin, span) = spans[index];
218201

219202
// Adjust for the beginning of the current `Event`
220-
let span = span.with_lo(span.lo() + BytePos::from_usize(offset - begin));
203+
let span = span.with_lo(span.lo() + BytePos::from_usize(range.start - begin));
221204

222205
check_text(cx, valid_idents, &text, span);
223206
}

0 commit comments

Comments
 (0)