Skip to content

Commit 39dea03

Browse files
committed
Simplify some match ergonomics in librustdoc
1 parent b0e9259 commit 39dea03

File tree

5 files changed

+99
-121
lines changed

5 files changed

+99
-121
lines changed

src/librustdoc/clean/cfg.rs

+29-35
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,15 @@ impl Cfg {
144144

145145
/// Whether the configuration consists of just `Cfg` or `Not`.
146146
fn is_simple(&self) -> bool {
147-
match *self {
147+
match self {
148148
Cfg::False | Cfg::True | Cfg::Cfg(..) | Cfg::Not(..) => true,
149149
Cfg::All(..) | Cfg::Any(..) => false,
150150
}
151151
}
152152

153153
/// Whether the configuration consists of just `Cfg`, `Not` or `All`.
154154
fn is_all(&self) -> bool {
155-
match *self {
155+
match self {
156156
Cfg::False | Cfg::True | Cfg::Cfg(..) | Cfg::Not(..) | Cfg::All(..) => true,
157157
Cfg::Any(..) => false,
158158
}
@@ -204,7 +204,7 @@ impl Cfg {
204204
}
205205

206206
fn should_append_only_to_description(&self) -> bool {
207-
match *self {
207+
match self {
208208
Cfg::False | Cfg::True => false,
209209
Cfg::Any(..) | Cfg::All(..) | Cfg::Cfg(..) => true,
210210
Cfg::Not(box Cfg::Cfg(..)) => true,
@@ -261,17 +261,17 @@ impl ops::Not for Cfg {
261261
impl ops::BitAndAssign for Cfg {
262262
fn bitand_assign(&mut self, other: Cfg) {
263263
match (self, other) {
264-
(&mut Cfg::False, _) | (_, Cfg::True) => {}
264+
(Cfg::False, _) | (_, Cfg::True) => {}
265265
(s, Cfg::False) => *s = Cfg::False,
266-
(s @ &mut Cfg::True, b) => *s = b,
267-
(&mut Cfg::All(ref mut a), Cfg::All(ref mut b)) => {
266+
(s @ Cfg::True, b) => *s = b,
267+
(Cfg::All(a), Cfg::All(ref mut b)) => {
268268
for c in b.drain(..) {
269269
if !a.contains(&c) {
270270
a.push(c);
271271
}
272272
}
273273
}
274-
(&mut Cfg::All(ref mut a), ref mut b) => {
274+
(Cfg::All(a), ref mut b) => {
275275
if !a.contains(b) {
276276
a.push(mem::replace(b, Cfg::True));
277277
}
@@ -305,15 +305,15 @@ impl ops::BitOrAssign for Cfg {
305305
fn bitor_assign(&mut self, other: Cfg) {
306306
match (self, other) {
307307
(Cfg::True, _) | (_, Cfg::False) | (_, Cfg::True) => {}
308-
(s @ &mut Cfg::False, b) => *s = b,
309-
(&mut Cfg::Any(ref mut a), Cfg::Any(ref mut b)) => {
308+
(s @ Cfg::False, b) => *s = b,
309+
(Cfg::Any(a), Cfg::Any(ref mut b)) => {
310310
for c in b.drain(..) {
311311
if !a.contains(&c) {
312312
a.push(c);
313313
}
314314
}
315315
}
316-
(&mut Cfg::Any(ref mut a), ref mut b) => {
316+
(Cfg::Any(a), ref mut b) => {
317317
if !a.contains(b) {
318318
a.push(mem::replace(b, Cfg::True));
319319
}
@@ -440,40 +440,34 @@ impl Display<'_> {
440440

441441
impl fmt::Display for Display<'_> {
442442
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
443-
match *self.0 {
444-
Cfg::Not(ref child) => match **child {
445-
Cfg::Any(ref sub_cfgs) => {
446-
let separator =
447-
if sub_cfgs.iter().all(Cfg::is_simple) { " nor " } else { ", nor " };
448-
fmt.write_str("neither ")?;
449-
450-
sub_cfgs
451-
.iter()
452-
.map(|sub_cfg| {
453-
fmt::from_fn(|fmt| {
454-
write_with_opt_paren(
455-
fmt,
456-
!sub_cfg.is_all(),
457-
Display(sub_cfg, self.1),
458-
)
459-
})
443+
match self.0 {
444+
Cfg::Not(box Cfg::Any(sub_cfgs)) => {
445+
let separator =
446+
if sub_cfgs.iter().all(Cfg::is_simple) { " nor " } else { ", nor " };
447+
fmt.write_str("neither ")?;
448+
449+
sub_cfgs
450+
.iter()
451+
.map(|sub_cfg| {
452+
fmt::from_fn(|fmt| {
453+
write_with_opt_paren(fmt, !sub_cfg.is_all(), Display(sub_cfg, self.1))
460454
})
461-
.joined(separator, fmt)
462-
}
463-
ref simple @ Cfg::Cfg(..) => write!(fmt, "non-{}", Display(simple, self.1)),
464-
ref c => write!(fmt, "not ({})", Display(c, self.1)),
465-
},
455+
})
456+
.joined(separator, fmt)
457+
}
458+
Cfg::Not(box simple @ Cfg::Cfg(..)) => write!(fmt, "non-{}", Display(simple, self.1)),
459+
Cfg::Not(box c) => write!(fmt, "not ({})", Display(c, self.1)),
466460

467-
Cfg::Any(ref sub_cfgs) => {
461+
Cfg::Any(sub_cfgs) => {
468462
let separator = if sub_cfgs.iter().all(Cfg::is_simple) { " or " } else { ", or " };
469463
self.display_sub_cfgs(fmt, sub_cfgs, separator)
470464
}
471-
Cfg::All(ref sub_cfgs) => self.display_sub_cfgs(fmt, sub_cfgs, " and "),
465+
Cfg::All(sub_cfgs) => self.display_sub_cfgs(fmt, sub_cfgs, " and "),
472466

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

476-
Cfg::Cfg(name, value) => {
470+
&Cfg::Cfg(name, value) => {
477471
let human_readable = match (name, value) {
478472
(sym::unix, None) => "Unix",
479473
(sym::windows, None) => "Windows",

src/librustdoc/clean/mod.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,9 @@ fn clean_generic_bound<'tcx>(
224224
bound: &hir::GenericBound<'tcx>,
225225
cx: &mut DocContext<'tcx>,
226226
) -> Option<GenericBound> {
227-
Some(match *bound {
227+
Some(match bound {
228228
hir::GenericBound::Outlives(lt) => GenericBound::Outlives(clean_lifetime(lt, cx)),
229-
hir::GenericBound::Trait(ref t) => {
229+
hir::GenericBound::Trait(t) => {
230230
// `T: ~const Destruct` is hidden because `T: Destruct` is a no-op.
231231
if let hir::BoundConstness::Maybe(_) = t.modifiers.constness
232232
&& cx.tcx.lang_items().destruct_trait() == Some(t.trait_ref.trait_def_id().unwrap())
@@ -352,8 +352,8 @@ fn clean_where_predicate<'tcx>(
352352
if !predicate.kind.in_where_clause() {
353353
return None;
354354
}
355-
Some(match *predicate.kind {
356-
hir::WherePredicateKind::BoundPredicate(ref wbp) => {
355+
Some(match predicate.kind {
356+
hir::WherePredicateKind::BoundPredicate(wbp) => {
357357
let bound_params = wbp
358358
.bound_generic_params
359359
.iter()
@@ -366,12 +366,12 @@ fn clean_where_predicate<'tcx>(
366366
}
367367
}
368368

369-
hir::WherePredicateKind::RegionPredicate(ref wrp) => WherePredicate::RegionPredicate {
369+
hir::WherePredicateKind::RegionPredicate(wrp) => WherePredicate::RegionPredicate {
370370
lifetime: clean_lifetime(wrp.lifetime, cx),
371371
bounds: wrp.bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(),
372372
},
373373

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

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

2132-
let lifetime = clean_trait_object_lifetime_bound(*reg, container, obj, cx.tcx);
2132+
let lifetime = clean_trait_object_lifetime_bound(reg, container, obj, cx.tcx);
21332133

21342134
let mut bounds = dids
21352135
.map(|did| {
@@ -2846,19 +2846,19 @@ fn clean_maybe_renamed_item<'tcx>(
28462846
));
28472847
return ret;
28482848
}
2849-
ItemKind::Enum(_, ref def, generics) => EnumItem(Enum {
2849+
ItemKind::Enum(_, def, generics) => EnumItem(Enum {
28502850
variants: def.variants.iter().map(|v| clean_variant(v, cx)).collect(),
28512851
generics: clean_generics(generics, cx),
28522852
}),
28532853
ItemKind::TraitAlias(_, generics, bounds) => TraitAliasItem(TraitAlias {
28542854
generics: clean_generics(generics, cx),
28552855
bounds: bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(),
28562856
}),
2857-
ItemKind::Union(_, ref variant_data, generics) => UnionItem(Union {
2857+
ItemKind::Union(_, variant_data, generics) => UnionItem(Union {
28582858
generics: clean_generics(generics, cx),
28592859
fields: variant_data.fields().iter().map(|x| clean_field(x, cx)).collect(),
28602860
}),
2861-
ItemKind::Struct(_, ref variant_data, generics) => StructItem(Struct {
2861+
ItemKind::Struct(_, variant_data, generics) => StructItem(Struct {
28622862
ctor_kind: variant_data.ctor_kind(),
28632863
generics: clean_generics(generics, cx),
28642864
fields: variant_data.fields().iter().map(|x| clean_field(x, cx)).collect(),

src/librustdoc/clean/types.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -1337,9 +1337,9 @@ pub(crate) enum WherePredicate {
13371337

13381338
impl WherePredicate {
13391339
pub(crate) fn get_bounds(&self) -> Option<&[GenericBound]> {
1340-
match *self {
1341-
WherePredicate::BoundPredicate { ref bounds, .. } => Some(bounds),
1342-
WherePredicate::RegionPredicate { ref bounds, .. } => Some(bounds),
1340+
match self {
1341+
WherePredicate::BoundPredicate { bounds, .. } => Some(bounds),
1342+
WherePredicate::RegionPredicate { bounds, .. } => Some(bounds),
13431343
_ => None,
13441344
}
13451345
}
@@ -1709,13 +1709,13 @@ impl Type {
17091709
///
17101710
/// [clean]: crate::clean
17111711
pub(crate) fn def_id(&self, cache: &Cache) -> Option<DefId> {
1712-
let t: PrimitiveType = match *self {
1713-
Type::Path { ref path } => return Some(path.def_id()),
1714-
DynTrait(ref bounds, _) => return bounds.first().map(|b| b.trait_.def_id()),
1715-
Primitive(p) => return cache.primitive_locations.get(&p).cloned(),
1712+
let t: PrimitiveType = match self {
1713+
Type::Path { path } => return Some(path.def_id()),
1714+
DynTrait(bounds, _) => return bounds.first().map(|b| b.trait_.def_id()),
1715+
Primitive(p) => return cache.primitive_locations.get(p).cloned(),
17161716
BorrowedRef { type_: box Generic(..), .. } => PrimitiveType::Reference,
1717-
BorrowedRef { ref type_, .. } => return type_.def_id(cache),
1718-
Tuple(ref tys) => {
1717+
BorrowedRef { type_, .. } => return type_.def_id(cache),
1718+
Tuple(tys) => {
17191719
if tys.is_empty() {
17201720
PrimitiveType::Unit
17211721
} else {
@@ -1727,7 +1727,7 @@ impl Type {
17271727
Array(..) => PrimitiveType::Array,
17281728
Type::Pat(..) => PrimitiveType::Pat,
17291729
RawPointer(..) => PrimitiveType::RawPointer,
1730-
QPath(box QPathData { ref self_type, .. }) => return self_type.def_id(cache),
1730+
QPath(box QPathData { self_type, .. }) => return self_type.def_id(cache),
17311731
Generic(_) | SelfTy | Infer | ImplTrait(_) | UnsafeBinder(_) => return None,
17321732
};
17331733
Primitive(t).def_id(cache)

src/librustdoc/formats/item_type.rs

+13-20
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ impl Serialize for ItemType {
7070

7171
impl<'a> From<&'a clean::Item> for ItemType {
7272
fn from(item: &'a clean::Item) -> ItemType {
73-
let kind = match item.kind {
74-
clean::StrippedItem(box ref item) => item,
75-
ref kind => kind,
73+
let kind = match &item.kind {
74+
clean::StrippedItem(box item) => item,
75+
kind => kind,
7676
};
7777

78-
match *kind {
78+
match kind {
7979
clean::ModuleItem(..) => ItemType::Module,
8080
clean::ExternCrateItem { .. } => ItemType::ExternCrate,
8181
clean::ImportItem(..) => ItemType::Import,
@@ -103,7 +103,7 @@ impl<'a> From<&'a clean::Item> for ItemType {
103103
clean::ForeignTypeItem => ItemType::ForeignType,
104104
clean::KeywordItem => ItemType::Keyword,
105105
clean::TraitAliasItem(..) => ItemType::TraitAlias,
106-
clean::ProcMacroItem(ref mac) => match mac.kind {
106+
clean::ProcMacroItem(mac) => match mac.kind {
107107
MacroKind::Bang => ItemType::Macro,
108108
MacroKind::Attr => ItemType::ProcAttribute,
109109
MacroKind::Derive => ItemType::ProcDerive,
@@ -134,22 +134,15 @@ impl ItemType {
134134
DefKind::Trait => Self::Trait,
135135
DefKind::TyAlias => Self::TypeAlias,
136136
DefKind::TraitAlias => Self::TraitAlias,
137-
DefKind::Macro(kind) => match kind {
138-
MacroKind::Bang => ItemType::Macro,
139-
MacroKind::Attr => ItemType::ProcAttribute,
140-
MacroKind::Derive => ItemType::ProcDerive,
141-
},
137+
DefKind::Macro(MacroKind::Bang) => ItemType::Macro,
138+
DefKind::Macro(MacroKind::Attr) => ItemType::ProcAttribute,
139+
DefKind::Macro(MacroKind::Derive) => ItemType::ProcDerive,
142140
DefKind::ForeignTy => Self::ForeignType,
143141
DefKind::Variant => Self::Variant,
144142
DefKind::Field => Self::StructField,
145143
DefKind::AssocTy => Self::AssocType,
146-
DefKind::AssocFn => {
147-
if let Some(DefKind::Trait) = parent_kind {
148-
Self::TyMethod
149-
} else {
150-
Self::Method
151-
}
152-
}
144+
DefKind::AssocFn if let Some(DefKind::Trait) = parent_kind => Self::TyMethod,
145+
DefKind::AssocFn => Self::Method,
153146
DefKind::Ctor(CtorOf::Struct, _) => Self::Struct,
154147
DefKind::Ctor(CtorOf::Variant, _) => Self::Variant,
155148
DefKind::AssocConst => Self::AssocConst,
@@ -170,7 +163,7 @@ impl ItemType {
170163
}
171164

172165
pub(crate) fn as_str(&self) -> &'static str {
173-
match *self {
166+
match self {
174167
ItemType::Module => "mod",
175168
ItemType::ExternCrate => "externcrate",
176169
ItemType::Import => "import",
@@ -199,10 +192,10 @@ impl ItemType {
199192
}
200193
}
201194
pub(crate) fn is_method(&self) -> bool {
202-
matches!(*self, ItemType::Method | ItemType::TyMethod)
195+
matches!(self, ItemType::Method | ItemType::TyMethod)
203196
}
204197
pub(crate) fn is_adt(&self) -> bool {
205-
matches!(*self, ItemType::Struct | ItemType::Union | ItemType::Enum)
198+
matches!(self, ItemType::Struct | ItemType::Union | ItemType::Enum)
206199
}
207200
}
208201

0 commit comments

Comments
 (0)