Skip to content

Commit 3a37c95

Browse files
committed
rustdoc: remove tuple link on round braces
This is 682889f but for tuples. The reasoning is the same: * This commit also changes it so that tuples with all-generic elements still link to the primitive.tuple.html page, just like slices. So there still plenty of on-ramps for anybody who doesn't know about it. * It's too hard to see when round braces are a separate link from the type inside of them. * It's too hard to click even if you do notice them.
1 parent 872503d commit 3a37c95

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

src/librustdoc/html/format.rs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ use rustc_span::symbol::kw;
2323
use rustc_span::{sym, Symbol};
2424
use rustc_target::spec::abi::Abi;
2525

26+
use itertools::Itertools;
27+
2628
use crate::clean::{
2729
self, types::ExternalLocation, utils::find_nearest_parent_module, ExternalCrate, ItemId,
2830
PrimitiveType,
@@ -864,20 +866,42 @@ fn fmt_type<'cx>(
864866
match &typs[..] {
865867
&[] => primitive_link(f, PrimitiveType::Unit, "()", cx),
866868
&[ref one] => {
867-
primitive_link(f, PrimitiveType::Tuple, "(", cx)?;
868-
// Carry `f.alternate()` into this display w/o branching manually.
869-
fmt::Display::fmt(&one.print(cx), f)?;
870-
primitive_link(f, PrimitiveType::Tuple, ",)", cx)
869+
if let clean::Generic(name) = one {
870+
primitive_link(f, PrimitiveType::Tuple, &format!("({name},)"), cx)
871+
} else {
872+
write!(f, "(")?;
873+
// Carry `f.alternate()` into this display w/o branching manually.
874+
fmt::Display::fmt(&one.print(cx), f)?;
875+
write!(f, ",)")
876+
}
871877
}
872878
many => {
873-
primitive_link(f, PrimitiveType::Tuple, "(", cx)?;
874-
for (i, item) in many.iter().enumerate() {
875-
if i != 0 {
876-
write!(f, ", ")?;
879+
let generic_names: Vec<Symbol> = many
880+
.iter()
881+
.filter_map(|t| match t {
882+
clean::Generic(name) => Some(*name),
883+
_ => None,
884+
})
885+
.collect();
886+
let is_generic = generic_names.len() == many.len();
887+
if is_generic {
888+
primitive_link(
889+
f,
890+
PrimitiveType::Tuple,
891+
&format!("({})", generic_names.iter().map(|s| s.as_str()).join(", ")),
892+
cx,
893+
)
894+
} else {
895+
write!(f, "(")?;
896+
for (i, item) in many.iter().enumerate() {
897+
if i != 0 {
898+
write!(f, ", ")?;
899+
}
900+
// Carry `f.alternate()` into this display w/o branching manually.
901+
fmt::Display::fmt(&item.print(cx), f)?;
877902
}
878-
fmt::Display::fmt(&item.print(cx), f)?;
903+
write!(f, ")")
879904
}
880-
primitive_link(f, PrimitiveType::Tuple, ")", cx)
881905
}
882906
}
883907
}

0 commit comments

Comments
 (0)