Skip to content

Commit be74773

Browse files
committed
Auto merge of rust-lang#10357 - nindalf:doc_markdown_relax, r=llogiq
Stop doc_markdown requiring backticks on links to external websites Fixes rust-lang#10302 This lint currently checks that any link should be enclosed with `backticks` if the title looks like a lang item. This PR changes the lint to only run on internal links. External links, indicated by `http` or `https`, are skipped. This PR also reorganises `pulldown_cmark` imports to bypass the clippy lint enforcing 100 line functions. --- changelog: Stop doc_markdown requiring backticks on links to external websites
2 parents dfe23dc + 9c9dbc2 commit be74773

File tree

4 files changed

+15
-35
lines changed

4 files changed

+15
-35
lines changed

clippy_lints/src/doc.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
66
use clippy_utils::{is_entrypoint_fn, method_chain_args, return_ty};
77
use if_chain::if_chain;
88
use itertools::Itertools;
9+
use pulldown_cmark::Event::{
10+
Code, End, FootnoteReference, HardBreak, Html, Rule, SoftBreak, Start, TaskListMarker, Text,
11+
};
12+
use pulldown_cmark::Tag::{CodeBlock, Heading, Item, Link, Paragraph};
13+
use pulldown_cmark::{BrokenLink, CodeBlockKind, CowStr, Options};
914
use rustc_ast::ast::{Async, AttrKind, Attribute, Fn, FnRetTy, ItemKind};
1015
use rustc_ast::token::CommentKind;
1116
use rustc_data_structures::fx::FxHashSet;
@@ -497,7 +502,6 @@ struct DocHeaders {
497502
}
498503

499504
fn check_attrs(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, attrs: &[Attribute]) -> Option<DocHeaders> {
500-
use pulldown_cmark::{BrokenLink, CowStr, Options};
501505
/// We don't want the parser to choke on intra doc links. Since we don't
502506
/// actually care about rendering them, just pretend that all broken links are
503507
/// point to a fake address.
@@ -538,8 +542,6 @@ fn check_attrs(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, attrs: &[
538542
pulldown_cmark::Parser::new_with_broken_link_callback(&doc, Options::empty(), Some(&mut cb)).into_offset_iter();
539543
// Iterate over all `Events` and combine consecutive events into one
540544
let events = parser.coalesce(|previous, current| {
541-
use pulldown_cmark::Event::Text;
542-
543545
let previous_range = previous.1;
544546
let current_range = current.1;
545547

@@ -564,12 +566,6 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
564566
spans: &[(usize, Span)],
565567
) -> DocHeaders {
566568
// true if a safety header was found
567-
use pulldown_cmark::Event::{
568-
Code, End, FootnoteReference, HardBreak, Html, Rule, SoftBreak, Start, TaskListMarker, Text,
569-
};
570-
use pulldown_cmark::Tag::{CodeBlock, Heading, Item, Link, Paragraph};
571-
use pulldown_cmark::{CodeBlockKind, CowStr};
572-
573569
let mut headers = DocHeaders::default();
574570
let mut in_code = false;
575571
let mut in_link = None;
@@ -660,6 +656,12 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
660656
check_link_quotes(cx, in_link.is_some(), trimmed_text, span, &range, begin, text.len());
661657
// Adjust for the beginning of the current `Event`
662658
let span = span.with_lo(span.lo() + BytePos::from_usize(range.start - begin));
659+
if let Some(link) = in_link.as_ref()
660+
&& let Ok(url) = Url::parse(link)
661+
&& (url.scheme() == "https" || url.scheme() == "http") {
662+
// Don't check the text associated with external URLs
663+
continue;
664+
}
663665
text_to_check.push((text, span));
664666
}
665667
},

tests/ui/doc/doc-fixable.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ fn test_allowed() {
7878
/// This test has [a `link_with_underscores`][chunked-example] inside it. See #823.
7979
/// See also [the issue tracker](https://github.com/rust-lang/rust-clippy/search?q=clippy::doc_markdown&type=Issues)
8080
/// on GitHub (which is a camel-cased word, but is OK). And here is another [inline link][inline_link].
81-
/// It can also be [`inline_link2`].
81+
/// It can also be [inline_link2]. A link to [StackOverflow](https://stackoverflow.com) is also acceptable.
8282
///
8383
/// [chunked-example]: https://en.wikipedia.org/wiki/Chunked_transfer_encoding#Example
8484
/// [inline_link]: https://foobar

tests/ui/doc/doc-fixable.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ fn test_units() {
7575
fn test_allowed() {
7676
}
7777

78-
/// This test has [a link_with_underscores][chunked-example] inside it. See #823.
78+
/// This test has [a `link_with_underscores`][chunked-example] inside it. See #823.
7979
/// See also [the issue tracker](https://github.com/rust-lang/rust-clippy/search?q=clippy::doc_markdown&type=Issues)
8080
/// on GitHub (which is a camel-cased word, but is OK). And here is another [inline link][inline_link].
81-
/// It can also be [inline_link2].
81+
/// It can also be [inline_link2]. A link to [StackOverflow](https://stackoverflow.com) is also acceptable.
8282
///
8383
/// [chunked-example]: https://en.wikipedia.org/wiki/Chunked_transfer_encoding#Example
8484
/// [inline_link]: https://foobar

tests/ui/doc/doc-fixable.stderr

+1-23
Original file line numberDiff line numberDiff line change
@@ -142,28 +142,6 @@ help: try
142142
LL | /// `be_sure_we_got_to_the_end_of_it`
143143
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
144144

145-
error: item in documentation is missing backticks
146-
--> $DIR/doc-fixable.rs:78:22
147-
|
148-
LL | /// This test has [a link_with_underscores][chunked-example] inside it. See #823.
149-
| ^^^^^^^^^^^^^^^^^^^^^
150-
|
151-
help: try
152-
|
153-
LL | /// This test has [a `link_with_underscores`][chunked-example] inside it. See #823.
154-
| ~~~~~~~~~~~~~~~~~~~~~~~
155-
156-
error: item in documentation is missing backticks
157-
--> $DIR/doc-fixable.rs:81:21
158-
|
159-
LL | /// It can also be [inline_link2].
160-
| ^^^^^^^^^^^^
161-
|
162-
help: try
163-
|
164-
LL | /// It can also be [`inline_link2`].
165-
| ~~~~~~~~~~~~~~
166-
167145
error: item in documentation is missing backticks
168146
--> $DIR/doc-fixable.rs:91:5
169147
|
@@ -329,5 +307,5 @@ help: try
329307
LL | /// An iterator over `mycrate::Collection`'s values.
330308
| ~~~~~~~~~~~~~~~~~~~~~
331309

332-
error: aborting due to 30 previous errors
310+
error: aborting due to 28 previous errors
333311

0 commit comments

Comments
 (0)