Auto merge of #131368 - GuillaumeGomez:rustdoc-dead-code, r=notriddle

[rustdoc] Remove intra-doc links dead code

While working on https://github.com/rust-lang/rust/pull/130278, I wondered what `resolve_display_text` was doing. I removed it and ran all rustdoc tests, and nothing failed. Are some intra-doc links tests missing or is it really dead code? Couldn't figure it out.

r? `@notriddle`
This commit is contained in:
bors 2024-10-08 10:07:44 +00:00
commit 6a3c45e1c6
2 changed files with 1 additions and 115 deletions

View File

@ -35,8 +35,7 @@ use std::str::{self, CharIndices};
use std::sync::OnceLock; use std::sync::OnceLock;
use pulldown_cmark::{ use pulldown_cmark::{
BrokenLink, BrokenLinkCallback, CodeBlockKind, CowStr, Event, LinkType, OffsetIter, Options, BrokenLink, CodeBlockKind, CowStr, Event, LinkType, Options, Parser, Tag, TagEnd, html,
Parser, Tag, TagEnd, html,
}; };
use rustc_data_structures::fx::{FxHashMap, FxIndexMap}; use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
use rustc_errors::{Diag, DiagMessage}; use rustc_errors::{Diag, DiagMessage};
@ -1686,7 +1685,6 @@ pub(crate) fn html_text_from_events<'a>(
pub(crate) struct MarkdownLink { pub(crate) struct MarkdownLink {
pub kind: LinkType, pub kind: LinkType,
pub link: String, pub link: String,
pub display_text: Option<String>,
pub range: MarkdownLinkRange, pub range: MarkdownLinkRange,
} }
@ -1848,23 +1846,9 @@ pub(crate) fn markdown_links<'md, R>(
LinkType::Autolink | LinkType::Email => unreachable!(), LinkType::Autolink | LinkType::Email => unreachable!(),
}; };
let display_text = if matches!(
link_type,
LinkType::Inline
| LinkType::ReferenceUnknown
| LinkType::Reference
| LinkType::Shortcut
| LinkType::ShortcutUnknown
) {
collect_link_data(&mut event_iter)
} else {
None
};
if let Some(link) = preprocess_link(MarkdownLink { if let Some(link) = preprocess_link(MarkdownLink {
kind: link_type, kind: link_type,
link: dest_url.into_string(), link: dest_url.into_string(),
display_text,
range, range,
}) { }) {
links.push(link); links.push(link);
@ -1877,37 +1861,6 @@ pub(crate) fn markdown_links<'md, R>(
links links
} }
/// Collects additional data of link.
fn collect_link_data<'input, F: BrokenLinkCallback<'input>>(
event_iter: &mut OffsetIter<'input, F>,
) -> Option<String> {
let mut display_text: Option<String> = None;
let mut append_text = |text: CowStr<'_>| {
if let Some(display_text) = &mut display_text {
display_text.push_str(&text);
} else {
display_text = Some(text.to_string());
}
};
while let Some((event, _span)) = event_iter.next() {
match event {
Event::Text(text) => {
append_text(text);
}
Event::Code(code) => {
append_text(code);
}
Event::End(_) => {
break;
}
_ => {}
}
}
display_text
}
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct RustCodeBlock { pub(crate) struct RustCodeBlock {
/// The range in the markdown that the code block occupies. Note that this includes the fences /// The range in the markdown that the code block occupies. Note that this includes the fences

View File

@ -1040,21 +1040,6 @@ impl LinkCollector<'_, '_> {
false, false,
)?; )?;
if ori_link.display_text.is_some() {
self.resolve_display_text(
path_str,
ResolutionInfo {
item_id,
module_id,
dis: disambiguator,
path_str: ori_link.display_text.clone()?.into_boxed_str(),
extra_fragment: extra_fragment.clone(),
},
&ori_link,
&diag_info,
);
}
// Check for a primitive which might conflict with a module // Check for a primitive which might conflict with a module
// Report the ambiguity and require that the user specify which one they meant. // Report the ambiguity and require that the user specify which one they meant.
// FIXME: could there ever be a primitive not in the type namespace? // FIXME: could there ever be a primitive not in the type namespace?
@ -1398,58 +1383,6 @@ impl LinkCollector<'_, '_> {
} }
} }
} }
/// Resolve display text if the provided link has separated parts of links.
///
/// For example:
/// Inline link `[display_text](dest_link)` and reference link `[display_text][reference_link]` has
/// separated parts of links.
fn resolve_display_text(
&mut self,
explicit_link: &Box<str>,
display_res_info: ResolutionInfo,
ori_link: &MarkdownLink,
diag_info: &DiagnosticInfo<'_>,
) {
// Check if explicit resolution's path is same as resolution of original link's display text path, see
// tests/rustdoc-ui/lint/redundant_explicit_links.rs for more cases.
//
// To avoid disambiguator from panicking, we check if display text path is possible to be disambiguated
// into explicit path.
if !matches!(
ori_link.kind,
LinkType::Inline | LinkType::Reference | LinkType::ReferenceUnknown
) {
return;
}
// Algorithm to check if display text could possibly be the explicit link:
//
// Consider 2 links which are display text and explicit link, pick the shorter
// one as symbol and longer one as full qualified path, and tries to match symbol
// to the full qualified path's last symbol.
//
// Otherwise, check if 2 links are same, if so, skip the resolve process.
//
// Notice that this algorithm is passive, might possibly miss actual redundant cases.
let explicit_link = explicit_link.to_string();
let display_text = ori_link.display_text.as_ref().unwrap();
if display_text.len() == explicit_link.len() {
// Whether they are same or not, skip the resolve process.
return;
}
if explicit_link.ends_with(&display_text[..]) || display_text.ends_with(&explicit_link[..])
{
self.resolve_with_disambiguator_cached(
display_res_info,
diag_info.clone(), // this struct should really be Copy, but Range is not :(
false,
true,
);
}
}
} }
/// Get the section of a link between the backticks, /// Get the section of a link between the backticks,