Skip to content

Commit bd63edc

Browse files
Rollup merge of #108129 - GuillaumeGomez:correctly-handle-links-starting-with-whitespace, r=petrochenkov
Correctly handle links starting with whitespace Part of #107995. I just got this issue, wrote a fix and then saw the issue. So here's the PR. ^^' r? `@petrochenkov`
2 parents 49d7ed1 + 8d801fd commit bd63edc

File tree

3 files changed

+32
-2
lines changed

3 files changed

+32
-2
lines changed

compiler/rustc_resolve/src/rustdoc.rs

+1
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ pub fn inner_docs(attrs: &[ast::Attribute]) -> bool {
339339
fn preprocess_link(link: &str) -> String {
340340
let link = link.replace('`', "");
341341
let link = link.split('#').next().unwrap();
342+
let link = link.trim();
342343
let link = link.rsplit('@').next().unwrap();
343344
let link = link.strip_suffix("()").unwrap_or(link);
344345
let link = link.strip_suffix("{}").unwrap_or(link);

src/librustdoc/passes/collect_intra_doc_links.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,8 @@ fn preprocess_link(
884884
let mut parts = stripped.split('#');
885885

886886
let link = parts.next().unwrap();
887-
if link.trim().is_empty() {
887+
let link = link.trim();
888+
if link.is_empty() {
888889
// This is an anchor to an element of the current page, nothing to do in here!
889890
return None;
890891
}
@@ -897,7 +898,7 @@ fn preprocess_link(
897898
// Parse and strip the disambiguator from the link, if present.
898899
let (disambiguator, path_str, link_text) = match Disambiguator::from_str(link) {
899900
Ok(Some((d, path, link_text))) => (Some(d), path.trim(), link_text.trim()),
900-
Ok(None) => (None, link.trim(), link.trim()),
901+
Ok(None) => (None, link, link),
901902
Err((err_msg, relative_range)) => {
902903
// Only report error if we would not have ignored this link. See issue #83859.
903904
if !should_ignore_link_with_disambiguators(link) {

tests/rustdoc/issue-107995.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Regression test for <https://github.com/rust-lang/rust/issues/107995>.
2+
3+
#![crate_name = "foo"]
4+
5+
// @has 'foo/fn.foo.html'
6+
// @has - '//*[@class="docblock"]//a[@href="fn.bar.html"]' 'bar`'
7+
/// A foo, see also [ bar`]
8+
pub fn foo() {}
9+
10+
// @has 'foo/fn.bar.html'
11+
// @has - '//*[@class="docblock"]' 'line Path line'
12+
// @has - '//*[@class="docblock"]//a[@href="struct.Path.html"]' 'Path'
13+
#[doc = "line ["]
14+
#[doc = "Path"]
15+
#[doc = "] line"]
16+
pub fn bar() {}
17+
18+
// @has 'foo/fn.another.html'
19+
// @has - '//*[@class="docblock"]//a[@href="struct.Path.html"]' 'Path'
20+
/// [ `Path`]
21+
pub fn another() {}
22+
23+
// @has 'foo/fn.last.html'
24+
// @has - '//*[@class="docblock"]//a[@href="struct.Path.html"]' 'Path'
25+
/// [ Path`]
26+
pub fn last() {}
27+
28+
pub struct Path;

0 commit comments

Comments
 (0)