From ef4e5b9ecb10480cd87bd7875e4169ca5edeb0fd Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sat, 27 Mar 2021 23:35:02 -0400 Subject: Rename non_autolinks -> bare_urls --- src/doc/rustdoc/src/lints.md | 23 ++--- src/librustdoc/lint.rs | 15 +-- src/librustdoc/passes/bare_urls.rs | 111 +++++++++++++++++++++++ src/librustdoc/passes/mod.rs | 8 +- src/librustdoc/passes/non_autolinks.rs | 111 ----------------------- src/test/rustdoc-ui/unknown-renamed-lints.rs | 6 +- src/test/rustdoc-ui/unknown-renamed-lints.stderr | 18 +++- src/test/rustdoc-ui/url-improvements.rs | 4 +- src/test/rustdoc-ui/url-improvements.stderr | 4 +- 9 files changed, 155 insertions(+), 145 deletions(-) create mode 100644 src/librustdoc/passes/bare_urls.rs delete mode 100644 src/librustdoc/passes/non_autolinks.rs (limited to 'src') diff --git a/src/doc/rustdoc/src/lints.md b/src/doc/rustdoc/src/lints.md index 174db711bce..7a3afb2cfb8 100644 --- a/src/doc/rustdoc/src/lints.md +++ b/src/doc/rustdoc/src/lints.md @@ -294,17 +294,14 @@ warning: unclosed HTML tag `h1` warning: 2 warnings emitted ``` -## non_autolinks +## bare_urls -This lint is **nightly-only** and **warns by default**. It detects links which -could use the "automatic" link syntax. For example: +This lint is **nightly-only** and **warns by default**. It detects URLs which are not links. +For example: ```rust /// http://example.org -/// [http://example.com](http://example.com) /// [http://example.net] -/// -/// [http://example.com]: http://example.com pub fn foo() {} ``` @@ -312,22 +309,18 @@ Which will give: ```text warning: this URL is not a hyperlink - --> foo.rs:1:5 + --> links.rs:1:5 | 1 | /// http://example.org | ^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `` | - = note: `#[warn(rustdoc::non_autolinks)]` on by default - -warning: unneeded long form for URL - --> foo.rs:2:5 - | -2 | /// [http://example.com](http://example.com) - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `` + = note: `#[warn(rustdoc::bare_urls)]` on by default warning: this URL is not a hyperlink - --> foo.rs:3:6 + --> links.rs:3:6 | 3 | /// [http://example.net] | ^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `` + +warning: 2 warnings emitted ``` diff --git a/src/librustdoc/lint.rs b/src/librustdoc/lint.rs index ffa2f7a47fd..fe3102e94f8 100644 --- a/src/librustdoc/lint.rs +++ b/src/librustdoc/lint.rs @@ -148,14 +148,13 @@ declare_rustdoc_lint! { } declare_rustdoc_lint! { - /// The `non_autolinks` lint detects when a URL could be written using - /// only angle brackets. This is a `rustdoc` only lint, see the - /// documentation in the [rustdoc book]. + /// The `non_autolinks` lint detects when a URL is not a hyperlink. + /// This is a `rustdoc` only lint, see the documentation in the [rustdoc book]. /// - /// [rustdoc book]: ../../../rustdoc/lints.html#non_autolinks - NON_AUTOLINKS, + /// [rustdoc book]: ../../../rustdoc/lints.html#bare_urls + BARE_URLS, Warn, - "detects URLs that could be written using only angle brackets" + "detects URLs that are not hyperlinks" } crate static RUSTDOC_LINTS: Lazy> = Lazy::new(|| { @@ -166,7 +165,7 @@ crate static RUSTDOC_LINTS: Lazy> = Lazy::new(|| { PRIVATE_DOC_TESTS, INVALID_CODEBLOCK_ATTRIBUTES, INVALID_HTML_TAGS, - NON_AUTOLINKS, + BARE_URLS, MISSING_CRATE_LEVEL_DOCS, ] }); @@ -185,4 +184,6 @@ crate fn register_lints(_sess: &Session, lint_store: &mut LintStore) { } lint_store .register_renamed("intra_doc_link_resolution_failure", "rustdoc::broken_intra_doc_links"); + lint_store.register_renamed("non_autolinks", "rustdoc::bare_urls"); + lint_store.register_renamed("rustdoc::non_autolinks", "rustdoc::bare_urls"); } diff --git a/src/librustdoc/passes/bare_urls.rs b/src/librustdoc/passes/bare_urls.rs new file mode 100644 index 00000000000..3f2e1c68c55 --- /dev/null +++ b/src/librustdoc/passes/bare_urls.rs @@ -0,0 +1,111 @@ +use super::{span_of_attrs, Pass}; +use crate::clean::*; +use crate::core::DocContext; +use crate::fold::DocFolder; +use crate::html::markdown::opts; +use core::ops::Range; +use pulldown_cmark::{Event, Parser, Tag}; +use regex::Regex; +use rustc_errors::Applicability; +use std::lazy::SyncLazy; +use std::mem; + +crate const CHECK_BARE_URLS: Pass = Pass { + name: "check-bare-urls", + run: check_bare_urls, + description: "detects URLs that are not hyperlinks", +}; + +const URL_REGEX: SyncLazy = SyncLazy::new(|| { + Regex::new(concat!( + r"https?://", // url scheme + r"([-a-zA-Z0-9@:%._\+~#=]{2,256}\.)+", // one or more subdomains + r"[a-zA-Z]{2,63}", // root domain + r"\b([-a-zA-Z0-9@:%_\+.~#?&/=]*)" // optional query or url fragments + )) + .expect("failed to build regex") +}); + +struct BareUrlsLinter<'a, 'tcx> { + cx: &'a mut DocContext<'tcx>, +} + +impl<'a, 'tcx> BareUrlsLinter<'a, 'tcx> { + fn find_raw_urls( + &self, + text: &str, + range: Range, + f: &impl Fn(&DocContext<'_>, &str, &str, Range), + ) { + trace!("looking for raw urls in {}", text); + // For now, we only check "full" URLs (meaning, starting with "http://" or "https://"). + for match_ in URL_REGEX.find_iter(&text) { + let url = match_.as_str(); + let url_range = match_.range(); + f( + self.cx, + "this URL is not a hyperlink", + url, + Range { start: range.start + url_range.start, end: range.start + url_range.end }, + ); + } + } +} + +crate fn check_bare_urls(krate: Crate, cx: &mut DocContext<'_>) -> Crate { + BareUrlsLinter { cx }.fold_crate(krate) +} + +impl<'a, 'tcx> DocFolder for BareUrlsLinter<'a, 'tcx> { + fn fold_item(&mut self, item: Item) -> Option { + let hir_id = match DocContext::as_local_hir_id(self.cx.tcx, item.def_id) { + Some(hir_id) => hir_id, + None => { + // If non-local, no need to check anything. + return Some(self.fold_item_recur(item)); + } + }; + let dox = item.attrs.collapsed_doc_value().unwrap_or_default(); + if !dox.is_empty() { + let report_diag = |cx: &DocContext<'_>, msg: &str, url: &str, range: Range| { + let sp = super::source_span_for_markdown_range(cx.tcx, &dox, &range, &item.attrs) + .or_else(|| span_of_attrs(&item.attrs)) + .unwrap_or(item.span.inner()); + cx.tcx.struct_span_lint_hir(crate::lint::BARE_URLS, hir_id, sp, |lint| { + lint.build(msg) + .span_suggestion( + sp, + "use an automatic link instead", + format!("<{}>", url), + Applicability::MachineApplicable, + ) + .emit() + }); + }; + + let mut p = Parser::new_ext(&dox, opts()).into_offset_iter(); + + while let Some((event, range)) = p.next() { + match event { + Event::Text(s) => self.find_raw_urls(&s, range, &report_diag), + // We don't want to check the text inside code blocks or links. + Event::Start(tag @ (Tag::CodeBlock(_) | Tag::Link(..))) => { + while let Some((event, _)) = p.next() { + match event { + Event::End(end) + if mem::discriminant(&end) == mem::discriminant(&tag) => + { + break; + } + _ => {} + } + } + } + _ => {} + } + } + } + + Some(self.fold_item_recur(item)) + } +} diff --git a/src/librustdoc/passes/mod.rs b/src/librustdoc/passes/mod.rs index 755217a4629..4cc2f7120a0 100644 --- a/src/librustdoc/passes/mod.rs +++ b/src/librustdoc/passes/mod.rs @@ -12,8 +12,8 @@ use crate::core::DocContext; mod stripper; crate use stripper::*; -mod non_autolinks; -crate use self::non_autolinks::CHECK_NON_AUTOLINKS; +mod bare_urls; +crate use self::bare_urls::CHECK_BARE_URLS; mod strip_hidden; crate use self::strip_hidden::STRIP_HIDDEN; @@ -90,7 +90,7 @@ crate const PASSES: &[Pass] = &[ COLLECT_TRAIT_IMPLS, CALCULATE_DOC_COVERAGE, CHECK_INVALID_HTML_TAGS, - CHECK_NON_AUTOLINKS, + CHECK_BARE_URLS, ]; /// The list of passes run by default. @@ -105,7 +105,7 @@ crate const DEFAULT_PASSES: &[ConditionalPass] = &[ ConditionalPass::always(CHECK_CODE_BLOCK_SYNTAX), ConditionalPass::always(CHECK_INVALID_HTML_TAGS), ConditionalPass::always(PROPAGATE_DOC_CFG), - ConditionalPass::always(CHECK_NON_AUTOLINKS), + ConditionalPass::always(CHECK_BARE_URLS), ]; /// The list of default passes run when `--doc-coverage` is passed to rustdoc. diff --git a/src/librustdoc/passes/non_autolinks.rs b/src/librustdoc/passes/non_autolinks.rs deleted file mode 100644 index 0ebd1879db5..00000000000 --- a/src/librustdoc/passes/non_autolinks.rs +++ /dev/null @@ -1,111 +0,0 @@ -use super::{span_of_attrs, Pass}; -use crate::clean::*; -use crate::core::DocContext; -use crate::fold::DocFolder; -use crate::html::markdown::opts; -use core::ops::Range; -use pulldown_cmark::{Event, Parser, Tag}; -use regex::Regex; -use rustc_errors::Applicability; -use std::lazy::SyncLazy; -use std::mem; - -crate const CHECK_NON_AUTOLINKS: Pass = Pass { - name: "check-non-autolinks", - run: check_non_autolinks, - description: "detects URLs that could be linkified", -}; - -const URL_REGEX: SyncLazy = SyncLazy::new(|| { - Regex::new(concat!( - r"https?://", // url scheme - r"([-a-zA-Z0-9@:%._\+~#=]{2,256}\.)+", // one or more subdomains - r"[a-zA-Z]{2,63}", // root domain - r"\b([-a-zA-Z0-9@:%_\+.~#?&/=]*)" // optional query or url fragments - )) - .expect("failed to build regex") -}); - -struct NonAutolinksLinter<'a, 'tcx> { - cx: &'a mut DocContext<'tcx>, -} - -impl<'a, 'tcx> NonAutolinksLinter<'a, 'tcx> { - fn find_raw_urls( - &self, - text: &str, - range: Range, - f: &impl Fn(&DocContext<'_>, &str, &str, Range), - ) { - trace!("looking for raw urls in {}", text); - // For now, we only check "full" URLs (meaning, starting with "http://" or "https://"). - for match_ in URL_REGEX.find_iter(&text) { - let url = match_.as_str(); - let url_range = match_.range(); - f( - self.cx, - "this URL is not a hyperlink", - url, - Range { start: range.start + url_range.start, end: range.start + url_range.end }, - ); - } - } -} - -crate fn check_non_autolinks(krate: Crate, cx: &mut DocContext<'_>) -> Crate { - NonAutolinksLinter { cx }.fold_crate(krate) -} - -impl<'a, 'tcx> DocFolder for NonAutolinksLinter<'a, 'tcx> { - fn fold_item(&mut self, item: Item) -> Option { - let hir_id = match DocContext::as_local_hir_id(self.cx.tcx, item.def_id) { - Some(hir_id) => hir_id, - None => { - // If non-local, no need to check anything. - return Some(self.fold_item_recur(item)); - } - }; - let dox = item.attrs.collapsed_doc_value().unwrap_or_default(); - if !dox.is_empty() { - let report_diag = |cx: &DocContext<'_>, msg: &str, url: &str, range: Range| { - let sp = super::source_span_for_markdown_range(cx.tcx, &dox, &range, &item.attrs) - .or_else(|| span_of_attrs(&item.attrs)) - .unwrap_or(item.span.inner()); - cx.tcx.struct_span_lint_hir(crate::lint::NON_AUTOLINKS, hir_id, sp, |lint| { - lint.build(msg) - .span_suggestion( - sp, - "use an automatic link instead", - format!("<{}>", url), - Applicability::MachineApplicable, - ) - .emit() - }); - }; - - let mut p = Parser::new_ext(&dox, opts()).into_offset_iter(); - - while let Some((event, range)) = p.next() { - match event { - Event::Text(s) => self.find_raw_urls(&s, range, &report_diag), - // We don't want to check the text inside code blocks or links. - Event::Start(tag @ (Tag::CodeBlock(_) | Tag::Link(..))) => { - while let Some((event, _)) = p.next() { - match event { - Event::End(end) - if mem::discriminant(&end) == mem::discriminant(&tag) => - { - break; - } - _ => {} - } - } - } - _ => {} - } - } - } - - Some(self.fold_item_recur(item)) - } -} diff --git a/src/test/rustdoc-ui/unknown-renamed-lints.rs b/src/test/rustdoc-ui/unknown-renamed-lints.rs index a05c0c81168..9096cce1276 100644 --- a/src/test/rustdoc-ui/unknown-renamed-lints.rs +++ b/src/test/rustdoc-ui/unknown-renamed-lints.rs @@ -8,8 +8,12 @@ //~^ ERROR unknown lint: `rustdoc::x` #![deny(intra_doc_link_resolution_failure)] //~^ ERROR renamed to `rustdoc::broken_intra_doc_links` - #![deny(non_autolinks)] +//~^ ERROR renamed to `rustdoc::bare_urls` +#![deny(rustdoc::non_autolinks)] +//~^ ERROR renamed to `rustdoc::bare_urls` + +#![deny(private_doc_tests)] // FIXME: the old names for rustdoc lints should warn by default once `rustdoc::` makes it to the // stable channel. diff --git a/src/test/rustdoc-ui/unknown-renamed-lints.stderr b/src/test/rustdoc-ui/unknown-renamed-lints.stderr index 98bfb83c704..51e06821cf7 100644 --- a/src/test/rustdoc-ui/unknown-renamed-lints.stderr +++ b/src/test/rustdoc-ui/unknown-renamed-lints.stderr @@ -28,19 +28,31 @@ note: the lint level is defined here LL | #![deny(renamed_and_removed_lints)] | ^^^^^^^^^^^^^^^^^^^^^^^^^ +error: lint `non_autolinks` has been renamed to `rustdoc::bare_urls` + --> $DIR/unknown-renamed-lints.rs:11:9 + | +LL | #![deny(non_autolinks)] + | ^^^^^^^^^^^^^ help: use the new name: `rustdoc::bare_urls` + +error: lint `rustdoc::non_autolinks` has been renamed to `rustdoc::bare_urls` + --> $DIR/unknown-renamed-lints.rs:13:9 + | +LL | #![deny(rustdoc::non_autolinks)] + | ^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `rustdoc::bare_urls` + error: lint `rustdoc` has been removed: use `rustdoc::all` instead - --> $DIR/unknown-renamed-lints.rs:16:9 + --> $DIR/unknown-renamed-lints.rs:20:9 | LL | #![deny(rustdoc)] | ^^^^^^^ error: unknown lint: `rustdoc::intra_doc_link_resolution_failure` - --> $DIR/unknown-renamed-lints.rs:20:9 + --> $DIR/unknown-renamed-lints.rs:24:9 | LL | #![deny(rustdoc::intra_doc_link_resolution_failure)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: Compilation failed, aborting rustdoc -error: aborting due to 6 previous errors +error: aborting due to 8 previous errors diff --git a/src/test/rustdoc-ui/url-improvements.rs b/src/test/rustdoc-ui/url-improvements.rs index b06ec438146..43a13b02d0a 100644 --- a/src/test/rustdoc-ui/url-improvements.rs +++ b/src/test/rustdoc-ui/url-improvements.rs @@ -1,4 +1,4 @@ -#![deny(rustdoc::non_autolinks)] +#![deny(rustdoc::bare_urls)] /// https://somewhere.com //~^ ERROR this URL is not a hyperlink @@ -51,7 +51,7 @@ pub fn c() {} /// [should_not.lint](should_not.lint) pub fn everything_is_fine_here() {} -#[allow(rustdoc::non_autolinks)] +#[allow(rustdoc::bare_urls)] pub mod foo { /// https://somewhere.com/a?hello=12&bye=11#xyz pub fn bar() {} diff --git a/src/test/rustdoc-ui/url-improvements.stderr b/src/test/rustdoc-ui/url-improvements.stderr index 404494d6802..3d5ebd8be6b 100644 --- a/src/test/rustdoc-ui/url-improvements.stderr +++ b/src/test/rustdoc-ui/url-improvements.stderr @@ -7,8 +7,8 @@ LL | /// https://somewhere.com note: the lint level is defined here --> $DIR/url-improvements.rs:1:9 | -LL | #![deny(rustdoc::non_autolinks)] - | ^^^^^^^^^^^^^^^^^^^^^^ +LL | #![deny(rustdoc::bare_urls)] + | ^^^^^^^^^^^^^^^^^^ error: this URL is not a hyperlink --> $DIR/url-improvements.rs:5:5 -- cgit 1.4.1-3-g733a5