1 use itertools::Itertools;
5 use syntax::codemap::{Span, BytePos};
9 /// **What it does:** Checks for the presence of `_`, `::` or camel-case words
10 /// outside ticks in documentation.
12 /// **Why is this bad?** *Rustdoc* supports markdown formatting, `_`, `::` and
13 /// camel-case probably indicates some code which should be included between
14 /// ticks. `_` can also be used for empasis in markdown, this lint tries to
17 /// **Known problems:** Lots of bad docs won’t be fixed, what the lint checks
18 /// for is limited, and there are still false positives.
22 /// /// Do something with the foo_bar parameter. See also that::other::module::foo.
23 /// // ^ `foo_bar` and `that::other::module::foo` should be ticked.
24 /// fn doit(foo_bar) { .. }
29 "presence of `_`, `::` or camel-case outside backticks in documentation"
34 valid_idents: Vec<String>,
38 pub fn new(valid_idents: Vec<String>) -> Self {
39 Doc { valid_idents: valid_idents }
43 impl LintPass for Doc {
44 fn get_lints(&self) -> LintArray {
45 lint_array![DOC_MARKDOWN]
49 impl EarlyLintPass for Doc {
50 fn check_crate(&mut self, cx: &EarlyContext, krate: &ast::Crate) {
51 check_attrs(cx, &self.valid_idents, &krate.attrs);
54 fn check_item(&mut self, cx: &EarlyContext, item: &ast::Item) {
55 check_attrs(cx, &self.valid_idents, &item.attrs);
60 parser: pulldown_cmark::Parser<'a>,
64 fn new(parser: pulldown_cmark::Parser<'a>) -> Parser<'a> {
65 Self { parser: parser }
69 impl<'a> Iterator for Parser<'a> {
70 type Item = (usize, pulldown_cmark::Event<'a>);
72 fn next(&mut self) -> Option<Self::Item> {
73 let offset = self.parser.get_offset();
74 self.parser.next().map(|event| (offset, event))
78 /// Cleanup documentation decoration (`///` and such).
80 /// We can't use `syntax::attr::AttributeMethods::with_desugared_doc` or
81 /// `syntax::parse::lexer::comments::strip_doc_comment_decoration` because we need to keep track of
82 /// the spans but this function is inspired from the later.
83 #[allow(cast_possible_truncation)]
84 pub fn strip_doc_comment_decoration(comment: &str, span: Span) -> (String, Vec<(usize, Span)>) {
85 // one-line comments lose their prefix
86 const ONELINERS: &'static [&'static str] = &["///!", "///", "//!", "//"];
87 for prefix in ONELINERS {
88 if comment.starts_with(*prefix) {
89 let doc = &comment[prefix.len()..];
90 let mut doc = doc.to_owned();
92 return (doc.to_owned(), vec![(doc.len(), Span { lo: span.lo + BytePos(prefix.len() as u32), ..span })]);
96 if comment.starts_with("/*") {
97 let doc = &comment[3..comment.len() - 2];
98 let mut sizes = vec![];
100 for line in doc.lines() {
101 let offset = line.as_ptr() as usize - comment.as_ptr() as usize;
102 debug_assert_eq!(offset as u32 as usize, offset);
104 // +1 for the newline
105 sizes.push((line.len() + 1, Span { lo: span.lo + BytePos(offset as u32), ..span }));
108 return (doc.to_string(), sizes);
111 panic!("not a doc-comment: {}", comment);
114 pub fn check_attrs<'a>(cx: &EarlyContext, valid_idents: &[String], attrs: &'a [ast::Attribute]) {
115 let mut doc = String::new();
116 let mut spans = vec![];
119 if attr.is_sugared_doc {
120 if let Some(ref current) = attr.value_str() {
121 let current = current.to_string();
122 let (current, current_spans) = strip_doc_comment_decoration(¤t, attr.span);
123 spans.extend_from_slice(¤t_spans);
124 doc.push_str(¤t);
126 } else if let Some(name) = attr.name() {
127 // ignore mix of sugared and non-sugared doc
135 for &mut (ref mut offset, _) in &mut spans {
136 let offset_copy = *offset;
138 current += offset_copy;
142 let parser = Parser::new(pulldown_cmark::Parser::new(&doc));
143 let parser = parser.coalesce(|x, y| {
144 use pulldown_cmark::Event::*;
150 (Text(x), Text(y)) => {
151 let mut x = x.into_owned();
153 Ok((x_offset, Text(x.into())))
155 (x, y) => Err(((x_offset, x), (y_offset, y))),
158 check_doc(cx, valid_idents, parser, &spans);
162 fn check_doc<'a, Events: Iterator<Item = (usize, pulldown_cmark::Event<'a>)>>(
164 valid_idents: &[String],
166 spans: &[(usize, Span)]
168 use pulldown_cmark::Event::*;
169 use pulldown_cmark::Tag::*;
171 let mut in_code = false;
173 for (offset, event) in docs {
175 Start(CodeBlock(_)) |
176 Start(Code) => in_code = true,
178 End(Code) => in_code = false,
179 Start(_tag) | End(_tag) => (), // We don't care about other tags
181 InlineHtml(_html) => (), // HTML is weird, just ignore it
184 FootnoteReference(text) |
187 let index = match spans.binary_search_by(|c| c.0.cmp(&offset)) {
192 let (begin, span) = spans[index];
194 // Adjust for the begining of the current `Event`
195 let span = Span { lo: span.lo + BytePos::from_usize(offset - begin), ..span };
197 check_text(cx, valid_idents, &text, span);
204 fn check_text(cx: &EarlyContext, valid_idents: &[String], text: &str, span: Span) {
205 for word in text.split_whitespace() {
206 // Trim punctuation as in `some comment (see foo::bar).`
208 // Or even as in `_foo bar_` which is emphasized.
209 let word = word.trim_matches(|c: char| !c.is_alphanumeric());
211 if valid_idents.iter().any(|i| i == word) {
215 // Adjust for the current word
216 let offset = word.as_ptr() as usize - text.as_ptr() as usize;
218 lo: span.lo + BytePos::from_usize(offset),
219 hi: span.lo + BytePos::from_usize(offset + word.len()),
223 check_word(cx, word, span);
227 fn check_word(cx: &EarlyContext, word: &str, span: Span) {
228 /// Checks if a string is camel-case, ie. contains at least two uppercase letter (`Clippy` is
229 /// ok) and one lower-case letter (`NASA` is ok). Plural are also excluded (`IDs` is ok).
230 fn is_camel_case(s: &str) -> bool {
231 if s.starts_with(|c: char| c.is_digit(10)) {
235 let s = if s.ends_with('s') {
241 s.chars().all(char::is_alphanumeric) && s.chars().filter(|&c| c.is_uppercase()).take(2).count() > 1 &&
242 s.chars().filter(|&c| c.is_lowercase()).take(1).count() > 0
245 fn has_underscore(s: &str) -> bool {
246 s != "_" && !s.contains("\\_") && s.contains('_')
249 if has_underscore(word) || word.contains("::") || is_camel_case(word) {
253 &format!("you should put `{}` between ticks in the documentation", word));