Skip to content

Commit ecb2637

Browse files
committed
narrow down the lint trigger constraint
1 parent 78c85f4 commit ecb2637

File tree

5 files changed

+65
-28
lines changed

5 files changed

+65
-28
lines changed

compiler/rustc_resolve/src/rustdoc.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use pulldown_cmark::{BrokenLink, Event, LinkType, Options, Parser, Tag};
1+
use pulldown_cmark::{BrokenLink, CowStr, Event, LinkType, Options, Parser, Tag};
22
use rustc_ast as ast;
33
use rustc_ast::util::comments::beautify_doc_string;
44
use rustc_data_structures::fx::FxHashMap;
@@ -436,15 +436,22 @@ fn parse_links<'md>(doc: &'md str) -> Vec<Box<str>> {
436436
fn collect_link_data<'input, 'callback>(
437437
event_iter: &mut Parser<'input, 'callback>,
438438
) -> Option<Box<str>> {
439-
let mut display_text = None;
439+
let mut display_text: Option<String> = None;
440+
let mut append_text = |text: CowStr<'_>| {
441+
if let Some(display_text) = &mut display_text {
442+
display_text.push_str(&text);
443+
} else {
444+
display_text = Some(text.to_string());
445+
}
446+
};
440447

441448
while let Some(event) = event_iter.next() {
442449
match event {
443-
Event::Text(code) => {
444-
display_text = Some(code.to_string().into_boxed_str());
450+
Event::Text(text) => {
451+
append_text(text);
445452
}
446453
Event::Code(code) => {
447-
display_text = Some(code.to_string().into_boxed_str());
454+
append_text(code);
448455
}
449456
Event::End(_) => {
450457
break;
@@ -453,5 +460,5 @@ fn collect_link_data<'input, 'callback>(
453460
}
454461
}
455462

456-
display_text
463+
display_text.map(String::into_boxed_str)
457464
}

src/librustdoc/html/markdown.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,7 @@ pub(crate) fn plain_text_summary(md: &str, link_names: &[RenderedLink]) -> Strin
12401240
pub(crate) struct MarkdownLink {
12411241
pub kind: LinkType,
12421242
pub link: String,
1243-
pub display_text: String,
1243+
pub display_text: Option<String>,
12441244
pub range: MarkdownLinkRange,
12451245
}
12461246

@@ -1402,13 +1402,23 @@ pub(crate) fn markdown_links<'md, R>(
14021402
LinkType::Autolink | LinkType::Email => unreachable!(),
14031403
};
14041404

1405-
let display_text =
1406-
collect_link_data(&mut event_iter).map_or(String::new(), CowStr::into_string);
1405+
let display_text = if matches!(
1406+
link_type,
1407+
LinkType::Inline
1408+
| LinkType::ReferenceUnknown
1409+
| LinkType::Reference
1410+
| LinkType::Shortcut
1411+
| LinkType::ShortcutUnknown
1412+
) {
1413+
collect_link_data(&mut event_iter)
1414+
} else {
1415+
None
1416+
};
14071417

14081418
if let Some(link) = preprocess_link(MarkdownLink {
14091419
kind: link_type,
1410-
display_text,
14111420
link: dest.into_string(),
1421+
display_text,
14121422
range,
14131423
}) {
14141424
links.push(link);
@@ -1424,16 +1434,23 @@ pub(crate) fn markdown_links<'md, R>(
14241434
/// Collects additional data of link.
14251435
fn collect_link_data<'input, 'callback>(
14261436
event_iter: &mut OffsetIter<'input, 'callback>,
1427-
) -> Option<CowStr<'input>> {
1428-
let mut display_text = None;
1437+
) -> Option<String> {
1438+
let mut display_text: Option<String> = None;
1439+
let mut append_text = |text: CowStr<'_>| {
1440+
if let Some(display_text) = &mut display_text {
1441+
display_text.push_str(&text);
1442+
} else {
1443+
display_text = Some(text.to_string());
1444+
}
1445+
};
14291446

14301447
while let Some((event, _span)) = event_iter.next() {
14311448
match event {
1432-
Event::Text(code) => {
1433-
display_text = Some(code);
1449+
Event::Text(text) => {
1450+
append_text(text);
14341451
}
14351452
Event::Code(code) => {
1436-
display_text = Some(code);
1453+
append_text(code);
14371454
}
14381455
Event::End(_) => {
14391456
break;

src/librustdoc/passes/collect_intra_doc_links.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,18 +1041,20 @@ impl LinkCollector<'_, '_> {
10411041
false,
10421042
)?;
10431043

1044-
self.resolve_display_text(
1045-
path_str,
1046-
ResolutionInfo {
1047-
item_id,
1048-
module_id,
1049-
dis: disambiguator,
1050-
path_str: ori_link.display_text.clone().into_boxed_str(),
1051-
extra_fragment: extra_fragment.clone(),
1052-
},
1053-
&ori_link,
1054-
&diag_info,
1055-
);
1044+
if ori_link.display_text.is_some() {
1045+
self.resolve_display_text(
1046+
path_str,
1047+
ResolutionInfo {
1048+
item_id,
1049+
module_id,
1050+
dis: disambiguator,
1051+
path_str: ori_link.display_text.clone()?.into_boxed_str(),
1052+
extra_fragment: extra_fragment.clone(),
1053+
},
1054+
&ori_link,
1055+
&diag_info,
1056+
);
1057+
}
10561058

10571059
// Check for a primitive which might conflict with a module
10581060
// Report the ambiguity and require that the user specify which one they meant.
@@ -1429,7 +1431,7 @@ impl LinkCollector<'_, '_> {
14291431
//
14301432
// Notice that this algorithm is passive, might possibly miss actual redudant cases.
14311433
let explicit_link = &explicit_link.to_string();
1432-
let display_text = &ori_link.display_text;
1434+
let display_text = ori_link.display_text.as_ref().unwrap();
14331435
let display_len = display_text.len();
14341436
let explicit_len = explicit_link.len();
14351437

src/librustdoc/passes/lint/redundant_explicit_links.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ fn check_redundant_explicit_link<'md>(
6767
Event::Start(Tag::Link(link_type, dest, _)) => {
6868
let link_data = collect_link_data(&mut offset_iter);
6969

70+
if let Some(resolvable_link) = link_data.resolvable_link.as_ref() {
71+
if &link_data.display_link != resolvable_link {
72+
// Skips if display link does not match to actual
73+
// resolvable link, usually happens if display link
74+
// has several segments, e.g.
75+
// [this is just an `Option`](Option)
76+
continue;
77+
}
78+
}
79+
7080
let explicit_link = dest.to_string();
7181
let display_link = link_data.resolvable_link.clone()?;
7282
let explicit_len = explicit_link.len();

tests/rustdoc-ui/lints/no-redundancy.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
#![deny(rustdoc::redundant_explicit_links)]
44

55
/// [Vec][std::vec::Vec#examples] should not warn, because it's not actually redundant!
6+
/// [This is just an `Option`][std::option::Option] has different display content to actual link!
67
pub fn func() {}

0 commit comments

Comments
 (0)