Skip to content

Commit 1191eb4

Browse files
committed
Prefer fall through to code repetition
1 parent b2e93a4 commit 1191eb4

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

src/librustc/ty/print/pretty.rs

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use rustc_span::symbol::{kw, Symbol};
1717
use rustc_target::spec::abi::Abi;
1818

1919
use std::cell::Cell;
20+
use std::char;
2021
use std::collections::BTreeMap;
2122
use std::fmt::{self, Write as _};
2223
use std::ops::{Deref, DerefMut};
@@ -917,22 +918,36 @@ pub trait PrettyPrinter<'tcx>:
917918

918919
match (scalar, &ty.kind) {
919920
// Byte strings (&[u8; N])
920-
(Scalar::Ptr(ptr), ty::Ref(_, ty::TyS { kind: ty::Array(t, n), .. }, _))
921-
if *t == self.tcx().types.u8 =>
922-
{
923-
match n.val.try_to_bits(self.tcx().data_layout.pointer_size) {
924-
Some(n) => {
925-
let byte_str = self
926-
.tcx()
927-
.alloc_map
928-
.lock()
929-
.unwrap_memory(ptr.alloc_id)
930-
.get_bytes(&self.tcx(), ptr, Size::from_bytes(n as u64))
931-
.unwrap();
932-
p!(pretty_print_byte_str(byte_str));
933-
}
934-
None => self.write_str("_")?,
935-
}
921+
(
922+
Scalar::Ptr(ptr),
923+
ty::Ref(
924+
_,
925+
ty::TyS {
926+
kind:
927+
ty::Array(
928+
ty::TyS { kind: ty::Uint(ast::UintTy::U8), .. },
929+
ty::Const {
930+
val:
931+
ty::ConstKind::Value(ConstValue::Scalar(Scalar::Raw {
932+
data,
933+
..
934+
})),
935+
..
936+
},
937+
),
938+
..
939+
},
940+
_,
941+
),
942+
) => {
943+
let byte_str = self
944+
.tcx()
945+
.alloc_map
946+
.lock()
947+
.unwrap_memory(ptr.alloc_id)
948+
.get_bytes(&self.tcx(), ptr, Size::from_bytes(*data as u64))
949+
.unwrap();
950+
p!(pretty_print_byte_str(byte_str));
936951
}
937952
// Bool
938953
(Scalar::Raw { data: 0, .. }, ty::Bool) => p!(write("false")),
@@ -970,10 +985,9 @@ pub trait PrettyPrinter<'tcx>:
970985
}
971986
}
972987
// Char
973-
(Scalar::Raw { data, .. }, ty::Char) => match ::std::char::from_u32(data as u32) {
974-
Some(c) => p!(write("{:?}", c)),
975-
None => p!(write("{}_char", data)),
976-
},
988+
(Scalar::Raw { data, .. }, ty::Char) if char::from_u32(data as u32).is_some() => {
989+
p!(write("{:?}", char::from_u32(data as u32).unwrap()))
990+
}
977991
// Raw pointers
978992
(Scalar::Raw { data, .. }, ty::RawPtr(_)) => {
979993
p!(write("{{0x{:x} as ", data), print(ty), write("}}"))

0 commit comments

Comments
 (0)