Skip to content

Commit ebc9891

Browse files
authored
Merge pull request #1838 from rust-lang-nursery/fix-doc
Don't lint autolinks in `doc_markdown`
2 parents 1cf4672 + aca6c1e commit ebc9891

File tree

6 files changed

+97
-2
lines changed

6 files changed

+97
-2
lines changed

Cargo.lock

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

clippy_lints/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ serde_derive = "1.0"
2828
toml = "0.4"
2929
unicode-normalization = "0.1"
3030
pulldown-cmark = "0.0.15"
31+
url = "1.5.0"
3132

3233
[features]
3334
debugging = []

clippy_lints/src/doc.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use syntax::ast;
55
use syntax::codemap::{BytePos, Span};
66
use syntax_pos::Pos;
77
use utils::span_lint;
8+
use url::Url;
89

910
/// **What it does:** Checks for the presence of `_`, `::` or camel-case words
1011
/// outside ticks in documentation.
@@ -195,16 +196,26 @@ fn check_doc<'a, Events: Iterator<Item = (usize, pulldown_cmark::Event<'a>)>>(
195196
use pulldown_cmark::Tag::*;
196197

197198
let mut in_code = false;
199+
let mut in_link = None;
198200

199201
for (offset, event) in docs {
200202
match event {
201203
Start(CodeBlock(_)) | Start(Code) => in_code = true,
202204
End(CodeBlock(_)) | End(Code) => in_code = false,
203-
Start(_tag) | End(_tag) => (), // We don't care about other tags
205+
Start(Link(link, _)) => in_link = Some(link),
206+
End(Link(_, _)) => in_link = None,
207+
Start(_tag) | End(_tag) => (), // We don't care about other tags
204208
Html(_html) | InlineHtml(_html) => (), // HTML is weird, just ignore it
205209
SoftBreak => (),
206210
HardBreak => (),
207211
FootnoteReference(text) | Text(text) => {
212+
if Some(&text) == in_link.as_ref() {
213+
// Probably a link of the form `<http://example.com>`
214+
// Which are represented as a link to "http://example.com" with
215+
// text "http://example.com" by pulldown-cmark
216+
continue;
217+
}
218+
208219
if !in_code {
209220
let index = match spans.binary_search_by(|c| c.0.cmp(&offset)) {
210221
Ok(o) => o,
@@ -270,6 +281,18 @@ fn check_word(cx: &EarlyContext, word: &str, span: Span) {
270281
s != "_" && !s.contains("\\_") && s.contains('_')
271282
}
272283

284+
if let Ok(url) = Url::parse(word) {
285+
// try to get around the fact that `foo::bar` parses as a valid URL
286+
if !url.cannot_be_a_base() {
287+
span_lint(cx,
288+
DOC_MARKDOWN,
289+
span,
290+
"you should put bare URLs between `<`/`>` or make a proper Markdown link");
291+
292+
return;
293+
}
294+
}
295+
273296
if has_underscore(word) || word.contains("::") || is_camel_case(word) {
274297
span_lint(
275298
cx,

clippy_lints/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ extern crate lazy_static;
5151

5252
extern crate itertools;
5353
extern crate pulldown_cmark;
54+
extern crate url;
5455

5556
macro_rules! declare_restriction_lint {
5657
{ pub $name:tt, $description:tt } => {

tests/ui/doc.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,11 @@ fn issue_1469() {}
159159
*This would also be an error under a strict common mark interpretation
160160
*/
161161
fn issue_1920() {}
162+
163+
/// Ok: <http://www.unicode.org/reports/tr9/#Reordering_Resolved_Levels>
164+
///
165+
/// Not ok: http://www.unicode.org
166+
/// Not ok: https://www.unicode.org
167+
/// Not ok: http://www.unicode.org/
168+
/// Not ok: http://www.unicode.org/reports/tr9/#Reordering_Resolved_Levels
169+
fn issue_1832() {}

tests/ui/doc.stderr

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,5 +156,29 @@ error: you should put `be_sure_we_got_to_the_end_of_it` between ticks in the doc
156156
138 | /// be_sure_we_got_to_the_end_of_it
157157
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
158158

159-
error: aborting due to 26 previous errors
159+
error: you should put bare URLs between `<`/`>` or make a proper Markdown link
160+
--> $DIR/doc.rs:165:13
161+
|
162+
165 | /// Not ok: http://www.unicode.org
163+
| ^^^^^^^^^^^^^^^^^^^^^^
164+
165+
error: you should put bare URLs between `<`/`>` or make a proper Markdown link
166+
--> $DIR/doc.rs:166:13
167+
|
168+
166 | /// Not ok: https://www.unicode.org
169+
| ^^^^^^^^^^^^^^^^^^^^^^^
170+
171+
error: you should put bare URLs between `<`/`>` or make a proper Markdown link
172+
--> $DIR/doc.rs:167:13
173+
|
174+
167 | /// Not ok: http://www.unicode.org/
175+
| ^^^^^^^^^^^^^^^^^^^^^^
176+
177+
error: you should put bare URLs between `<`/`>` or make a proper Markdown link
178+
--> $DIR/doc.rs:168:13
179+
|
180+
168 | /// Not ok: http://www.unicode.org/reports/tr9/#Reordering_Resolved_Levels
181+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
182+
183+
error: aborting due to 30 previous errors
160184

0 commit comments

Comments
 (0)