Skip to content

Commit 4b6fc8b

Browse files
Improve code
1 parent b0badc1 commit 4b6fc8b

File tree

2 files changed

+24
-36
lines changed

2 files changed

+24
-36
lines changed

src/librustdoc/clean/utils.rs

+12-24
Original file line numberDiff line numberDiff line change
@@ -328,42 +328,30 @@ fn print_const_with_custom_print_scalar<'tcx>(
328328
// For all other types, fallback to the original `pretty_print_const`.
329329
match (ct, ct.ty().kind()) {
330330
(mir::Const::Val(mir::ConstValue::Scalar(int), _), ty::Uint(ui)) => {
331-
if with_underscores {
332-
if with_type {
333-
format!(
334-
"{}{}",
335-
format_integer_with_underscore_sep(&int.to_string()),
336-
ui.name_str()
337-
)
338-
} else {
339-
format_integer_with_underscore_sep(&int.to_string())
340-
}
341-
} else if with_type {
342-
format!("{}{}", int.to_string(), ui.name_str())
331+
let mut output = if with_underscores {
332+
format_integer_with_underscore_sep(&int.to_string())
343333
} else {
344334
int.to_string()
335+
};
336+
if with_type {
337+
output += ui.name_str();
345338
}
339+
output
346340
}
347341
(mir::Const::Val(mir::ConstValue::Scalar(int), _), ty::Int(i)) => {
348342
let ty = ct.ty();
349343
let size = tcx.layout_of(ty::ParamEnv::empty().and(ty)).unwrap().size;
350344
let data = int.assert_bits(size);
351345
let sign_extended_data = size.sign_extend(data) as i128;
352-
if with_underscores {
353-
if with_type {
354-
format!(
355-
"{}{}",
356-
format_integer_with_underscore_sep(&sign_extended_data.to_string()),
357-
i.name_str()
358-
)
359-
} else {
360-
format_integer_with_underscore_sep(&sign_extended_data.to_string())
361-
}
362-
} else if with_type {
363-
format!("{}{}", sign_extended_data.to_string(), i.name_str())
346+
let mut output = if with_underscores {
347+
format_integer_with_underscore_sep(&sign_extended_data.to_string())
364348
} else {
365349
sign_extended_data.to_string()
350+
};
351+
if with_type {
352+
output += i.name_str();
366353
}
354+
output
367355
}
368356
_ => ct.to_string(),
369357
}

src/librustdoc/html/render/print_item.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
55
use rustc_hir as hir;
66
use rustc_hir::def::CtorKind;
77
use rustc_hir::def_id::DefId;
8+
use rustc_index::IndexVec;
89
use rustc_middle::middle::stability;
910
use rustc_middle::ty::{self, TyCtxt};
1011
use rustc_span::hygiene::MacroKind;
1112
use rustc_span::symbol::{kw, sym, Symbol};
13+
use rustc_target::abi::VariantIdx;
1214
use std::cell::{RefCell, RefMut};
1315
use std::cmp::Ordering;
1416
use std::fmt;
@@ -1442,9 +1444,7 @@ fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::
14421444

14431445
/// It'll return true if all variants are C-like variants and if at least one of them has a value
14441446
/// set.
1445-
fn should_show_c_like_variants_value(
1446-
variants: &rustc_index::IndexVec<rustc_target::abi::VariantIdx, clean::Item>,
1447-
) -> bool {
1447+
fn should_show_enum_discriminant(variants: &IndexVec<VariantIdx, clean::Item>) -> bool {
14481448
let mut has_variants_with_value = false;
14491449
for variant in variants {
14501450
if let clean::VariantItem(ref var) = *variant.kind &&
@@ -1463,14 +1463,14 @@ fn display_c_like_variant(
14631463
cx: &mut Context<'_>,
14641464
item: &clean::Item,
14651465
variant: &clean::Variant,
1466-
index: rustc_target::abi::VariantIdx,
1467-
should_show_c_like_variants_value: bool,
1466+
index: VariantIdx,
1467+
should_show_enum_discriminant: bool,
14681468
enum_def_id: DefId,
14691469
) {
14701470
let name = item.name.unwrap();
14711471
if let Some(ref value) = variant.discriminant {
14721472
write!(w, "{} = {}", name.as_str(), value.value(cx.tcx(), true));
1473-
} else if should_show_c_like_variants_value {
1473+
} else if should_show_enum_discriminant {
14741474
let adt_def = cx.tcx().adt_def(enum_def_id);
14751475
let discr = adt_def.discriminant_for_variant(cx.tcx(), index);
14761476
if discr.ty.is_signed() {
@@ -1487,13 +1487,13 @@ fn render_enum_fields(
14871487
mut w: &mut Buffer,
14881488
cx: &mut Context<'_>,
14891489
g: Option<&clean::Generics>,
1490-
variants: &rustc_index::IndexVec<rustc_target::abi::VariantIdx, clean::Item>,
1490+
variants: &IndexVec<VariantIdx, clean::Item>,
14911491
count_variants: usize,
14921492
has_stripped_entries: bool,
14931493
is_non_exhaustive: bool,
14941494
enum_def_id: DefId,
14951495
) {
1496-
let should_show_c_like_variants_value = should_show_c_like_variants_value(variants);
1496+
let should_show_enum_discriminant = should_show_enum_discriminant(variants);
14971497
if !g.is_some_and(|g| print_where_clause_and_check(w, g, cx)) {
14981498
// If there wasn't a `where` clause, we add a whitespace.
14991499
w.write_str(" ");
@@ -1522,7 +1522,7 @@ fn render_enum_fields(
15221522
v,
15231523
var,
15241524
index,
1525-
should_show_c_like_variants_value,
1525+
should_show_enum_discriminant,
15261526
enum_def_id,
15271527
),
15281528
clean::VariantKind::Tuple(ref s) => {
@@ -1551,7 +1551,7 @@ fn item_variants(
15511551
w: &mut Buffer,
15521552
cx: &mut Context<'_>,
15531553
it: &clean::Item,
1554-
variants: &rustc_index::IndexVec<rustc_target::abi::VariantIdx, clean::Item>,
1554+
variants: &IndexVec<VariantIdx, clean::Item>,
15551555
) {
15561556
let tcx = cx.tcx();
15571557
write!(
@@ -1564,7 +1564,7 @@ fn item_variants(
15641564
document_non_exhaustive_header(it),
15651565
document_non_exhaustive(it)
15661566
);
1567-
let should_show_c_like_variants_value = should_show_c_like_variants_value(variants);
1567+
let should_show_enum_discriminant = should_show_enum_discriminant(variants);
15681568
for (index, variant) in variants.iter_enumerated() {
15691569
if variant.is_stripped() {
15701570
continue;
@@ -1593,7 +1593,7 @@ fn item_variants(
15931593
variant,
15941594
var,
15951595
index,
1596-
should_show_c_like_variants_value,
1596+
should_show_enum_discriminant,
15971597
it.def_id().unwrap(),
15981598
);
15991599
} else {

0 commit comments

Comments
 (0)