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

Correctly handle links starting with whitespace #108129

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_resolve/src/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ pub fn inner_docs(attrs: &[ast::Attribute]) -> bool {
fn preprocess_link(link: &str) -> String {
let link = link.replace('`', "");
let link = link.split('#').next().unwrap();
let link = link.trim();
let link = link.rsplit('@').next().unwrap();
let link = link.strip_suffix("()").unwrap_or(link);
let link = link.strip_suffix("{}").unwrap_or(link);
Expand Down
5 changes: 3 additions & 2 deletions src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,8 @@ fn preprocess_link(
let mut parts = stripped.split('#');

let link = parts.next().unwrap();
if link.trim().is_empty() {
let link = link.trim();
if link.is_empty() {
// This is an anchor to an element of the current page, nothing to do in here!
return None;
}
Expand All @@ -897,7 +898,7 @@ fn preprocess_link(
// Parse and strip the disambiguator from the link, if present.
let (disambiguator, path_str, link_text) = match Disambiguator::from_str(link) {
Ok(Some((d, path, link_text))) => (Some(d), path.trim(), link_text.trim()),
Ok(None) => (None, link.trim(), link.trim()),
Ok(None) => (None, link, link),
petrochenkov marked this conversation as resolved.
Show resolved Hide resolved
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) {
Expand Down
28 changes: 28 additions & 0 deletions tests/rustdoc/issue-107995.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Regression test for <https://github.com/rust-lang/rust/issues/107995>.

#![crate_name = "foo"]

// @has 'foo/fn.foo.html'
// @has - '//*[@class="docblock"]//a[@href="fn.bar.html"]' 'bar`'
/// A foo, see also [ bar`]
pub fn foo() {}

// @has 'foo/fn.bar.html'
// @has - '//*[@class="docblock"]' 'line Path line'
// @has - '//*[@class="docblock"]//a[@href="struct.Path.html"]' 'Path'
#[doc = "line ["]
#[doc = "Path"]
#[doc = "] line"]
pub fn bar() {}

// @has 'foo/fn.another.html'
// @has - '//*[@class="docblock"]//a[@href="struct.Path.html"]' 'Path'
/// [ `Path`]
pub fn another() {}

// @has 'foo/fn.last.html'
// @has - '//*[@class="docblock"]//a[@href="struct.Path.html"]' 'Path'
/// [ Path`]
pub fn last() {}

pub struct Path;