From 2fbdc79ae66185b6f32289acd43e48c98d270a05 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Fri, 3 Dec 2021 13:23:18 -0800 Subject: [PATCH] Remove a Clean impl for a tuple (1) This commit removes the first of nine Clean impls on tuples, converting it to a function instead. The fact that these are impls causes several problems: 1. They are nameless, so it's unclear what they do. 2. It's hard to find where they're used apart from removing them and seeing what errors occur (this applies to all Clean impls, not just the tuple ones). 3. Rustc doesn't currently warn when impls are unused, so dead code can accumulate easily (all Clean impls). 4. Their bodies often use tuple field indexing syntax (e.g., `self.1`) to refer to their "arguments", which makes reading the code more difficult. As I noted, some of these problems apply to all Clean impls, but even those problems are exacerbated by the tuple impls since they make general understanding of the code harder. Converting the impls to functions solves all four of these problems. --- src/librustdoc/clean/mod.rs | 45 ++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 3267e3087106..95724cfd0051 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -102,11 +102,9 @@ impl Clean for hir::GenericBound<'_> { _ => bug!("clean: parenthesized `GenericBound::LangItemTrait`"), }; + let trait_ = clean_trait_ref_with_bindings(cx, trait_ref, &bindings); GenericBound::TraitBound( - PolyTrait { - trait_: (trait_ref, &bindings[..]).clean(cx), - generic_params: vec![], - }, + PolyTrait { trait_, generic_params: vec![] }, hir::TraitBoundModifier::None, ) } @@ -117,29 +115,26 @@ impl Clean for hir::GenericBound<'_> { } } -impl Clean for (ty::TraitRef<'_>, &[TypeBinding]) { - fn clean(&self, cx: &mut DocContext<'_>) -> Path { - let (trait_ref, bindings) = *self; - let kind = cx.tcx.def_kind(trait_ref.def_id).into(); - if !matches!(kind, ItemType::Trait | ItemType::TraitAlias) { - span_bug!( - cx.tcx.def_span(trait_ref.def_id), - "`TraitRef` had unexpected kind {:?}", - kind - ); - } - inline::record_extern_fqn(cx, trait_ref.def_id, kind); - let path = external_path(cx, trait_ref.def_id, true, bindings.to_vec(), trait_ref.substs); - - debug!("ty::TraitRef\n subst: {:?}\n", trait_ref.substs); - - path +fn clean_trait_ref_with_bindings( + cx: &mut DocContext<'_>, + trait_ref: ty::TraitRef<'_>, + bindings: &[TypeBinding], +) -> Path { + let kind = cx.tcx.def_kind(trait_ref.def_id).into(); + if !matches!(kind, ItemType::Trait | ItemType::TraitAlias) { + span_bug!(cx.tcx.def_span(trait_ref.def_id), "`TraitRef` had unexpected kind {:?}", kind); } + inline::record_extern_fqn(cx, trait_ref.def_id, kind); + let path = external_path(cx, trait_ref.def_id, true, bindings.to_vec(), trait_ref.substs); + + debug!("ty::TraitRef\n subst: {:?}\n", trait_ref.substs); + + path } impl Clean for ty::TraitRef<'tcx> { fn clean(&self, cx: &mut DocContext<'_>) -> Path { - (*self, &[][..]).clean(cx) + clean_trait_ref_with_bindings(cx, *self, &[]) } } @@ -162,11 +157,9 @@ impl Clean for (ty::PolyTraitRef<'_>, &[TypeBinding]) { }) .collect(); + let trait_ = clean_trait_ref_with_bindings(cx, poly_trait_ref.skip_binder(), bindings); GenericBound::TraitBound( - PolyTrait { - trait_: (poly_trait_ref.skip_binder(), bindings).clean(cx), - generic_params: late_bound_regions, - }, + PolyTrait { trait_, generic_params: late_bound_regions }, hir::TraitBoundModifier::None, ) }