From 2da721b4a6352cb70ece3401c05075a92e88a9a1 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 11 Sep 2022 14:29:20 +0200 Subject: [PATCH 01/34] Add cargo-clippy workflow job Signed-off-by: Matthias Beyer --- .github/workflows/ci.yml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7b5c8296..267b2707 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -71,6 +71,21 @@ jobs: args: -- --check + clippy: + name: clippy + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions-rs/toolchain@v1 + with: + toolchain: 1.60.0 + override: true + - uses: swatinem/rust-cache@v1 + - run: rustup component add clippy + - name: cargo-clippy + run: cargo clippy --all --all-targets --all-features + + # We need some "accummulation" job here because bors fails (timeouts) to # listen on matrix builds. # Hence, we have some kind of dummy here that bors can listen on @@ -81,6 +96,7 @@ jobs: - check - deny - fmt + - clippy runs-on: ubuntu-latest steps: - name: CI succeeded From 706c2adc541332f9bdcf7b8eb2e8a687cc76ed72 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 07:47:17 +0200 Subject: [PATCH 02/34] Fix clippy: Use field name initialization Signed-off-by: Matthias Beyer --- src/process.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/process.rs b/src/process.rs index 1e891623..f2f60f92 100644 --- a/src/process.rs +++ b/src/process.rs @@ -123,7 +123,7 @@ impl PtyProcess { } ForkResult::Parent { child: child_pid } => Ok(PtyProcess { pty: master_fd, - child_pid: child_pid, + child_pid, kill_timeout: None, }), } From b0b3e16bdb87ba6ad4d22fee65551b4f069abc04 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 07:47:53 +0200 Subject: [PATCH 03/34] Fix clippy: Use .map() instead of .and_then() Signed-off-by: Matthias Beyer --- src/process.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/process.rs b/src/process.rs index f2f60f92..777d6585 100644 --- a/src/process.rs +++ b/src/process.rs @@ -142,7 +142,7 @@ impl PtyProcess { /// the process does not react to a normal kill. If kill_timeout is set the process is /// `kill -9`ed after duration pub fn set_kill_timeout(&mut self, timeout_ms: Option) { - self.kill_timeout = timeout_ms.and_then(|millis| Some(time::Duration::from_millis(millis))); + self.kill_timeout = timeout_ms.map(|millis| time::Duration::from_millis(millis)); } /// Get status of child process, non-blocking. From b2281250e0793b3405101957e1ecc7aa20b95323 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 07:48:35 +0200 Subject: [PATCH 04/34] Fix clippy: Use iflet instead of match Signed-off-by: Matthias Beyer --- src/process.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/process.rs b/src/process.rs index 777d6585..816b0f8f 100644 --- a/src/process.rs +++ b/src/process.rs @@ -228,11 +228,8 @@ impl PtyProcess { impl Drop for PtyProcess { fn drop(&mut self) { - match self.status() { - Some(wait::WaitStatus::StillAlive) => { - self.exit().expect("cannot exit"); - } - _ => {} + if let Some(wait::WaitStatus::StillAlive) = self.status() { + self.exit().expect("cannot exit"); } } } From 8d15394685ad944d1be70b8f17e65b11177676e1 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 07:49:29 +0200 Subject: [PATCH 05/34] Fix clippy: Allow upper case acronym for "EOF" Signed-off-by: Matthias Beyer --- src/reader.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/reader.rs b/src/reader.rs index 1c33803c..0f5bbce2 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -14,6 +14,7 @@ enum PipeError { } #[derive(Debug)] +#[allow(clippy::upper_case_acronyms)] enum PipedChar { Char(u8), EOF, From 204bcf8f87dceab75c0ce0241ec8edbc4f2a7077 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 07:49:54 +0200 Subject: [PATCH 06/34] Fix clippy: Remove redundant closure Signed-off-by: Matthias Beyer --- src/process.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/process.rs b/src/process.rs index 816b0f8f..016c79fa 100644 --- a/src/process.rs +++ b/src/process.rs @@ -142,7 +142,7 @@ impl PtyProcess { /// the process does not react to a normal kill. If kill_timeout is set the process is /// `kill -9`ed after duration pub fn set_kill_timeout(&mut self, timeout_ms: Option) { - self.kill_timeout = timeout_ms.map(|millis| time::Duration::from_millis(millis)); + self.kill_timeout = timeout_ms.map(time::Duration::from_millis); } /// Get status of child process, non-blocking. From cf6c81aa0fc05bb0a6fe9029e6d46e12439dedd3 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 07:50:38 +0200 Subject: [PATCH 07/34] Fix clippy: Remove "&" from match Signed-off-by: Matthias Beyer --- src/reader.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/reader.rs b/src/reader.rs index 0f5bbce2..67639bb0 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -31,13 +31,13 @@ pub enum ReadUntil { impl fmt::Display for ReadUntil { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let printable = match self { - &ReadUntil::String(ref s) if s == "\n" => "\\n (newline)".to_string(), - &ReadUntil::String(ref s) if s == "\r" => "\\r (carriage return)".to_string(), - &ReadUntil::String(ref s) => format!("\"{}\"", s), - &ReadUntil::Regex(ref r) => format!("Regex: \"{}\"", r), - &ReadUntil::EOF => "EOF (End of File)".to_string(), - &ReadUntil::NBytes(n) => format!("reading {} bytes", n), - &ReadUntil::Any(ref v) => { + ReadUntil::String(ref s) if s == "\n" => "\\n (newline)".to_string(), + ReadUntil::String(ref s) if s == "\r" => "\\r (carriage return)".to_string(), + ReadUntil::String(ref s) => format!("\"{}\"", s), + ReadUntil::Regex(ref r) => format!("Regex: \"{}\"", r), + ReadUntil::EOF => "EOF (End of File)".to_string(), + ReadUntil::NBytes(n) => format!("reading {} bytes", n), + ReadUntil::Any(ref v) => { let mut res = Vec::new(); for r in v { res.push(r.to_string()); From 4c8a71a77b228e125b3d2fceb2413ac7bac5d6b3 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 07:50:38 +0200 Subject: [PATCH 08/34] Fix clippy: Remove "&" from match Signed-off-by: Matthias Beyer --- src/reader.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/reader.rs b/src/reader.rs index 67639bb0..7d851346 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -63,24 +63,24 @@ impl fmt::Display for ReadUntil { /// 2. position after match pub fn find(needle: &ReadUntil, buffer: &str, eof: bool) -> Option<(usize, usize)> { match needle { - &ReadUntil::String(ref s) => buffer.find(s).and_then(|pos| Some((pos, pos + s.len()))), - &ReadUntil::Regex(ref pattern) => { + ReadUntil::String(ref s) => buffer.find(s).and_then(|pos| Some((pos, pos + s.len()))), + ReadUntil::Regex(ref pattern) => { if let Some(mat) = pattern.find(buffer) { Some((mat.start(), mat.end())) } else { None } } - &ReadUntil::EOF => { + ReadUntil::EOF => { if eof { Some((0, buffer.len())) } else { None } } - &ReadUntil::NBytes(n) => { - if n <= buffer.len() { - Some((0, n)) + ReadUntil::NBytes(n) => { + if *n <= buffer.len() { + Some((0, *n)) } else if eof && buffer.len() > 0 { // reached almost end of buffer, return string, even though it will be // smaller than the wished n bytes @@ -89,7 +89,7 @@ pub fn find(needle: &ReadUntil, buffer: &str, eof: bool) -> Option<(usize, usize None } } - &ReadUntil::Any(ref any) => { + ReadUntil::Any(ref any) => { for read_until in any { if let Some(pos_tuple) = find(&read_until, buffer, eof) { return Some(pos_tuple); From 41f06182c5b2b9b9c0b5ce9defdb8d940580496f Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 07:51:30 +0200 Subject: [PATCH 09/34] Fix clippy: Use .map() instead of .and_then() Signed-off-by: Matthias Beyer --- src/reader.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/reader.rs b/src/reader.rs index 7d851346..2abb22a6 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -63,7 +63,7 @@ impl fmt::Display for ReadUntil { /// 2. position after match pub fn find(needle: &ReadUntil, buffer: &str, eof: bool) -> Option<(usize, usize)> { match needle { - ReadUntil::String(ref s) => buffer.find(s).and_then(|pos| Some((pos, pos + s.len()))), + ReadUntil::String(ref s) => buffer.find(s).map(|pos| (pos, pos + s.len())), ReadUntil::Regex(ref pattern) => { if let Some(mat) = pattern.find(buffer) { Some((mat.start(), mat.end())) From acc202e1ee31081978e64d6badc2c3ec82e098a1 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 07:52:53 +0200 Subject: [PATCH 10/34] Fix clippy: Remove manual implementation of Option::map Signed-off-by: Matthias Beyer --- src/reader.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/reader.rs b/src/reader.rs index 2abb22a6..bf5f7eff 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -64,13 +64,7 @@ impl fmt::Display for ReadUntil { pub fn find(needle: &ReadUntil, buffer: &str, eof: bool) -> Option<(usize, usize)> { match needle { ReadUntil::String(ref s) => buffer.find(s).map(|pos| (pos, pos + s.len())), - ReadUntil::Regex(ref pattern) => { - if let Some(mat) = pattern.find(buffer) { - Some((mat.start(), mat.end())) - } else { - None - } - } + ReadUntil::Regex(ref pattern) => pattern.find(buffer).map(|mat| (mat.start(), mat.end())), ReadUntil::EOF => { if eof { Some((0, buffer.len())) From ac5d4e9577a10e86fae7f326211af0843fbd2655 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 07:54:43 +0200 Subject: [PATCH 11/34] Fix clippy: Use is_empty() instead of comparing to zero Signed-off-by: Matthias Beyer --- src/reader.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/reader.rs b/src/reader.rs index bf5f7eff..c580d47d 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -75,7 +75,7 @@ pub fn find(needle: &ReadUntil, buffer: &str, eof: bool) -> Option<(usize, usize ReadUntil::NBytes(n) => { if *n <= buffer.len() { Some((0, *n)) - } else if eof && buffer.len() > 0 { + } else if eof && buffer.is_empty() { // reached almost end of buffer, return string, even though it will be // smaller than the wished n bytes Some((0, buffer.len())) From 075ecd8fd11b8c539562e96d10c65536ccdbe778 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 07:55:03 +0200 Subject: [PATCH 12/34] Fix clippy: Remove redundant reference Signed-off-by: Matthias Beyer --- src/reader.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/reader.rs b/src/reader.rs index c580d47d..00a3c2c6 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -85,7 +85,7 @@ pub fn find(needle: &ReadUntil, buffer: &str, eof: bool) -> Option<(usize, usize } ReadUntil::Any(ref any) => { for read_until in any { - if let Some(pos_tuple) = find(&read_until, buffer, eof) { + if let Some(pos_tuple) = find(read_until, buffer, eof) { return Some(pos_tuple); } } From bd966e81d95cb17707846f306b431bc479608175 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 07:55:31 +0200 Subject: [PATCH 13/34] Fix clippy: Use .map() instead of .and_then() Signed-off-by: Matthias Beyer --- src/reader.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/reader.rs b/src/reader.rs index 00a3c2c6..351b15dd 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -150,7 +150,7 @@ impl NBReader { reader: rx, buffer: String::with_capacity(1024), eof: false, - timeout: timeout.and_then(|millis| Some(time::Duration::from_millis(millis))), + timeout: timeout.map(|millis| time::Duration::from_millis(millis)), } } From f9fbe0d757c3c76e88e8187e8bdfb2ba396521e1 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 07:55:46 +0200 Subject: [PATCH 14/34] Fix clippy: Remove redundant closure Signed-off-by: Matthias Beyer --- src/reader.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/reader.rs b/src/reader.rs index 351b15dd..82e55ff8 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -150,7 +150,7 @@ impl NBReader { reader: rx, buffer: String::with_capacity(1024), eof: false, - timeout: timeout.map(|millis| time::Duration::from_millis(millis)), + timeout: timeout.map(time::Duration::from_millis), } } From 6bdd3b33c11bb43f9fddb04ae0d522bdc6b4f303 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 07:56:20 +0200 Subject: [PATCH 15/34] Fix clippy: Use char instead of str for pattern Signed-off-by: Matthias Beyer --- src/reader.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/reader.rs b/src/reader.rs index 82e55ff8..7e223d51 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -247,8 +247,8 @@ impl NBReader { needle.to_string(), self.buffer .clone() - .replace("\n", "`\\n`\n") - .replace("\r", "`\\r`") + .replace('\n', "`\\n`\n") + .replace('\r', "`\\r`") .replace('\u{1b}', "`^`"), timeout, ) From 957025c0c030330ba9c41b5e0651daf5e73f3466 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 07:56:51 +0200 Subject: [PATCH 16/34] Fix clippy: Use .is_empty() instead of comparing .len() to zero Signed-off-by: Matthias Beyer --- src/reader.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/reader.rs b/src/reader.rs index 7e223d51..28cddf74 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -265,7 +265,7 @@ impl NBReader { pub fn try_read(&mut self) -> Option { // discard eventual errors, EOF will be handled in read_until correctly let _ = self.read_into_buffer(); - if self.buffer.len() > 0 { + if self.buffer.is_empty() { self.buffer.drain(..1).last() } else { None From e22289f2551bae566898ad053941baa657b9e669 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 07:57:18 +0200 Subject: [PATCH 17/34] Fix clippy: Write byte instead of casting Signed-off-by: Matthias Beyer --- src/session.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.rs b/src/session.rs index 23c2304c..2eebdaa5 100644 --- a/src/session.rs +++ b/src/session.rs @@ -32,7 +32,7 @@ impl StreamSession { let mut len = self.send(line)?; len += self .writer - .write(&['\n' as u8]) + .write(&[b'\n']) .chain_err(|| "cannot write newline")?; Ok(len) } From ad09bbb09330c5bc6f2145317da55ad9a41b53d2 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 07:57:36 +0200 Subject: [PATCH 18/34] Fix clippy: Use byte instead of casting Signed-off-by: Matthias Beyer --- src/session.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/session.rs b/src/session.rs index 2eebdaa5..4bc06a49 100644 --- a/src/session.rs +++ b/src/session.rs @@ -53,8 +53,8 @@ impl StreamSession { /// E.g. `send_control('c')` sends ctrl-c. Upper/smaller case does not matter. pub fn send_control(&mut self, c: char) -> Result<()> { let code = match c { - 'a'..='z' => c as u8 + 1 - 'a' as u8, - 'A'..='Z' => c as u8 + 1 - 'A' as u8, + 'a'..='z' => c as u8 + 1 - b'a', + 'A'..='Z' => c as u8 + 1 - b'A', '[' => 27, '\\' => 28, ']' => 29, From 27199a8e460ff3a0486d2b6b2daa983337faa14a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 07:58:03 +0200 Subject: [PATCH 19/34] Fix clippy: Use .map() instead of .and_then() Signed-off-by: Matthias Beyer --- src/session.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.rs b/src/session.rs index 4bc06a49..77a3c66c 100644 --- a/src/session.rs +++ b/src/session.rs @@ -105,7 +105,7 @@ impl StreamSession { /// Wait until we see EOF (i.e. child process has terminated) /// Return all the yet unread output pub fn exp_eof(&mut self) -> Result { - self.exp(&ReadUntil::EOF).and_then(|(_, s)| Ok(s)) + self.exp(&ReadUntil::EOF).map(|(_, s)| s) } /// Wait until provided regex is seen on stdout of child process. From 2d2be25458b06e52cf270af00a226a12df8e0fef Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 07:58:43 +0200 Subject: [PATCH 20/34] Fix clippy: Remove unnecessary let binding Signed-off-by: Matthias Beyer --- src/session.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/session.rs b/src/session.rs index 77a3c66c..5edafab2 100644 --- a/src/session.rs +++ b/src/session.rs @@ -116,12 +116,10 @@ impl StreamSession { /// Note that `exp_regex("^foo")` matches the start of the yet consumed output. /// For matching the start of the line use `exp_regex("\nfoo")` pub fn exp_regex(&mut self, regex: &str) -> Result<(String, String)> { - let res = self - .exp(&ReadUntil::Regex( - Regex::new(regex).chain_err(|| "invalid regex")?, - )) - .and_then(|s| Ok(s)); - res + self.exp(&ReadUntil::Regex( + Regex::new(regex).chain_err(|| "invalid regex")?, + )) + .and_then(|s| Ok(s)) } /// Wait until provided string is seen on stdout of child process. From ada1198df2c2204f5e71f63378f6593e4775e96b Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 07:58:03 +0200 Subject: [PATCH 21/34] Fix clippy: Use .map() instead of .and_then() Signed-off-by: Matthias Beyer --- src/session.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.rs b/src/session.rs index 5edafab2..ff8bbbb8 100644 --- a/src/session.rs +++ b/src/session.rs @@ -126,7 +126,7 @@ impl StreamSession { /// Return the yet unread output (without the matched string) pub fn exp_string(&mut self, needle: &str) -> Result { self.exp(&ReadUntil::String(needle.to_string())) - .and_then(|(s, _)| Ok(s)) + .map(|(s, _)| s) } /// Wait until provided char is seen on stdout of child process. From 3b0a5e5d1208de7e85db4bb8e6aca4650a3db640 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 07:58:03 +0200 Subject: [PATCH 22/34] Fix clippy: Use .map() instead of .and_then() Signed-off-by: Matthias Beyer --- src/session.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.rs b/src/session.rs index ff8bbbb8..ab2e84eb 100644 --- a/src/session.rs +++ b/src/session.rs @@ -133,7 +133,7 @@ impl StreamSession { /// Return the yet unread output (without the matched char) pub fn exp_char(&mut self, needle: char) -> Result { self.exp(&ReadUntil::String(needle.to_string())) - .and_then(|(s, _)| Ok(s)) + .map(|(s, _)| s) } /// Wait until any of the provided needles is found. From ce61d9ef3e8d7f8c1fe50f16c4c1e99fb13eeebc Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 07:59:42 +0200 Subject: [PATCH 23/34] Fix clippy: Remove unnecessary .and_then() Signed-off-by: Matthias Beyer --- src/session.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/session.rs b/src/session.rs index ab2e84eb..22ec4b87 100644 --- a/src/session.rs +++ b/src/session.rs @@ -119,7 +119,6 @@ impl StreamSession { self.exp(&ReadUntil::Regex( Regex::new(regex).chain_err(|| "invalid regex")?, )) - .and_then(|s| Ok(s)) } /// Wait until provided string is seen on stdout of child process. From b4afa7c47f65d19cce245b9bf8701bbea65751ca Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 08:00:09 +0200 Subject: [PATCH 24/34] Fix clippy: Remove unnecessary reference Signed-off-by: Matthias Beyer --- src/session.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.rs b/src/session.rs index 22ec4b87..d32f3100 100644 --- a/src/session.rs +++ b/src/session.rs @@ -359,7 +359,7 @@ impl Drop for PtyReplSession { fn drop(&mut self) { if let Some(ref cmd) = self.quit_command { self.pty_session - .send_line(&cmd) + .send_line(cmd) .expect("could not run `exit` on bash process"); } } From 696da5ca402ecb8d3bd7ae243485a3dcf7345d4a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 08:00:52 +0200 Subject: [PATCH 25/34] Fix clippy: Ignore return value Signed-off-by: Matthias Beyer --- src/session.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.rs b/src/session.rs index d32f3100..1301c073 100644 --- a/src/session.rs +++ b/src/session.rs @@ -396,7 +396,7 @@ pub fn spawn_bash(timeout: Option) -> Result { // would set as PS1 and we cannot know when is the right time // to set the new PS1 let mut rcfile = tempfile::NamedTempFile::new().unwrap(); - rcfile + let _ = rcfile .write( b"include () { [[ -f \"$1\" ]] && source \"$1\"; }\n\ include /etc/bash.bashrc\n\ From 9a8caab272ef7a287e445f5ad4e81659cc7ed6f4 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 08:01:20 +0200 Subject: [PATCH 26/34] Fix clippy: Remove unnecessary return keyword Signed-off-by: Matthias Beyer --- src/session.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.rs b/src/session.rs index 1301c073..1198cc49 100644 --- a/src/session.rs +++ b/src/session.rs @@ -411,7 +411,7 @@ pub fn spawn_bash(timeout: Option) -> Result { rcfile .path() .to_str() - .unwrap_or_else(|| return "temp file does not exist".into()), + .unwrap_or_else(|| "temp file does not exist".into()), ]); spawn_command(c, timeout).and_then(|p| { let new_prompt = "[REXPECT_PROMPT>"; From 95b7802930d52188a3ecf0970f08ece5f3857d6f Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 08:01:44 +0200 Subject: [PATCH 27/34] Fix clippy: Remove unnecessary .into() Signed-off-by: Matthias Beyer --- src/session.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.rs b/src/session.rs index 1198cc49..ce4837e8 100644 --- a/src/session.rs +++ b/src/session.rs @@ -411,7 +411,7 @@ pub fn spawn_bash(timeout: Option) -> Result { rcfile .path() .to_str() - .unwrap_or_else(|| "temp file does not exist".into()), + .unwrap_or_else(|| "temp file does not exist"), ]); spawn_command(c, timeout).and_then(|p| { let new_prompt = "[REXPECT_PROMPT>"; From 6e71851d30ac5adbd7e01b438e5cfba205416139 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 08:02:16 +0200 Subject: [PATCH 28/34] Fix clippy: Use .map() instead of .and_then() Signed-off-by: Matthias Beyer --- src/session.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/session.rs b/src/session.rs index ce4837e8..3f688cfc 100644 --- a/src/session.rs +++ b/src/session.rs @@ -436,13 +436,11 @@ pub fn spawn_bash(timeout: Option) -> Result { /// /// This is just a proof of concept implementation (and serves for documentation purposes) pub fn spawn_python(timeout: Option) -> Result { - spawn_command(Command::new("python"), timeout).and_then(|p| { - Ok(PtyReplSession { - prompt: ">>> ".to_string(), - pty_session: p, - quit_command: Some("exit()".to_string()), - echo_on: true, - }) + spawn_command(Command::new("python"), timeout).map(|p| PtyReplSession { + prompt: ">>> ".to_string(), + pty_session: p, + quit_command: Some("exit()".to_string()), + echo_on: true, }) } From dd7f9806d94ebb8eaeff21e7fa59e30b1f04c9bc Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 08:02:54 +0200 Subject: [PATCH 29/34] Fix clippy: Use .subsec_millis() instead of dividing nanos by 1 Mio Signed-off-by: Matthias Beyer --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index aca37ba7..5d1f357d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -107,7 +107,7 @@ pub mod errors { description("The process didn't end within the given timeout") display("Timeout Error: Expected {} but got \"{}\" (after waiting {} ms)", expected, got, (timeout.as_secs() * 1000) as u32 - + timeout.subsec_nanos() / 1_000_000) + + timeout.subsec_millis()) } EmptyProgramName { description("The provided program name is empty.") From 426e5649e5bda520a49e91c12745976c577a23d6 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 08:03:42 +0200 Subject: [PATCH 30/34] Fix clippy: Remove unnecessary let binding Signed-off-by: Matthias Beyer --- src/reader.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/reader.rs b/src/reader.rs index 28cddf74..0b452757 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -126,7 +126,7 @@ impl NBReader { loop { match reader.read(&mut byte) { Ok(0) => { - let _ = tx.send(Ok(PipedChar::EOF)).chain_err(|| "cannot send")?; + tx.send(Ok(PipedChar::EOF)).chain_err(|| "cannot send")?; break; } Ok(_) => { From 7da595ccfc01fd7b03ad94128e9c58ff62262b0d Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 08:04:07 +0200 Subject: [PATCH 31/34] Fix clippy: Use .unwrap_or() instead of .unwrap_or_else() Signed-off-by: Matthias Beyer --- src/session.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/session.rs b/src/session.rs index 3f688cfc..aa4ee1de 100644 --- a/src/session.rs +++ b/src/session.rs @@ -408,10 +408,7 @@ pub fn spawn_bash(timeout: Option) -> Result { let mut c = Command::new("bash"); c.args(&[ "--rcfile", - rcfile - .path() - .to_str() - .unwrap_or_else(|| "temp file does not exist"), + rcfile.path().to_str().unwrap_or("temp file does not exist"), ]); spawn_command(c, timeout).and_then(|p| { let new_prompt = "[REXPECT_PROMPT>"; From 4b051d11caeb404a7fd3dd145d2fe5e0f7780e9f Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 08:12:55 +0200 Subject: [PATCH 32/34] Fix clippy: Remove unnecessary format!() Signed-off-by: Matthias Beyer --- src/session.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/session.rs b/src/session.rs index aa4ee1de..17a9ba9d 100644 --- a/src/session.rs +++ b/src/session.rs @@ -525,7 +525,7 @@ mod tests { ReadUntil::String("Hi".to_string()), ]) { Ok(s) => assert_eq!(("".to_string(), "Hi\r".to_string()), s), - Err(e) => assert!(false, format!("got error: {}", e)), + Err(e) => assert!(false, "got error: {}", e), } Ok(()) }() From 6f03d4866fe0dadc0ebb2591d78db1e07ba32743 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 08:13:33 +0200 Subject: [PATCH 33/34] Fix clippy: Replace assert!(false,... with panic!() Signed-off-by: Matthias Beyer --- src/reader.rs | 4 ++-- src/session.rs | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/reader.rs b/src/reader.rs index 0b452757..ce2bb119 100644 --- a/src/reader.rs +++ b/src/reader.rs @@ -288,9 +288,9 @@ mod tests { ); // check for EOF match r.read_until(&ReadUntil::NBytes(10)) { - Ok(_) => assert!(false), + Ok(_) => panic!(), Err(Error(ErrorKind::EOF(_, _, _), _)) => {} - Err(Error(_, _)) => assert!(false), + Err(Error(_, _)) => panic!(), } } diff --git a/src/session.rs b/src/session.rs index 17a9ba9d..ba9379a2 100644 --- a/src/session.rs +++ b/src/session.rs @@ -476,9 +476,9 @@ mod tests { || -> Result<()> { let mut p = spawn("sleep 3", Some(1000)).expect("cannot run sleep 3"); match p.exp_eof() { - Ok(_) => assert!(false, "should raise Timeout"), + Ok(_) => panic!("should raise Timeout"), Err(Error(ErrorKind::Timeout(_, _, _), _)) => {} - Err(_) => assert!(false, "should raise TimeOut"), + Err(_) => panic!("should raise TimeOut"), } Ok(()) }() @@ -525,7 +525,7 @@ mod tests { ReadUntil::String("Hi".to_string()), ]) { Ok(s) => assert_eq!(("".to_string(), "Hi\r".to_string()), s), - Err(e) => assert!(false, "got error: {}", e), + Err(e) => panic!("got error: {}", e), } Ok(()) }() @@ -536,9 +536,9 @@ mod tests { fn test_expect_empty_command_error() { let p = spawn("", Some(1000)); match p { - Ok(_) => assert!(false, "should raise an error"), + Ok(_) => panic!("should raise an error"), Err(Error(ErrorKind::EmptyProgramName, _)) => {} - Err(_) => assert!(false, "should raise EmptyProgramName"), + Err(_) => panic!("should raise EmptyProgramName"), } } From 8a9a7d995619471094fa56cd1d24d40f8effd7f9 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 12 Sep 2022 08:14:20 +0200 Subject: [PATCH 34/34] Fix clippy: Ignore return value Signed-off-by: Matthias Beyer --- src/process.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/process.rs b/src/process.rs index 016c79fa..0a62c8ce 100644 --- a/src/process.rs +++ b/src/process.rs @@ -250,7 +250,7 @@ mod tests { let f = process.get_file_handle(); let mut writer = LineWriter::new(&f); let mut reader = BufReader::new(&f); - writer.write(b"hello cat\n")?; + let _ = writer.write(b"hello cat\n")?; let mut buf = String::new(); reader.read_line(&mut buf)?; assert_eq!(buf, "hello cat\r\n");