Skip to content

Commit 1393404

Browse files
authored
Merge pull request #429 from Kmeakin/clippy-fix
Apply clippy suggestions
2 parents fe4f169 + af33a7b commit 1393404

File tree

10 files changed

+116
-79
lines changed

10 files changed

+116
-79
lines changed

fathom/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ version = "0.1.0"
44
authors = ["YesLogic Pty. Ltd. <[email protected]>"]
55
repository = "https://github.com/yeslogic/fathom"
66
edition = "2021"
7-
rust-version = "1.56.0"
7+
rust-version = "1.62.0"
88
publish = false
99

1010
description = "A language for declaratively specifying binary data formats"

fathom/src/core.rs

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ pub enum UIntStyle {
555555
}
556556

557557
/// Constants
558-
#[derive(Debug, Copy, Clone, PartialOrd)]
558+
#[derive(Debug, Copy, Clone)]
559559
pub enum Const {
560560
Bool(bool),
561561
U8(u8, UIntStyle),
@@ -566,7 +566,6 @@ pub enum Const {
566566
S16(i16),
567567
S32(i32),
568568
S64(i64),
569-
// TODO: use logical equality for floating point numbers
570569
F32(f32),
571570
F64(f64),
572571
Pos(usize),
@@ -585,15 +584,66 @@ impl PartialEq for Const {
585584
(Const::S16(a), Const::S16(b)) => a == b,
586585
(Const::S32(a), Const::S32(b)) => a == b,
587586
(Const::S64(a), Const::S64(b)) => a == b,
588-
(Const::F32(a), Const::F32(b)) => a == b,
589-
(Const::F64(a), Const::F64(b)) => a == b,
587+
(Const::F32(a), Const::F32(b)) => a.total_cmp(&b).is_eq(),
588+
(Const::F64(a), Const::F64(b)) => a.total_cmp(&b).is_eq(),
590589
(Const::Pos(a), Const::Pos(b)) => a == b,
591590
(Const::Ref(a), Const::Ref(b)) => a == b,
592591
_ => false,
593592
}
594593
}
595594
}
596595

