aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2021-12-14 18:38:01 -0500
committerJesse Luehrs <doy@tozt.net>2021-12-14 18:38:40 -0500
commit75d44755bdd5da61e6409fa739e333b1e2b9a1da (patch)
treeba22c47415e27841f24850da5c4b1597248c8fec /src
parenta50aafc38b07359bd4c7bf88f118cdbcdd561790 (diff)
downloadttyrec-75d44755bdd5da61e6409fa739e333b1e2b9a1da.tar.gz
ttyrec-75d44755bdd5da61e6409fa739e333b1e2b9a1da.zip
clippy
Diffstat (limited to 'src')
-rw-r--r--src/blocking/reader.rs6
-rw-r--r--src/creator.rs16
-rw-r--r--src/frame.rs1
-rw-r--r--src/lib.rs13
-rw-r--r--src/parser.rs50
-rw-r--r--src/reader.rs6
6 files changed, 58 insertions, 34 deletions
diff --git a/src/blocking/reader.rs b/src/blocking/reader.rs
index 609c393..f4a51a8 100644
--- a/src/blocking/reader.rs
+++ b/src/blocking/reader.rs
@@ -33,7 +33,11 @@ impl<T: std::io::Read> Reader<T> {
if bytes == 0 {
return Err(crate::Error::EOF);
}
- self.parser.add_bytes(&self.buf[..bytes]);
+ self.parser.add_bytes(
+ // read() returning a value means that that many bytes are
+ // guaranteed to be available
+ self.buf.get(..bytes).unwrap_or_else(|| unreachable!()),
+ );
}
}
diff --git a/src/creator.rs b/src/creator.rs
index 590d282..ff44b81 100644
--- a/src/creator.rs
+++ b/src/creator.rs
@@ -4,7 +4,7 @@
/// [`Frame`](crate::Frame) objects, which can be serialized to bytes using
/// their `try_from` implementation, and then a ttyrec file can be generated
/// by concatenating those byte strings.
-#[derive(Debug, Clone)]
+#[derive(Debug, Clone, Default)]
pub struct Creator {
base_time: Option<std::time::Instant>,
}
@@ -31,21 +31,16 @@ impl Creator {
/// Note that this is not guaranteed to do the correct thing unless the
/// `cur_time` parameters given in each [`frame_at`](Self::frame_at) call
/// are non-decreasing.
- #[allow(clippy::missing_panics_doc)]
pub fn frame_at(
&mut self,
cur_time: std::time::Instant,
data: &[u8],
) -> crate::frame::Frame {
- // this is triggering incorrectly - the code transformation it
- // suggests doesn't work due to lifetime issues
- #[allow(clippy::option_if_let_else)]
let base_time = if let Some(base_time) = &self.base_time {
base_time
} else {
self.base_time = Some(cur_time);
- // this unwrap is safe because we just set the value to Some
- self.base_time.as_ref().unwrap()
+ &cur_time
};
crate::frame::Frame {
time: cur_time - *base_time,
@@ -54,13 +49,8 @@ impl Creator {
}
}
-impl Default for Creator {
- fn default() -> Self {
- Self { base_time: None }
- }
-}
-
#[cfg(test)]
+#[allow(clippy::unwrap_used)]
mod test {
use super::*;
diff --git a/src/frame.rs b/src/frame.rs
index 5996409..af5fa1c 100644
--- a/src/frame.rs
+++ b/src/frame.rs
@@ -43,6 +43,7 @@ impl TryFrom<Frame> for Vec<u8> {
}
#[cfg(test)]
+#[allow(clippy::unwrap_used)]
mod test {
use super::*;
diff --git a/src/lib.rs b/src/lib.rs
index c7a43ec..9d22842 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -11,12 +11,19 @@
//! by building with `default_features = false` (by default, the `"async"`
//! feature is enabled).
-// XXX this is broken with ale
-// #![warn(clippy::cargo)]
+#![warn(clippy::cargo)]
#![warn(clippy::pedantic)]
#![warn(clippy::nursery)]
+#![warn(clippy::unwrap_used)]
+#![warn(clippy::expect_used)]
+#![warn(clippy::indexing_slicing)]
+#![warn(clippy::as_conversions)]
+#![allow(clippy::cognitive_complexity)]
#![allow(clippy::missing_const_for_fn)]
-#![allow(clippy::multiple_crate_versions)]
+#![allow(clippy::similar_names)]
+#![allow(clippy::struct_excessive_bools)]
+#![allow(clippy::too_many_arguments)]
+#![allow(clippy::too_many_lines)]
mod creator;
pub use creator::Creator;
diff --git a/src/parser.rs b/src/parser.rs
index 9887b38..37c5118 100644
--- a/src/parser.rs
+++ b/src/parser.rs
@@ -14,7 +14,9 @@ impl Header {
}
fn len(&self) -> usize {
- self.len as usize
+ usize::try_from(self.len).unwrap_or_else(|_| {
+ panic!("this library requires sizeof(usize) to be at least 4")
+ })
}
}
@@ -50,7 +52,6 @@ impl Parser {
/// If a complete frame is found, the bytes for that frame will be removed
/// from the internal buffer and the frame object will be returned. If a
/// complete frame is not found, this method will return [`None`].
- #[allow(clippy::missing_panics_doc)]
pub fn next_frame(&mut self) -> Option<crate::frame::Frame> {
let header = if let Some(header) = &self.read_state {
header
@@ -60,28 +61,41 @@ impl Parser {
}
// these unwraps are guaranteed safe by the length check above
- let secs1 = self.reading.pop_front().unwrap();
- let secs2 = self.reading.pop_front().unwrap();
- let secs3 = self.reading.pop_front().unwrap();
- let secs4 = self.reading.pop_front().unwrap();
+ let secs1 =
+ self.reading.pop_front().unwrap_or_else(|| unreachable!());
+ let secs2 =
+ self.reading.pop_front().unwrap_or_else(|| unreachable!());
+ let secs3 =
+ self.reading.pop_front().unwrap_or_else(|| unreachable!());
+ let secs4 =
+ self.reading.pop_front().unwrap_or_else(|| unreachable!());
let secs = u32::from_le_bytes([secs1, secs2, secs3, secs4]);
- let micros1 = self.reading.pop_front().unwrap();
- let micros2 = self.reading.pop_front().unwrap();
- let micros3 = self.reading.pop_front().unwrap();
- let micros4 = self.reading.pop_front().unwrap();
+ let micros1 =
+ self.reading.pop_front().unwrap_or_else(|| unreachable!());
+ let micros2 =
+ self.reading.pop_front().unwrap_or_else(|| unreachable!());
+ let micros3 =
+ self.reading.pop_front().unwrap_or_else(|| unreachable!());
+ let micros4 =
+ self.reading.pop_front().unwrap_or_else(|| unreachable!());
let micros =
u32::from_le_bytes([micros1, micros2, micros3, micros4]);
- let len1 = self.reading.pop_front().unwrap();
- let len2 = self.reading.pop_front().unwrap();
- let len3 = self.reading.pop_front().unwrap();
- let len4 = self.reading.pop_front().unwrap();
+ let len1 =
+ self.reading.pop_front().unwrap_or_else(|| unreachable!());
+ let len2 =
+ self.reading.pop_front().unwrap_or_else(|| unreachable!());
+ let len3 =
+ self.reading.pop_front().unwrap_or_else(|| unreachable!());
+ let len4 =
+ self.reading.pop_front().unwrap_or_else(|| unreachable!());
let len = u32::from_le_bytes([len1, len2, len3, len4]);
let header = Header { secs, micros, len };
self.read_state = Some(header);
- self.read_state.as_ref().unwrap()
+ // unwrap is safe because we just set self.read_state to Some
+ self.read_state.as_ref().unwrap_or_else(|| unreachable!())
};
if self.reading.len() < header.len() {
@@ -90,7 +104,11 @@ impl Parser {
let mut data = vec![];
for _ in 0..header.len() {
- data.push(self.reading.pop_front().unwrap());
+ data.push(
+ // unwrap is safe because we just checked that there are
+ // sufficient bytes in self.reading
+ self.reading.pop_front().unwrap_or_else(|| unreachable!()),
+ );
}
let time = header.time();
diff --git a/src/reader.rs b/src/reader.rs
index 3d963f1..478af36 100644
--- a/src/reader.rs
+++ b/src/reader.rs
@@ -37,7 +37,11 @@ impl<T: futures_lite::io::AsyncRead + std::marker::Unpin + Send> Reader<T> {
if bytes == 0 {
return Err(crate::Error::EOF);
}
- self.parser.add_bytes(&self.buf[..bytes]);
+ self.parser.add_bytes(
+ // read() returning a value means that that many bytes are
+ // guaranteed to be available
+ self.buf.get(..bytes).unwrap_or_else(|| unreachable!()),
+ );
}
}