From 4b6fc8b70fd355614bad31d4268993dc7ef17431 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 9 Oct 2023 14:26:52 +0200 Subject: [PATCH] Improve code --- src/librustdoc/clean/utils.rs | 36 ++++++++---------------- src/librustdoc/html/render/print_item.rs | 24 ++++++++-------- 2 files changed, 24 insertions(+), 36 deletions(-) diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index 3be4f065ccd..01078504b71 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -328,42 +328,30 @@ fn print_const_with_custom_print_scalar<'tcx>( // For all other types, fallback to the original `pretty_print_const`. match (ct, ct.ty().kind()) { (mir::Const::Val(mir::ConstValue::Scalar(int), _), ty::Uint(ui)) => { - if with_underscores { - if with_type { - format!( - "{}{}", - format_integer_with_underscore_sep(&int.to_string()), - ui.name_str() - ) - } else { - format_integer_with_underscore_sep(&int.to_string()) - } - } else if with_type { - format!("{}{}", int.to_string(), ui.name_str()) + let mut output = if with_underscores { + format_integer_with_underscore_sep(&int.to_string()) } else { int.to_string() + }; + if with_type { + output += ui.name_str(); } + output } (mir::Const::Val(mir::ConstValue::Scalar(int), _), ty::Int(i)) => { let ty = ct.ty(); let size = tcx.layout_of(ty::ParamEnv::empty().and(ty)).unwrap().size; let data = int.assert_bits(size); let sign_extended_data = size.sign_extend(data) as i128; - if with_underscores { - if with_type { - format!( - "{}{}", - format_integer_with_underscore_sep(&sign_extended_data.to_string()), - i.name_str() - ) - } else { - format_integer_with_underscore_sep(&sign_extended_data.to_string()) - } - } else if with_type { - format!("{}{}", sign_extended_data.to_string(), i.name_str()) + let mut output = if with_underscores { + format_integer_with_underscore_sep(&sign_extended_data.to_string()) } else { sign_extended_data.to_string() + }; + if with_type { + output += i.name_str(); } + output } _ => ct.to_string(), } diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 2da5e8ef17a..467493cb0b3 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -5,10 +5,12 @@ use rustc_hir as hir; use rustc_hir::def::CtorKind; use rustc_hir::def_id::DefId; +use rustc_index::IndexVec; use rustc_middle::middle::stability; use rustc_middle::ty::{self, TyCtxt}; use rustc_span::hygiene::MacroKind; use rustc_span::symbol::{kw, sym, Symbol}; +use rustc_target::abi::VariantIdx; use std::cell::{RefCell, RefMut}; use std::cmp::Ordering; use std::fmt; @@ -1442,9 +1444,7 @@ fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean:: /// It'll return true if all variants are C-like variants and if at least one of them has a value /// set. -fn should_show_c_like_variants_value( - variants: &rustc_index::IndexVec, -) -> bool { +fn should_show_enum_discriminant(variants: &IndexVec) -> bool { let mut has_variants_with_value = false; for variant in variants { if let clean::VariantItem(ref var) = *variant.kind && @@ -1463,14 +1463,14 @@ fn display_c_like_variant( cx: &mut Context<'_>, item: &clean::Item, variant: &clean::Variant, - index: rustc_target::abi::VariantIdx, - should_show_c_like_variants_value: bool, + index: VariantIdx, + should_show_enum_discriminant: bool, enum_def_id: DefId, ) { let name = item.name.unwrap(); if let Some(ref value) = variant.discriminant { write!(w, "{} = {}", name.as_str(), value.value(cx.tcx(), true)); - } else if should_show_c_like_variants_value { + } else if should_show_enum_discriminant { let adt_def = cx.tcx().adt_def(enum_def_id); let discr = adt_def.discriminant_for_variant(cx.tcx(), index); if discr.ty.is_signed() { @@ -1487,13 +1487,13 @@ fn render_enum_fields( mut w: &mut Buffer, cx: &mut Context<'_>, g: Option<&clean::Generics>, - variants: &rustc_index::IndexVec, + variants: &IndexVec, count_variants: usize, has_stripped_entries: bool, is_non_exhaustive: bool, enum_def_id: DefId, ) { - let should_show_c_like_variants_value = should_show_c_like_variants_value(variants); + let should_show_enum_discriminant = should_show_enum_discriminant(variants); if !g.is_some_and(|g| print_where_clause_and_check(w, g, cx)) { // If there wasn't a `where` clause, we add a whitespace. w.write_str(" "); @@ -1522,7 +1522,7 @@ fn render_enum_fields( v, var, index, - should_show_c_like_variants_value, + should_show_enum_discriminant, enum_def_id, ), clean::VariantKind::Tuple(ref s) => { @@ -1551,7 +1551,7 @@ fn item_variants( w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, - variants: &rustc_index::IndexVec, + variants: &IndexVec, ) { let tcx = cx.tcx(); write!( @@ -1564,7 +1564,7 @@ fn item_variants( document_non_exhaustive_header(it), document_non_exhaustive(it) ); - let should_show_c_like_variants_value = should_show_c_like_variants_value(variants); + let should_show_enum_discriminant = should_show_enum_discriminant(variants); for (index, variant) in variants.iter_enumerated() { if variant.is_stripped() { continue; @@ -1593,7 +1593,7 @@ fn item_variants( variant, var, index, - should_show_c_like_variants_value, + should_show_enum_discriminant, it.def_id().unwrap(), ); } else {