596+
impl Eq for Const {}
597+
598+
impl PartialOrd for Const {
599+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
600+
Some(self.cmp(other))
601+
}
602+
}
603+
604+
impl Ord for Const {
605+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
606+
match (*self, *other) {
607+
(Const::Bool(a), Const::Bool(b)) => a.cmp(&b),
608+
(Const::U8(a, _), Const::U8(b, _)) => a.cmp(&b),
609+
(Const::U16(a, _), Const::U16(b, _)) => a.cmp(&b),
610+
(Const::U32(a, _), Const::U32(b, _)) => a.cmp(&b),
611+
(Const::U64(a, _), Const::U64(b, _)) => a.cmp(&b),
612+
(Const::S8(a), Const::S8(b)) => a.cmp(&b),
613+
(Const::S16(a), Const::S16(b)) => a.cmp(&b),
614+
(Const::S32(a), Const::S32(b)) => a.cmp(&b),
615+
(Const::S64(a), Const::S64(b)) => a.cmp(&b),
616+
(Const::F32(a), Const::F32(b)) => a.total_cmp(&b),
617+
(Const::F64(a), Const::F64(b)) => a.total_cmp(&b),
618+
(Const::Pos(a), Const::Pos(b)) => a.cmp(&b),
619+
(Const::Ref(a), Const::Ref(b)) => a.cmp(&b),
620+
_ => {
621+
fn discriminant(r#const: &Const) -> usize {
622+
match r#const {
623+
Const::Bool(_) => 0,
624+
Const::U8(_, _) => 1,
625+
Const::U16(_, _) => 2,
626+
Const::U32(_, _) => 3,
627+
Const::U64(_, _) => 4,
628+
Const::S8(_) => 5,
629+
Const::S16(_) => 6,
630+
Const::S32(_) => 7,
631+
Const::S64(_) => 8,
632+
Const::F32(_) => 9,
633+
Const::F64(_) => 10,
634+
Const::Pos(_) => 11,
635+
Const::Ref(_) => 12,
636+
}
637+
}
638+
639+
let tag1 = discriminant(self);
640+
let tag2 = discriminant(other);
641+
tag1.cmp(&tag2)
642+
}
643+
}
644+
}
645+
}
646+
597647
pub trait ToBeBytes<const N: usize> {
598648
fn to_be_bytes(self) -> [u8; N];
599649
}
@@ -622,16 +672,16 @@ pub trait UIntStyled<const N: usize>:
622672
impl UIntStyle {
623673
pub fn format<T: UIntStyled<N>, const N: usize>(&self, number: T) -> String {
624674
match self {
625-
UIntStyle::Binary => format!("0b{:b}", number),
675+
UIntStyle::Binary => format!("0b{number:b}"),
626676
UIntStyle::Decimal => number.to_string(),
627-
UIntStyle::Hexadecimal => format!("0x{:x}", number),
677+
UIntStyle::Hexadecimal => format!("0x{number:x}"),
628678
UIntStyle::Ascii => {
629679
let bytes = number.to_be_bytes();
630680
if bytes.iter().all(|c| c.is_ascii() && !c.is_ascii_control()) {
631681
let s = std::str::from_utf8(&bytes).unwrap(); // unwrap safe due to above check
632-
format!("\"{}\"", s)
682+
format!("\"{s}\"")
633683
} else {
634-
format!("0x{:x}", number)
684+
format!("0x{number:x}")
635685
}
636686
}
637687
}

fathom/src/core/binary.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ impl<'arena, 'data> Context<'arena, 'data> {
584584
// especially without succumbing to non-termination, but we'll panic
585585
// here just in case.
586586
if self.lookup_ref(pos, format).is_some() {
587-
panic!("recursion found when storing cached reference {}", pos);
587+
panic!("recursion found when storing cached reference {pos}");
588588
}
589589

590590
// Store the parsed reference in the reference cache

fathom/src/driver.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ impl<'surface, 'core> Driver<'surface, 'core> {
9393
};
9494

9595
let diagnostic = Diagnostic::bug()
96-
.with_message(format!("compiler panicked at '{}'", message))
96+
.with_message(format!("compiler panicked at '{message}'"))
9797
.with_notes(vec![
9898
match location {
99-
Some(location) => format!("panicked at: {}", location),
99+
Some(location) => format!("panicked at: {location}"),
100100
None => "panicked at: unknown location".to_owned(),
101101
},
102-
format!("please file a bug report at: {}", BUG_REPORT_URL),
102+
format!("please file a bug report at: {BUG_REPORT_URL}"),
103103
// TODO: print rust backtrace
104104
// TODO: print fathom backtrace
105105
]);
@@ -403,7 +403,7 @@ impl<'surface, 'core> Driver<'surface, 'core> {
403403

404404
fn emit_read_diagnostic(&self, name: impl std::fmt::Display, error: std::io::Error) {
405405
let diagnostic =
406-
Diagnostic::error().with_message(format!("couldn't read `{}`: {}", name, error));
406+
Diagnostic::error().with_message(format!("couldn't read `{name}`: {error}"));
407407
self.emit_diagnostic(diagnostic);
408408
}
409409

@@ -450,22 +450,20 @@ impl<'surface, 'core> Driver<'surface, 'core> {
450450
.with_notes(vec![format!("option_unwrap was called on a none value.")]),
451451
ReadError::BufferError(span, err) => self.buffer_error_to_diagnostic(err, span),
452452
ReadError::InvalidFormat(span) | ReadError::InvalidValue(span) => Diagnostic::bug()
453-
.with_message(format!("unexpected error '{}'", err))
453+
.with_message(format!("unexpected error '{err}'"))
454454
.with_labels(
455455
IntoIterator::into_iter([label_for_span(&span)])
456456
.into_iter()
457457
.flatten()
458458
.collect(),
459459
)
460460
.with_notes(vec![format!(
461-
"please file a bug report at: {}",
462-
BUG_REPORT_URL
461+
"please file a bug report at: {BUG_REPORT_URL}"
463462
)]),
464463
ReadError::UnknownItem => Diagnostic::bug()
465-
.with_message(format!("unexpected error '{}'", err))
464+
.with_message(format!("unexpected error '{err}'"))
466465
.with_notes(vec![format!(
467-
"please file a bug report at: {}",
468-
BUG_REPORT_URL
466+
"please file a bug report at: {BUG_REPORT_URL}"
469467
)]),
470468
}
471469
}
@@ -492,8 +490,7 @@ impl<'surface, 'core> Driver<'surface, 'core> {
492490
.collect(),
493491
)
494492
.with_notes(vec![format!(
495-
"The offset {} is before the start of the buffer.",
496-
offset
493+
"The offset {offset} is before the start of the buffer."
497494
)]),
498495
BufferError::SetOffsetAfterEndOfBuffer {
499496
offset: Some(offset),
@@ -506,8 +503,7 @@ impl<'surface, 'core> Driver<'surface, 'core> {
506503
.collect(),
507504
)
508505
.with_notes(vec![format!(
509-
"The offset {} is beyond the end of the buffer.",
510-
offset
506+
"The offset {offset} is beyond the end of the buffer."
511507
)]),
512508
BufferError::SetOffsetAfterEndOfBuffer { offset: None } => Diagnostic::error()
513509
.with_message(err.to_string())
@@ -521,16 +517,15 @@ impl<'surface, 'core> Driver<'surface, 'core> {
521517
"The offset is beyond the end of the buffer (overflow).",
522518
)]),
523519
BufferError::PositionOverflow => Diagnostic::bug()
524-
.with_message(format!("unexpected error '{}'", err))
520+
.with_message(format!("unexpected error '{err}'"))
525521
.with_labels(
526522
IntoIterator::into_iter([label_for_span(&span)])
527523
.into_iter()
528524
.flatten()
529525
.collect(),
530526
)
531527
.with_notes(vec![format!(
532-
"please file a bug report at: {}",
533-
BUG_REPORT_URL
528+
"please file a bug report at: {BUG_REPORT_URL}"
534529
)]),
535530
}
536531
}

