Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get rid of some false negatives in rustdoc::broken_intra_doc_links #132748

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
13 changes: 10 additions & 3 deletions src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -930,13 +930,20 @@ fn preprocess_link(
ori_link: &MarkdownLink,
dox: &str,
) -> Option<Result<PreprocessingInfo, PreprocessingError>> {
// certain link kinds cannot have their path be urls,
// so they should not be ignored, no matter how much they look like urls.
// e.g. [https://example.com/] is not a link to example.com.
let can_be_url = ori_link.kind != LinkType::ShortcutUnknown
&& ori_link.kind != LinkType::CollapsedUnknown
&& ori_link.kind != LinkType::ReferenceUnknown;

// [] is mostly likely not supposed to be a link
if ori_link.link.is_empty() {
return None;
}

// Bail early for real links.
if ori_link.link.contains('/') {
if can_be_url && ori_link.link.contains('/') {
return None;
}

Expand All @@ -961,7 +968,7 @@ fn preprocess_link(
Ok(None) => (None, link, link),
Err((err_msg, relative_range)) => {
// Only report error if we would not have ignored this link. See issue #83859.
if !should_ignore_link_with_disambiguators(link) {
if !(can_be_url && should_ignore_link_with_disambiguators(link)) {
let disambiguator_range = match range_between_backticks(&ori_link.range, dox) {
MarkdownLinkRange::Destination(no_backticks_range) => {
MarkdownLinkRange::Destination(
Expand All @@ -978,7 +985,7 @@ fn preprocess_link(
}
};

if should_ignore_link(path_str) {
if can_be_url && should_ignore_link(path_str) {
return None;
}

Expand Down
13 changes: 13 additions & 0 deletions tests/rustdoc-ui/bad-intra-doc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![no_std]
#![deny(rustdoc::broken_intra_doc_links)]

// regression test for https://github.com/rust-lang/rust/issues/54191

/// this is not a link to [`example.com`] //~ERROR unresolved link
///
/// this link [`has spaces in it`].
///
/// attempted link to method: [`Foo.bar()`] //~ERROR unresolved link
///
/// classic broken intra-doc link: [`Bar`] //~ERROR unresolved link
pub struct Foo;
31 changes: 31 additions & 0 deletions tests/rustdoc-ui/bad-intra-doc.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
error: unresolved link to `example.com`
--> $DIR/bad-intra-doc.rs:6:29
|
LL | /// this is not a link to [`example.com`]
| ^^^^^^^^^^^ no item named `example.com` in scope
|
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
note: the lint level is defined here
--> $DIR/bad-intra-doc.rs:2:9
|
LL | #![deny(rustdoc::broken_intra_doc_links)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: unresolved link to `Foo.bar`
--> $DIR/bad-intra-doc.rs:10:33
|
LL | /// attempted link to method: [`Foo.bar()`]
| ^^^^^^^^^ no item named `Foo.bar` in scope
|
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`

error: unresolved link to `Bar`
--> $DIR/bad-intra-doc.rs:12:38
|
LL | /// classic broken intra-doc link: [`Bar`]
| ^^^ no item named `Bar` in scope
|
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`

error: aborting due to 3 previous errors

30 changes: 15 additions & 15 deletions tests/rustdoc-ui/disambiguator-endswith-named-suffix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,67 @@
//@ normalize-stderr-test: "nightly|beta|1\.[0-9][0-9]\.[0-9]" -> "$$CHANNEL"

//! [struct@m!()] //~ WARN: unmatched disambiguator `struct` and suffix `!()`
//! [struct@m!{}]
//! [struct@m!{}] //~ WARN: unmatched disambiguator `struct` and suffix `!{}`
//! [struct@m![]]
//! [struct@f()] //~ WARN: unmatched disambiguator `struct` and suffix `()`
//! [struct@m!] //~ WARN: unmatched disambiguator `struct` and suffix `!`
//!
//! [enum@m!()] //~ WARN: unmatched disambiguator `enum` and suffix `!()`
//! [enum@m!{}]
//! [enum@m!{}] //~ WARN: unmatched disambiguator `enum` and suffix `!{}`
//! [enum@m![]]
//! [enum@f()] //~ WARN: unmatched disambiguator `enum` and suffix `()`
//! [enum@m!] //~ WARN: unmatched disambiguator `enum` and suffix `!`
//!
//! [trait@m!()] //~ WARN: unmatched disambiguator `trait` and suffix `!()`
//! [trait@m!{}]
//! [trait@m!{}] //~ WARN: unmatched disambiguator `trait` and suffix `!{}`
//! [trait@m![]]
//! [trait@f()] //~ WARN: unmatched disambiguator `trait` and suffix `()`
//! [trait@m!] //~ WARN: unmatched disambiguator `trait` and suffix `!`
//!
//! [module@m!()] //~ WARN: unmatched disambiguator `module` and suffix `!()`
//! [module@m!{}]
//! [module@m!{}] //~ WARN: unmatched disambiguator `module` and suffix `!{}`
//! [module@m![]]
//! [module@f()] //~ WARN: unmatched disambiguator `module` and suffix `()`
//! [module@m!] //~ WARN: unmatched disambiguator `module` and suffix `!`
//!
//! [mod@m!()] //~ WARN: unmatched disambiguator `mod` and suffix `!()`
//! [mod@m!{}]
//! [mod@m!{}] //~ WARN: unmatched disambiguator `mod` and suffix `!{}`
//! [mod@m![]]
//! [mod@f()] //~ WARN: unmatched disambiguator `mod` and suffix `()`
//! [mod@m!] //~ WARN: unmatched disambiguator `mod` and suffix `!`
//!
//! [const@m!()] //~ WARN: unmatched disambiguator `const` and suffix `!()`
//! [const@m!{}]
//! [const@m!{}] //~ WARN: unmatched disambiguator `const` and suffix `!{}`
//! [const@m![]]
//! [const@f()] //~ WARN: incompatible link kind for `f`
//! [const@m!] //~ WARN: unmatched disambiguator `const` and suffix `!`
//!
//! [constant@m!()] //~ WARN: unmatched disambiguator `constant` and suffix `!()`
//! [constant@m!{}]
//! [constant@m!{}] //~ WARN: unmatched disambiguator `constant` and suffix `!{}`
//! [constant@m![]]
//! [constant@f()] //~ WARN: incompatible link kind for `f`
//! [constant@m!] //~ WARN: unmatched disambiguator `constant` and suffix `!`
//!
//! [static@m!()] //~ WARN: unmatched disambiguator `static` and suffix `!()`
//! [static@m!{}]
//! [static@m!{}] //~ WARN: unmatched disambiguator `static` and suffix `!{}`
//! [static@m![]]
//! [static@f()] //~ WARN: incompatible link kind for `f`
//! [static@m!] //~ WARN: unmatched disambiguator `static` and suffix `!`
//!
//! [function@m!()] //~ WARN: unmatched disambiguator `function` and suffix `!()`
//! [function@m!{}]
//! [function@m!{}] //~ WARN: unmatched disambiguator `function` and suffix `!{}`
//! [function@m![]]
//! [function@f()]
//! [function@m!] //~ WARN: unmatched disambiguator `function` and suffix `!`
//!
//! [fn@m!()] //~ WARN: unmatched disambiguator `fn` and suffix `!()`
//! [fn@m!{}]
//! [fn@m!{}] //~ WARN: unmatched disambiguator `fn` and suffix `!{}`
//! [fn@m![]]
//! [fn@f()]
//! [fn@m!] //~ WARN: unmatched disambiguator `fn` and suffix `!`
//!
//! [method@m!()] //~ WARN: unmatched disambiguator `method` and suffix `!()`
//! [method@m!{}]
//! [method@m!{}] //~ WARN: unmatched disambiguator `method` and suffix `!{}`
//! [method@m![]]
//! [method@f()]
//! [method@m!] //~ WARN: unmatched disambiguator `method` and suffix `!`
Expand All @@ -74,13 +74,13 @@
//! [derive@m!] //~ WARN: incompatible link kind for `m`
//!
//! [type@m!()] //~ WARN: unmatched disambiguator `type` and suffix `!()`
//! [type@m!{}]
//! [type@m!{}] //~ WARN: unmatched disambiguator `type` and suffix `!{}`
//! [type@m![]]
//! [type@f()] //~ WARN: unmatched disambiguator `type` and suffix `()`
//! [type@m!] //~ WARN: unmatched disambiguator `type` and suffix `!`
//!
//! [value@m!()] //~ WARN: unmatched disambiguator `value` and suffix `!()`
//! [value@m!{}]
//! [value@m!{}] //~ WARN: unmatched disambiguator `value` and suffix `!{}`
//! [value@m![]]
//! [value@f()]
//! [value@m!] //~ WARN: unmatched disambiguator `value` and suffix `!`
Expand All @@ -92,13 +92,13 @@
//! [macro@m!]
//!
//! [prim@m!()] //~ WARN: unmatched disambiguator `prim` and suffix `!()`
//! [prim@m!{}]
//! [prim@m!{}] //~ WARN: unmatched disambiguator `prim` and suffix `!{}`
//! [prim@m![]]
//! [prim@f()] //~ WARN: unmatched disambiguator `prim` and suffix `()`
//! [prim@m!] //~ WARN: unmatched disambiguator `prim` and suffix `!`
//!
//! [primitive@m!()] //~ WARN: unmatched disambiguator `primitive` and suffix `!()`
//! [primitive@m!{}]
//! [primitive@m!{}] //~ WARN: unmatched disambiguator `primitive` and suffix `!{}`
//! [primitive@m![]]
//! [primitive@f()] //~ WARN: unmatched disambiguator `primitive` and suffix `()`
//! [primitive@m!] //~ WARN: unmatched disambiguator `primitive` and suffix `!`
Expand Down
Loading
Loading