Skip to content

Make some matches slightly more ergonomic in librustdoc #141131

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

Merged
merged 1 commit into from
May 17, 2025
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
64 changes: 29 additions & 35 deletions src/librustdoc/clean/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,15 @@ impl Cfg {

/// Whether the configuration consists of just `Cfg` or `Not`.
fn is_simple(&self) -> bool {
match *self {
match self {
Cfg::False | Cfg::True | Cfg::Cfg(..) | Cfg::Not(..) => true,
Cfg::All(..) | Cfg::Any(..) => false,
}
}

/// Whether the configuration consists of just `Cfg`, `Not` or `All`.
fn is_all(&self) -> bool {
match *self {
match self {
Cfg::False | Cfg::True | Cfg::Cfg(..) | Cfg::Not(..) | Cfg::All(..) => true,
Cfg::Any(..) => false,
}
Expand Down Expand Up @@ -204,7 +204,7 @@ impl Cfg {
}

fn should_append_only_to_description(&self) -> bool {
match *self {
match self {
Cfg::False | Cfg::True => false,
Cfg::Any(..) | Cfg::All(..) | Cfg::Cfg(..) => true,
Cfg::Not(box Cfg::Cfg(..)) => true,
Expand Down Expand Up @@ -261,17 +261,17 @@ impl ops::Not for Cfg {
impl ops::BitAndAssign for Cfg {
fn bitand_assign(&mut self, other: Cfg) {
match (self, other) {
(&mut Cfg::False, _) | (_, Cfg::True) => {}
(Cfg::False, _) | (_, Cfg::True) => {}
(s, Cfg::False) => *s = Cfg::False,
(s @ &mut Cfg::True, b) => *s = b,
(&mut Cfg::All(ref mut a), Cfg::All(ref mut b)) => {
(s @ Cfg::True, b) => *s = b,
(Cfg::All(a), Cfg::All(ref mut b)) => {
for c in b.drain(..) {
if !a.contains(&c) {
a.push(c);
}
}
}
(&mut Cfg::All(ref mut a), ref mut b) => {
(Cfg::All(a), ref mut b) => {
if !a.contains(b) {
a.push(mem::replace(b, Cfg::True));
}
Expand Down Expand Up @@ -305,15 +305,15 @@ impl ops::BitOrAssign for Cfg {
fn bitor_assign(&mut self, other: Cfg) {
match (self, other) {
(Cfg::True, _) | (_, Cfg::False) | (_, Cfg::True) => {}
(s @ &mut Cfg::False, b) => *s = b,
(&mut Cfg::Any(ref mut a), Cfg::Any(ref mut b)) => {
(s @ Cfg::False, b) => *s = b,
(Cfg::Any(a), Cfg::Any(ref mut b)) => {
for c in b.drain(..) {
if !a.contains(&c) {
a.push(c);
}
}
}
(&mut Cfg::Any(ref mut a), ref mut b) => {
(Cfg::Any(a), ref mut b) => {
if !a.contains(b) {
a.push(mem::replace(b, Cfg::True));
}
Expand Down Expand Up @@ -440,40 +440,34 @@ impl Display<'_> {

impl fmt::Display for Display<'_> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self.0 {
Cfg::Not(ref child) => match **child {
Cfg::Any(ref sub_cfgs) => {
let separator =
if sub_cfgs.iter().all(Cfg::is_simple) { " nor " } else { ", nor " };
fmt.write_str("neither ")?;

sub_cfgs
.iter()
.map(|sub_cfg| {
fmt::from_fn(|fmt| {
write_with_opt_paren(
fmt,
!sub_cfg.is_all(),
Display(sub_cfg, self.1),
)
})
match self.0 {
Cfg::Not(box Cfg::Any(sub_cfgs)) => {
let separator =
if sub_cfgs.iter().all(Cfg::is_simple) { " nor " } else { ", nor " };
fmt.write_str("neither ")?;

sub_cfgs
.iter()
.map(|sub_cfg| {
fmt::from_fn(|fmt| {
write_with_opt_paren(fmt, !sub_cfg.is_all(), Display(sub_cfg, self.1))
})
.joined(separator, fmt)
}
ref simple @ Cfg::Cfg(..) => write!(fmt, "non-{}", Display(simple, self.1)),
ref c => write!(fmt, "not ({})", Display(c, self.1)),
},
})
.joined(separator, fmt)
}
Cfg::Not(box simple @ Cfg::Cfg(..)) => write!(fmt, "non-{}", Display(simple, self.1)),
Cfg::Not(box c) => write!(fmt, "not ({})", Display(c, self.1)),

Cfg::Any(ref sub_cfgs) => {
Cfg::Any(sub_cfgs) => {
let separator = if sub_cfgs.iter().all(Cfg::is_simple) { " or " } else { ", or " };
self.display_sub_cfgs(fmt, sub_cfgs, separator)
}
Cfg::All(ref sub_cfgs) => self.display_sub_cfgs(fmt, sub_cfgs, " and "),
Cfg::All(sub_cfgs) => self.display_sub_cfgs(fmt, sub_cfgs, " and "),

Cfg::True => fmt.write_str("everywhere"),
Cfg::False => fmt.write_str("nowhere"),

Cfg::Cfg(name, value) => {
&Cfg::Cfg(name, value) => {
let human_readable = match (name, value) {
(sym::unix, None) => "Unix",
(sym::windows, None) => "Windows",
Expand Down
22 changes: 11 additions & 11 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,9 @@ fn clean_generic_bound<'tcx>(
bound: &hir::GenericBound<'tcx>,
cx: &mut DocContext<'tcx>,
) -> Option<GenericBound> {
Some(match *bound {
Some(match bound {
hir::GenericBound::Outlives(lt) => GenericBound::Outlives(clean_lifetime(lt, cx)),
hir::GenericBound::Trait(ref t) => {
hir::GenericBound::Trait(t) => {
// `T: ~const Destruct` is hidden because `T: Destruct` is a no-op.
if let hir::BoundConstness::Maybe(_) = t.modifiers.constness
&& cx.tcx.lang_items().destruct_trait() == Some(t.trait_ref.trait_def_id().unwrap())
Expand Down Expand Up @@ -352,8 +352,8 @@ fn clean_where_predicate<'tcx>(
if !predicate.kind.in_where_clause() {
return None;
}
Some(match *predicate.kind {
hir::WherePredicateKind::BoundPredicate(ref wbp) => {
Some(match predicate.kind {
hir::WherePredicateKind::BoundPredicate(wbp) => {
let bound_params = wbp
.bound_generic_params
.iter()
Expand All @@ -366,12 +366,12 @@ fn clean_where_predicate<'tcx>(
}
}

hir::WherePredicateKind::RegionPredicate(ref wrp) => WherePredicate::RegionPredicate {
hir::WherePredicateKind::RegionPredicate(wrp) => WherePredicate::RegionPredicate {
lifetime: clean_lifetime(wrp.lifetime, cx),
bounds: wrp.bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(),
},

hir::WherePredicateKind::EqPredicate(ref wrp) => WherePredicate::EqPredicate {
hir::WherePredicateKind::EqPredicate(wrp) => WherePredicate::EqPredicate {
lhs: clean_ty(wrp.lhs_ty, cx),
rhs: clean_ty(wrp.rhs_ty, cx).into(),
},
Expand Down Expand Up @@ -2112,7 +2112,7 @@ pub(crate) fn clean_middle_ty<'tcx>(
);
Type::Path { path }
}
ty::Dynamic(obj, ref reg, _) => {
ty::Dynamic(obj, reg, _) => {
// HACK: pick the first `did` as the `did` of the trait object. Someone
// might want to implement "native" support for marker-trait-only
// trait objects.
Expand All @@ -2129,7 +2129,7 @@ pub(crate) fn clean_middle_ty<'tcx>(

inline::record_extern_fqn(cx, did, ItemType::Trait);

let lifetime = clean_trait_object_lifetime_bound(*reg, container, obj, cx.tcx);
let lifetime = clean_trait_object_lifetime_bound(reg, container, obj, cx.tcx);

let mut bounds = dids
.map(|did| {
Expand Down Expand Up @@ -2846,19 +2846,19 @@ fn clean_maybe_renamed_item<'tcx>(
));
return ret;
}
ItemKind::Enum(_, ref def, generics) => EnumItem(Enum {
ItemKind::Enum(_, def, generics) => EnumItem(Enum {
variants: def.variants.iter().map(|v| clean_variant(v, cx)).collect(),
generics: clean_generics(generics, cx),
}),
ItemKind::TraitAlias(_, generics, bounds) => TraitAliasItem(TraitAlias {
generics: clean_generics(generics, cx),
bounds: bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(),
}),
ItemKind::Union(_, ref variant_data, generics) => UnionItem(Union {
ItemKind::Union(_, variant_data, generics) => UnionItem(Union {
generics: clean_generics(generics, cx),
fields: variant_data.fields().iter().map(|x| clean_field(x, cx)).collect(),
}),
ItemKind::Struct(_, ref variant_data, generics) => StructItem(Struct {
ItemKind::Struct(_, variant_data, generics) => StructItem(Struct {
ctor_kind: variant_data.ctor_kind(),
generics: clean_generics(generics, cx),
fields: variant_data.fields().iter().map(|x| clean_field(x, cx)).collect(),
Expand Down
20 changes: 10 additions & 10 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1337,9 +1337,9 @@ pub(crate) enum WherePredicate {

impl WherePredicate {
pub(crate) fn get_bounds(&self) -> Option<&[GenericBound]> {
match *self {
WherePredicate::BoundPredicate { ref bounds, .. } => Some(bounds),
WherePredicate::RegionPredicate { ref bounds, .. } => Some(bounds),
match self {
WherePredicate::BoundPredicate { bounds, .. } => Some(bounds),
WherePredicate::RegionPredicate { bounds, .. } => Some(bounds),
_ => None,
}
}
Expand Down Expand Up @@ -1709,13 +1709,13 @@ impl Type {
///
/// [clean]: crate::clean
pub(crate) fn def_id(&self, cache: &Cache) -> Option<DefId> {
let t: PrimitiveType = match *self {
Type::Path { ref path } => return Some(path.def_id()),
DynTrait(ref bounds, _) => return bounds.first().map(|b| b.trait_.def_id()),
Primitive(p) => return cache.primitive_locations.get(&p).cloned(),
let t: PrimitiveType = match self {
Type::Path { path } => return Some(path.def_id()),
DynTrait(bounds, _) => return bounds.first().map(|b| b.trait_.def_id()),
Primitive(p) => return cache.primitive_locations.get(p).cloned(),
BorrowedRef { type_: box Generic(..), .. } => PrimitiveType::Reference,
BorrowedRef { ref type_, .. } => return type_.def_id(cache),
Tuple(ref tys) => {
BorrowedRef { type_, .. } => return type_.def_id(cache),
Tuple(tys) => {
if tys.is_empty() {
PrimitiveType::Unit
} else {
Expand All @@ -1727,7 +1727,7 @@ impl Type {
Array(..) => PrimitiveType::Array,
Type::Pat(..) => PrimitiveType::Pat,
RawPointer(..) => PrimitiveType::RawPointer,
QPath(box QPathData { ref self_type, .. }) => return self_type.def_id(cache),
QPath(box QPathData { self_type, .. }) => return self_type.def_id(cache),
Generic(_) | SelfTy | Infer | ImplTrait(_) | UnsafeBinder(_) => return None,
};
Primitive(t).def_id(cache)
Expand Down
33 changes: 13 additions & 20 deletions src/librustdoc/formats/item_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ impl Serialize for ItemType {

impl<'a> From<&'a clean::Item> for ItemType {
fn from(item: &'a clean::Item) -> ItemType {
let kind = match item.kind {
clean::StrippedItem(box ref item) => item,
ref kind => kind,
let kind = match &item.kind {
clean::StrippedItem(box item) => item,
kind => kind,
};

match *kind {
match kind {
clean::ModuleItem(..) => ItemType::Module,
clean::ExternCrateItem { .. } => ItemType::ExternCrate,
clean::ImportItem(..) => ItemType::Import,
Expand Down Expand Up @@ -103,7 +103,7 @@ impl<'a> From<&'a clean::Item> for ItemType {
clean::ForeignTypeItem => ItemType::ForeignType,
clean::KeywordItem => ItemType::Keyword,
clean::TraitAliasItem(..) => ItemType::TraitAlias,
clean::ProcMacroItem(ref mac) => match mac.kind {
clean::ProcMacroItem(mac) => match mac.kind {
MacroKind::Bang => ItemType::Macro,
MacroKind::Attr => ItemType::ProcAttribute,
MacroKind::Derive => ItemType::ProcDerive,
Expand Down Expand Up @@ -134,22 +134,15 @@ impl ItemType {
DefKind::Trait => Self::Trait,
DefKind::TyAlias => Self::TypeAlias,
DefKind::TraitAlias => Self::TraitAlias,
DefKind::Macro(kind) => match kind {
MacroKind::Bang => ItemType::Macro,
MacroKind::Attr => ItemType::ProcAttribute,
MacroKind::Derive => ItemType::ProcDerive,
},
DefKind::Macro(MacroKind::Bang) => ItemType::Macro,
DefKind::Macro(MacroKind::Attr) => ItemType::ProcAttribute,
DefKind::Macro(MacroKind::Derive) => ItemType::ProcDerive,
DefKind::ForeignTy => Self::ForeignType,
DefKind::Variant => Self::Variant,
DefKind::Field => Self::StructField,
DefKind::AssocTy => Self::AssocType,
DefKind::AssocFn => {
if let Some(DefKind::Trait) = parent_kind {
Self::TyMethod
} else {
Self::Method
}
}
DefKind::AssocFn if let Some(DefKind::Trait) = parent_kind => Self::TyMethod,
DefKind::AssocFn => Self::Method,
DefKind::Ctor(CtorOf::Struct, _) => Self::Struct,
DefKind::Ctor(CtorOf::Variant, _) => Self::Variant,
DefKind::AssocConst => Self::AssocConst,
Expand All @@ -170,7 +163,7 @@ impl ItemType {
}

pub(crate) fn as_str(&self) -> &'static str {
match *self {
match self {
ItemType::Module => "mod",
ItemType::ExternCrate => "externcrate",
ItemType::Import => "import",
Expand Down Expand Up @@ -199,10 +192,10 @@ impl ItemType {
}
}
pub(crate) fn is_method(&self) -> bool {
matches!(*self, ItemType::Method | ItemType::TyMethod)
matches!(self, ItemType::Method | ItemType::TyMethod)
}
pub(crate) fn is_adt(&self) -> bool {
matches!(*self, ItemType::Struct | ItemType::Union | ItemType::Enum)
matches!(self, ItemType::Struct | ItemType::Union | ItemType::Enum)
}
}

Expand Down
Loading
Loading