fathom/src/env.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,11 @@ impl<Entry> SliceEnv<Entry> {
257257

258258
impl<Entry: PartialEq> SliceEnv<Entry> {
259259
pub fn elem_level(&self, entry: &Entry) -> Option<Level> {
260-
Iterator::zip(levels(), self.iter()).find_map(|(var, e)| (entry == e).then(|| var))
260+
Iterator::zip(levels(), self.iter()).find_map(|(var, e)| (entry == e).then_some(var))
261261
}
262262

263263
pub fn elem_index(&self, entry: &Entry) -> Option<Index> {
264-
Iterator::zip(indices(), self.iter().rev()).find_map(|(var, e)| (entry == e).then(|| var))
264+
Iterator::zip(indices(), self.iter().rev()).find_map(|(var, e)| (entry == e).then_some(var))
265265
}
266266
}
267267

fathom/src/source.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl StringInterner {
4747
if max_index >= len {
4848
self.tuple_labels.reserve(max_index.saturating_sub(cap));
4949
for index in len..=max_index {
50-
let label = self.get_or_intern(format!("_{}", index));
50+
let label = self.get_or_intern(format!("_{index}"));
5151
self.tuple_labels.push(label);
5252
}
5353
}

fathom/src/surface.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,11 +459,11 @@ impl ParseMessage {
459459
token,
460460
expected,
461461
} => Diagnostic::error()
462-
.with_message(format!("unexpected token {}", token))
462+
.with_message(format!("unexpected token {token}"))
463463
.with_labels(vec![primary_label(range).with_message("unexpected token")])
464464
.with_notes(format_expected(expected).map_or(Vec::new(), |message| vec![message])),
465465
ParseMessage::ExtraToken { range, token } => Diagnostic::error()
466-
.with_message(format!("extra token {}", token))
466+
.with_message(format!("extra token {token}"))
467467
.with_labels(vec![primary_label(range).with_message("extra token")]),
468468
}
469469
}
@@ -479,7 +479,7 @@ fn format_expected(expected: &[impl std::fmt::Display]) -> Option<String> {
479479
use itertools::Itertools;
480480

481481
expected.split_last().map(|items| match items {
482-
(last, []) => format!("expected {}", last),
482+
(last, []) => format!("expected {last}"),
483483
(last, expected) => format!("expected {} or {}", expected.iter().format(", "), last),
484484
})
485485
}

fathom/src/surface/elaboration.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ impl<'interner, 'arena> Context<'interner, 'arena> {
457457
}
458458

459459
let filtered_fields = (fields.iter().enumerate())
460-
.filter_map(move |(index, field)| (!duplicate_indices.contains(&index)).then(|| field));
460+
.filter_map(move |(index, field)| (!duplicate_indices.contains(&index)).then_some(field));
461461

462462
(labels.into(), filtered_fields)
463463
}

0 commit comments

Comments
 (0)