Skip to content

Commit 535bd13

Browse files
committed
A few cleanups for fmt_macros, graphviz, apfloat, target, serialize and term
1 parent f6d43ed commit 535bd13

File tree

10 files changed

+141
-153
lines changed

10 files changed

+141
-153
lines changed

src/libfmt_macros/lib.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ impl<'a> Parser<'a> {
411411

412412
// fill character
413413
if let Some(&(_, c)) = self.cur.peek() {
414-
match self.cur.clone().skip(1).next() {
414+
match self.cur.clone().nth(1) {
415415
Some((_, '>')) | Some((_, '<')) | Some((_, '^')) => {
416416
spec.fill = Some(c);
417417
self.cur.next();
@@ -504,13 +504,11 @@ impl<'a> Parser<'a> {
504504
if word.is_empty() {
505505
self.cur = tmp;
506506
CountImplied
507+
} else if self.consume('$') {
508+
CountIsName(word)
507509
} else {
508-
if self.consume('$') {
509-
CountIsName(word)
510-
} else {
511-
self.cur = tmp;
512-
CountImplied
513-
}
510+
self.cur = tmp;
511+
CountImplied
514512
}
515513
}
516514
}

src/libgraphviz/lib.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,8 @@ impl<'a> Id<'a> {
420420
if !name.chars().all(|c| c.is_ascii_alphanumeric() || c == '_' ) {
421421
return Err(());
422422
}
423-
return Ok(Id { name: name });
423+
424+
Ok(Id { name })
424425
}
425426

426427
pub fn as_slice(&'a self) -> &'a str {
@@ -533,10 +534,10 @@ impl<'a> LabelText<'a> {
533534
/// Renders text as string suitable for a label in a .dot file.
534535
/// This includes quotes or suitable delimiters.
535536
pub fn to_dot_string(&self) -> String {
536-
match self {
537-
&LabelStr(ref s) => format!("\"{}\"", s.escape_default()),
538-
&EscStr(ref s) => format!("\"{}\"", LabelText::escape_str(&s)),
539-
&HtmlStr(ref s) => format!("<{}>", s),
537+
match *self {
538+
LabelStr(ref s) => format!("\"{}\"", s.escape_default()),
539+
EscStr(ref s) => format!("\"{}\"", LabelText::escape_str(&s)),
540+
HtmlStr(ref s) => format!("<{}>", s),
540541
}
541542
}
542543

src/librustc_apfloat/ieee.rs

+44-47
Original file line numberDiff line numberDiff line change
@@ -536,23 +536,21 @@ impl<S: Semantics> fmt::Display for IeeeFloat<S> {
536536
// Check whether we should use scientific notation.
537537
let scientific = if width == 0 {
538538
true
539+
} else if exp >= 0 {
540+
// 765e3 --> 765000
541+
// ^^^
542+
// But we shouldn't make the number look more precise than it is.
543+
exp as usize > width || digits + exp as usize > precision
539544
} else {
540-
if exp >= 0 {
541-
// 765e3 --> 765000
542-
// ^^^
543-
// But we shouldn't make the number look more precise than it is.
544-
exp as usize > width || digits + exp as usize > precision
545+
// Power of the most significant digit.
546+
let msd = exp + (digits - 1) as ExpInt;
547+
if msd >= 0 {
548+
// 765e-2 == 7.65
549+
false
545550
} else {
546-
// Power of the most significant digit.
547-
let msd = exp + (digits - 1) as ExpInt;
548-
if msd >= 0 {
549-
// 765e-2 == 7.65
550-
false
551-
} else {
552-
// 765e-5 == 0.00765
553-
// ^ ^^
554-
-msd as usize > width
555-
}
551+
// 765e-5 == 0.00765
552+
// ^ ^^
553+
-msd as usize > width
556554
}
557555
};
558556

@@ -702,7 +700,7 @@ impl<S: Semantics> Float for IeeeFloat<S> {
702700
// exponent = 1..10
703701
// significand = 1..1
704702
IeeeFloat {
705-
sig: [!0 & ((1 << S::PRECISION) - 1)],
703+
sig: [(1 << S::PRECISION) - 1],
706704
exp: S::MAX_EXP,
707705
category: Category::Normal,
708706
sign: false,
@@ -1507,10 +1505,11 @@ impl<S: Semantics, T: Semantics> FloatConvert<IeeeFloat<T>> for IeeeFloat<S> {
15071505
}
15081506

15091507
// If this is a truncation, perform the shift.
1510-
let mut loss = Loss::ExactlyZero;
1511-
if shift < 0 && (r.is_finite_non_zero() || r.category == Category::NaN) {
1512-
loss = sig::shift_right(&mut r.sig, &mut 0, -shift as usize);
1513-
}
1508+
let loss = if shift < 0 && (r.is_finite_non_zero() || r.category == Category::NaN) {
1509+
sig::shift_right(&mut r.sig, &mut 0, -shift as usize)
1510+
} else {
1511+
Loss::ExactlyZero
1512+
};
15141513

15151514
// If this is an extension, perform the shift.
15161515
if shift > 0 && (r.is_finite_non_zero() || r.category == Category::NaN) {
@@ -1738,27 +1737,25 @@ impl<S: Semantics> IeeeFloat<S> {
17381737
bit_pos -= 4;
17391738
if bit_pos >= 0 {
17401739
r.sig[0] |= (hex_value as Limb) << bit_pos;
1741-
} else {
1742-
// If zero or one-half (the hexadecimal digit 8) are followed
1743-
// by non-zero, they're a little more than zero or one-half.
1744-
if let Some(ref mut loss) = loss {
1745-
if hex_value != 0 {
1746-
if *loss == Loss::ExactlyZero {
1747-
*loss = Loss::LessThanHalf;
1748-
}
1749-
if *loss == Loss::ExactlyHalf {
1750-
*loss = Loss::MoreThanHalf;
1751-
}
1740+
// If zero or one-half (the hexadecimal digit 8) are followed
1741+
// by non-zero, they're a little more than zero or one-half.
1742+
} else if let Some(ref mut loss) = loss {
1743+
if hex_value != 0 {
1744+
if *loss == Loss::ExactlyZero {
1745+
*loss = Loss::LessThanHalf;
1746+
}
1747+
if *loss == Loss::ExactlyHalf {
1748+
*loss = Loss::MoreThanHalf;
17521749
}
1753-
} else {
1754-
loss = Some(match hex_value {
1755-
0 => Loss::ExactlyZero,
1756-
1..=7 => Loss::LessThanHalf,
1757-
8 => Loss::ExactlyHalf,
1758-
9..=15 => Loss::MoreThanHalf,
1759-
_ => unreachable!(),
1760-
});
17611750
}
1751+
} else {
1752+
loss = Some(match hex_value {
1753+
0 => Loss::ExactlyZero,
1754+
1..=7 => Loss::LessThanHalf,
1755+
8 => Loss::ExactlyHalf,
1756+
9..=15 => Loss::MoreThanHalf,
1757+
_ => unreachable!(),
1758+
});
17621759
}
17631760
} else if c == 'p' || c == 'P' {
17641761
if !any_digits {
@@ -2309,9 +2306,9 @@ mod sig {
23092306

23102307
/// One, not zero, based LSB. That is, returns 0 for a zeroed significand.
23112308
pub(super) fn olsb(limbs: &[Limb]) -> usize {
2312-
for i in 0..limbs.len() {
2313-
if limbs[i] != 0 {
2314-
return i * LIMB_BITS + limbs[i].trailing_zeros() as usize + 1;
2309+
for (i, &limb) in limbs.iter().enumerate() {
2310+
if limb != 0 {
2311+
return i * LIMB_BITS + limb.trailing_zeros() as usize + 1;
23152312
}
23162313
}
23172314

@@ -2320,9 +2317,9 @@ mod sig {
23202317

23212318
/// One, not zero, based MSB. That is, returns 0 for a zeroed significand.
23222319
pub(super) fn omsb(limbs: &[Limb]) -> usize {
2323-
for i in (0..limbs.len()).rev() {
2324-
if limbs[i] != 0 {
2325-
return (i + 1) * LIMB_BITS - limbs[i].leading_zeros() as usize;
2320+
for (i, &limb) in limbs.iter().enumerate().rev() {
2321+
if limb != 0 {
2322+
return (i + 1) * LIMB_BITS - limb.leading_zeros() as usize;
23262323
}
23272324
}
23282325

@@ -2378,7 +2375,7 @@ mod sig {
23782375
limb = dst[i - jump];
23792376
if shift > 0 {
23802377
limb <<= shift;
2381-
if i >= jump + 1 {
2378+
if i > jump {
23822379
limb |= dst[i - jump - 1] >> (LIMB_BITS - shift);
23832380
}
23842381
}
@@ -2448,7 +2445,7 @@ mod sig {
24482445
let n = dst_limbs * LIMB_BITS - shift;
24492446
if n < src_bits {
24502447
let mask = (1 << (src_bits - n)) - 1;
2451-
dst[dst_limbs - 1] |= (src[dst_limbs] & mask) << n % LIMB_BITS;
2448+
dst[dst_limbs - 1] |= (src[dst_limbs] & mask) << (n % LIMB_BITS);
24522449
} else if n > src_bits && src_bits % LIMB_BITS > 0 {
24532450
dst[dst_limbs - 1] &= (1 << (src_bits % LIMB_BITS)) - 1;
24542451
}

src/librustc_target/spec/mod.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -764,14 +764,10 @@ impl Target {
764764
// the JSON parser is not updated to match the structs.
765765

766766
let get_req_field = |name: &str| {
767-
match obj.find(name)
768-
.map(|s| s.as_string())
769-
.and_then(|os| os.map(|s| s.to_string())) {
770-
Some(val) => Ok(val),
771-
None => {
772-
return Err(format!("Field {} in target specification is required", name))
773-
}
774-
}
767+
obj.find(name)
768+
.map(|s| s.as_string())
769+
.and_then(|os| os.map(|s| s.to_string()))
770+
.ok_or_else(|| format!("Field {} in target specification is required", name))
775771
};
776772

777773
let get_opt_field = |name: &str, default: &str| {

src/libserialize/hex.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub trait ToHex {
2222
fn to_hex(&self) -> String;
2323
}
2424

25-
const CHARS: &'static [u8] = b"0123456789abcdef";
25+
const CHARS: &[u8] = b"0123456789abcdef";
2626

2727
impl ToHex for [u8] {
2828
/// Turn a vector of `u8` bytes into a hexadecimal string.

src/libserialize/json.rs

+28-30
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ fn escape_char(writer: &mut dyn fmt::Write, v: char) -> EncodeResult {
438438
}
439439

440440
fn spaces(wr: &mut dyn fmt::Write, mut n: usize) -> EncodeResult {
441-
const BUF: &'static str = " ";
441+
const BUF: &str = " ";
442442

443443
while n >= BUF.len() {
444444
wr.write_str(BUF)?;
@@ -799,21 +799,21 @@ impl<'a> ::Encoder for PrettyEncoder<'a> {
799799
escape_str(self.writer, name)
800800
} else {
801801
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
802-
write!(self.writer, "{{\n")?;
802+
writeln!(self.writer, "{{")?;
803803
self.curr_indent += self.indent;
804804
spaces(self.writer, self.curr_indent)?;
805805
write!(self.writer, "\"variant\": ")?;
806806
escape_str(self.writer, name)?;
807-
write!(self.writer, ",\n")?;
807+
writeln!(self.writer, ",")?;
808808
spaces(self.writer, self.curr_indent)?;
809-
write!(self.writer, "\"fields\": [\n")?;
809+
writeln!(self.writer, "\"fields\": [")?;
810810
self.curr_indent += self.indent;
811811
f(self)?;
812812
self.curr_indent -= self.indent;
813-
write!(self.writer, "\n")?;
813+
writeln!(self.writer)?;
814814
spaces(self.writer, self.curr_indent)?;
815815
self.curr_indent -= self.indent;
816-
write!(self.writer, "]\n")?;
816+
writeln!(self.writer, "]")?;
817817
spaces(self.writer, self.curr_indent)?;
818818
write!(self.writer, "}}")?;
819819
Ok(())
@@ -825,7 +825,7 @@ impl<'a> ::Encoder for PrettyEncoder<'a> {
825825
{
826826
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
827827
if idx != 0 {
828-
write!(self.writer, ",\n")?;
828+
writeln!(self.writer, ",")?;
829829
}
830830
spaces(self.writer, self.curr_indent)?;
831831
f(self)
@@ -864,7 +864,7 @@ impl<'a> ::Encoder for PrettyEncoder<'a> {
864864
self.curr_indent += self.indent;
865865
f(self)?;
866866
self.curr_indent -= self.indent;
867-
write!(self.writer, "\n")?;
867+
writeln!(self.writer)?;
868868
spaces(self.writer, self.curr_indent)?;
869869
write!(self.writer, "}}")?;
870870
}
@@ -876,9 +876,9 @@ impl<'a> ::Encoder for PrettyEncoder<'a> {
876876
{
877877
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
878878
if idx == 0 {
879-
write!(self.writer, "\n")?;
879+
writeln!(self.writer)?;
880880
} else {
881-
write!(self.writer, ",\n")?;
881+
writeln!(self.writer, ",")?;
882882
}
883883
spaces(self.writer, self.curr_indent)?;
884884
escape_str(self.writer, name)?;
@@ -940,7 +940,7 @@ impl<'a> ::Encoder for PrettyEncoder<'a> {
940940
self.curr_indent += self.indent;
941941
f(self)?;
942942
self.curr_indent -= self.indent;
943-
write!(self.writer, "\n")?;
943+
writeln!(self.writer)?;
944944
spaces(self.writer, self.curr_indent)?;
945945
write!(self.writer, "]")?;
946946
}
@@ -952,9 +952,9 @@ impl<'a> ::Encoder for PrettyEncoder<'a> {
952952
{
953953
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
954954
if idx == 0 {
955-
write!(self.writer, "\n")?;
955+
writeln!(self.writer)?;
956956
} else {
957-
write!(self.writer, ",\n")?;
957+
writeln!(self.writer, ",")?;
958958
}
959959
spaces(self.writer, self.curr_indent)?;
960960
f(self)
@@ -971,7 +971,7 @@ impl<'a> ::Encoder for PrettyEncoder<'a> {
971971
self.curr_indent += self.indent;
972972
f(self)?;
973973
self.curr_indent -= self.indent;
974-
write!(self.writer, "\n")?;
974+
writeln!(self.writer)?;
975975
spaces(self.writer, self.curr_indent)?;
976976
write!(self.writer, "}}")?;
977977
}
@@ -983,9 +983,9 @@ impl<'a> ::Encoder for PrettyEncoder<'a> {
983983
{
984984
if self.is_emitting_map_key { return Err(EncoderError::BadHashmapKey); }
985985
if idx == 0 {
986-
write!(self.writer, "\n")?;
986+
writeln!(self.writer)?;
987987
} else {
988-
write!(self.writer, ",\n")?;
988+
writeln!(self.writer, ",")?;
989989
}
990990
spaces(self.writer, self.curr_indent)?;
991991
self.is_emitting_map_key = true;
@@ -1387,10 +1387,10 @@ impl Stack {
13871387

13881388
// Used by Parser to test whether the top-most element is an index.
13891389
fn last_is_index(&self) -> bool {
1390-
if self.is_empty() { return false; }
1391-
return match *self.stack.last().unwrap() {
1392-
InternalIndex(_) => true,
1393-
_ => false,
1390+
if let Some(InternalIndex(_)) = self.stack.last() {
1391+
true
1392+
} else {
1393+
false
13941394
}
13951395
}
13961396

@@ -1530,19 +1530,17 @@ impl<T: Iterator<Item=char>> Parser<T> {
15301530
}
15311531

15321532
F64Value(res)
1533-
} else {
1534-
if neg {
1535-
let res = (res as i64).wrapping_neg();
1533+
} else if neg {
1534+
let res = (res as i64).wrapping_neg();
15361535

1537-
// Make sure we didn't underflow.
1538-
if res > 0 {
1539-
Error(SyntaxError(InvalidNumber, self.line, self.col))
1540-
} else {
1541-
I64Value(res)
1542-
}
1536+
// Make sure we didn't underflow.
1537+
if res > 0 {
1538+
Error(SyntaxError(InvalidNumber, self.line, self.col))
15431539
} else {
1544-
U64Value(res)
1540+
I64Value(res)
15451541
}
1542+
} else {
1543+
U64Value(res)
15461544
}
15471545
}
15481546

src/libserialize/leb128.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ pub fn write_signed_leb128_to<W>(mut value: i128, mut write: W)
103103
loop {
104104
let mut byte = (value as u8) & 0x7f;
105105
value >>= 7;
106-
let more = !((((value == 0) && ((byte & 0x40) == 0)) ||
107-
((value == -1) && ((byte & 0x40) != 0))));
106+
let more = !(((value == 0) && ((byte & 0x40) == 0)) ||
107+
((value == -1) && ((byte & 0x40) != 0)));
108108

109109
if more {
110110
byte |= 0x80; // Mark this byte to show that more bytes will follow.

0 commit comments

Comments
 (0)