Fix DOC_MARKDOWN requiring backticks on links to external websites

This commit is contained in:
Krishna Sundarram 2023-02-16 14:20:40 +00:00
parent 86fb33a973
commit 9c9dbc2408
4 changed files with 15 additions and 35 deletions

View file

@ -6,6 +6,11 @@ use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
use clippy_utils::{is_entrypoint_fn, method_chain_args, return_ty}; use clippy_utils::{is_entrypoint_fn, method_chain_args, return_ty};
use if_chain::if_chain; use if_chain::if_chain;
use itertools::Itertools; use itertools::Itertools;
use pulldown_cmark::Event::{
Code, End, FootnoteReference, HardBreak, Html, Rule, SoftBreak, Start, TaskListMarker, Text,
};
use pulldown_cmark::Tag::{CodeBlock, Heading, Item, Link, Paragraph};
use pulldown_cmark::{BrokenLink, CodeBlockKind, CowStr, Options};
use rustc_ast::ast::{Async, AttrKind, Attribute, Fn, FnRetTy, ItemKind}; use rustc_ast::ast::{Async, AttrKind, Attribute, Fn, FnRetTy, ItemKind};
use rustc_ast::token::CommentKind; use rustc_ast::token::CommentKind;
use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::fx::FxHashSet;
@ -497,7 +502,6 @@ struct DocHeaders {
} }
fn check_attrs(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, attrs: &[Attribute]) -> Option<DocHeaders> { fn check_attrs(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, attrs: &[Attribute]) -> Option<DocHeaders> {
use pulldown_cmark::{BrokenLink, CowStr, Options};
/// We don't want the parser to choke on intra doc links. Since we don't /// We don't want the parser to choke on intra doc links. Since we don't
/// actually care about rendering them, just pretend that all broken links are /// actually care about rendering them, just pretend that all broken links are
/// point to a fake address. /// point to a fake address.
@ -538,8 +542,6 @@ fn check_attrs(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, attrs: &[
pulldown_cmark::Parser::new_with_broken_link_callback(&doc, Options::empty(), Some(&mut cb)).into_offset_iter(); pulldown_cmark::Parser::new_with_broken_link_callback(&doc, Options::empty(), Some(&mut cb)).into_offset_iter();
// Iterate over all `Events` and combine consecutive events into one // Iterate over all `Events` and combine consecutive events into one
let events = parser.coalesce(|previous, current| { let events = parser.coalesce(|previous, current| {
use pulldown_cmark::Event::Text;
let previous_range = previous.1; let previous_range = previous.1;
let current_range = current.1; let current_range = current.1;
@ -564,12 +566,6 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
spans: &[(usize, Span)], spans: &[(usize, Span)],
) -> DocHeaders { ) -> DocHeaders {
// true if a safety header was found // true if a safety header was found
use pulldown_cmark::Event::{
Code, End, FootnoteReference, HardBreak, Html, Rule, SoftBreak, Start, TaskListMarker, Text,
};
use pulldown_cmark::Tag::{CodeBlock, Heading, Item, Link, Paragraph};
use pulldown_cmark::{CodeBlockKind, CowStr};
let mut headers = DocHeaders::default(); let mut headers = DocHeaders::default();
let mut in_code = false; let mut in_code = false;
let mut in_link = None; let mut in_link = None;
@ -660,6 +656,12 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
check_link_quotes(cx, in_link.is_some(), trimmed_text, span, &range, begin, text.len()); check_link_quotes(cx, in_link.is_some(), trimmed_text, span, &range, begin, text.len());
// Adjust for the beginning of the current `Event` // Adjust for the beginning of the current `Event`
let span = span.with_lo(span.lo() + BytePos::from_usize(range.start - begin)); let span = span.with_lo(span.lo() + BytePos::from_usize(range.start - begin));
if let Some(link) = in_link.as_ref()
&& let Ok(url) = Url::parse(link)
&& (url.scheme() == "https" || url.scheme() == "http") {
// Don't check the text associated with external URLs
continue;
}
text_to_check.push((text, span)); text_to_check.push((text, span));
} }
}, },

View file

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

View file

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

View file

@ -142,28 +142,6 @@ help: try
LL | /// `be_sure_we_got_to_the_end_of_it` LL | /// `be_sure_we_got_to_the_end_of_it`
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
error: item in documentation is missing backticks
--> $DIR/doc-fixable.rs:78:22
|
LL | /// This test has [a link_with_underscores][chunked-example] inside it. See #823.
| ^^^^^^^^^^^^^^^^^^^^^
|
help: try
|
LL | /// This test has [a `link_with_underscores`][chunked-example] inside it. See #823.
| ~~~~~~~~~~~~~~~~~~~~~~~
error: item in documentation is missing backticks
--> $DIR/doc-fixable.rs:81:21
|
LL | /// It can also be [inline_link2].
| ^^^^^^^^^^^^
|
help: try
|
LL | /// It can also be [`inline_link2`].
| ~~~~~~~~~~~~~~
error: item in documentation is missing backticks error: item in documentation is missing backticks
--> $DIR/doc-fixable.rs:91:5 --> $DIR/doc-fixable.rs:91:5
| |
@ -329,5 +307,5 @@ help: try
LL | /// An iterator over `mycrate::Collection`'s values. LL | /// An iterator over `mycrate::Collection`'s values.
| ~~~~~~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~~~~~~
error: aborting due to 30 previous errors error: aborting due to 28 previous errors