From 68346699e1bcdc552cddfc513acc45c4bd0e078f Mon Sep 17 00:00:00 2001 From: Zrzka Date: Thu, 5 Sep 2019 16:13:23 +0200 Subject: [PATCH] Warnings cleanup (#198) --- crossterm_input/src/input/input.rs | 2 +- crossterm_input/src/input/unix_input.rs | 20 +++--- crossterm_utils/src/command.rs | 12 ++-- crossterm_utils/src/macros.rs | 83 +++++++++++++------------ examples/command.rs | 24 +++---- examples/crossterm.rs | 3 + examples/style.rs | 2 - examples/terminal.rs | 2 +- 8 files changed, 76 insertions(+), 72 deletions(-) diff --git a/crossterm_input/src/input/input.rs b/crossterm_input/src/input/input.rs index 7b9f278..bab8717 100644 --- a/crossterm_input/src/input/input.rs +++ b/crossterm_input/src/input/input.rs @@ -53,7 +53,7 @@ impl TerminalInput { pub fn read_line(&self) -> io::Result { let mut rv = String::new(); io::stdin().read_line(&mut rv)?; - let len = rv.trim_right_matches(&['\r', '\n'][..]).len(); + let len = rv.trim_end_matches(&['\r', '\n'][..]).len(); rv.truncate(len); Ok(rv) } diff --git a/crossterm_input/src/input/unix_input.rs b/crossterm_input/src/input/unix_input.rs index eb2857f..90456db 100644 --- a/crossterm_input/src/input/unix_input.rs +++ b/crossterm_input/src/input/unix_input.rs @@ -100,7 +100,7 @@ pub struct AsyncReader { impl AsyncReader { /// Construct a new instance of the `AsyncReader`. /// The reading will immediately start when calling this function. - pub fn new(function: Box, &Arc) + Send>) -> AsyncReader { + pub fn new(function: Box, &Arc) + Send>) -> AsyncReader { let shutdown_handle = Arc::new(AtomicBool::new(false)); let (event_tx, event_rx) = mpsc::channel(); @@ -242,7 +242,7 @@ where Some(b'O') => { match iter.next() { // F1-F4 - Some(val @ b'P'...b'S') => { + Some(val @ b'P'..=b'S') => { InputEvent::Keyboard(KeyEvent::F(1 + val - b'P')) } _ => return Err(error), @@ -263,10 +263,10 @@ where b'\n' | b'\r' => InputEvent::Keyboard(KeyEvent::Char('\n')), b'\t' => InputEvent::Keyboard(KeyEvent::Char('\t')), b'\x7F' => InputEvent::Keyboard(KeyEvent::Backspace), - c @ b'\x01'...b'\x1A' => { + c @ b'\x01'..=b'\x1A' => { InputEvent::Keyboard(KeyEvent::Ctrl((c as u8 - 0x1 + b'a') as char)) } - c @ b'\x1C'...b'\x1F' => { + c @ b'\x1C'..=b'\x1F' => { InputEvent::Keyboard(KeyEvent::Ctrl((c as u8 - 0x1C + b'4') as char)) } b'\0' => InputEvent::Keyboard(KeyEvent::Null), @@ -290,7 +290,7 @@ where Some(b'[') => match iter.next() { // NOTE (@imdaveho): cannot find when this occurs; // having another '[' after ESC[ not a likely scenario - Some(val @ b'A'...b'E') => InputEvent::Keyboard(KeyEvent::F(1 + val - b'A')), + Some(val @ b'A'..=b'E') => InputEvent::Keyboard(KeyEvent::F(1 + val - b'A')), _ => InputEvent::Unknown, }, Some(b'D') => InputEvent::Keyboard(KeyEvent::Left), @@ -350,7 +350,7 @@ where let cy = nums.next().unwrap().parse::().unwrap(); match cb { - 0...2 | 64...65 => { + 0..=2 | 64..=65 => { let button = match cb { 0 => MouseButton::Left, 1 => MouseButton::Middle, @@ -370,7 +370,7 @@ where _ => InputEvent::Unknown, } } - Some(c @ b'0'...b'9') => { + Some(c @ b'0'..=b'9') => { // Numbered escape code. let mut buf = Vec::new(); buf.push(c); @@ -430,9 +430,9 @@ where 4 | 8 => InputEvent::Keyboard(KeyEvent::End), 5 => InputEvent::Keyboard(KeyEvent::PageUp), 6 => InputEvent::Keyboard(KeyEvent::PageDown), - v @ 11...15 => InputEvent::Keyboard(KeyEvent::F(v - 10)), - v @ 17...21 => InputEvent::Keyboard(KeyEvent::F(v - 11)), - v @ 23...24 => InputEvent::Keyboard(KeyEvent::F(v - 12)), + v @ 11..=15 => InputEvent::Keyboard(KeyEvent::F(v - 10)), + v @ 17..=21 => InputEvent::Keyboard(KeyEvent::F(v - 11)), + v @ 23..=24 => InputEvent::Keyboard(KeyEvent::F(v - 12)), _ => InputEvent::Unknown, } } diff --git a/crossterm_utils/src/command.rs b/crossterm_utils/src/command.rs index 8202961..9c07b41 100644 --- a/crossterm_utils/src/command.rs +++ b/crossterm_utils/src/command.rs @@ -1,4 +1,6 @@ -use crate::{execute, impl_display, queue, write_cout, Result}; +#[cfg(windows)] +use crate::Result; +use crate::{execute, impl_display, queue, write_cout}; use std::fmt::Display; use std::io::Write; @@ -31,13 +33,13 @@ pub trait Command { /// This can be used in order to get more performance. pub trait QueueableCommand { /// Queues the given command for later execution. - fn queue(mut self, command: impl Command) -> Self; + fn queue(self, command: impl Command) -> Self; } /// A trait that defines behaviour for a command that will be executed immediately. pub trait ExecutableCommand { /// Execute the given command directly. - fn execute(mut self, command: impl Command) -> Self; + fn execute(self, command: impl Command) -> Self; } impl QueueableCommand for T @@ -66,7 +68,7 @@ where /// Because of that there is no difference between `execute` and `queue` for those windows versions. /// - Queuing might sound that there is some scheduling going on, however, this means that we write to the stdout without flushing which will cause commands to be stored in the buffer without them being written to the terminal. fn queue(mut self, command: impl Command) -> Self { - queue!(self, command); + let _ = queue!(self, command); self } } @@ -90,7 +92,7 @@ where /// This is happening because Windows versions lower then 10 do not support ANSI codes, and thus they can't be written to the given buffer. /// Because of that there is no difference between `execute` and `queue` for those windows versions. fn execute(mut self, command: impl Command) -> Self { - execute!(self, command); + let _ = execute!(self, command); self } } diff --git a/crossterm_utils/src/macros.rs b/crossterm_utils/src/macros.rs index 2f8bcc8..52d545b 100644 --- a/crossterm_utils/src/macros.rs +++ b/crossterm_utils/src/macros.rs @@ -1,5 +1,4 @@ /// Append a the first few characters of an ANSI escape code to the given string. - #[macro_export] macro_rules! csi { ($( $l:expr ),*) => { concat!("\x1B[", $( $l ),*) }; @@ -66,42 +65,43 @@ macro_rules! write_cout { /// - Queuing might sound that there is some scheduling going on, however, this means that we write to the stdout without flushing which will cause commands to be stored in the buffer without them being written to the terminal. #[macro_export] macro_rules! queue { - ($write:expr, $($command:expr), *) => - {{ + ($write:expr, $($command:expr), *) => {{ + // Silent warning when the macro is used inside the `command` module + #[allow(unused_imports)] use $crate::Command; let mut error = None; $( #[cfg(windows)] - { + { if $crate::supports_ansi() { - match write!($write, "{}",$command.get_ansi_code()) { + match write!($write, "{}", $command.get_ansi_code()) { Err(e) => { - error = Some(Err($crate::ErrorKind::from(e))); + error = Some(Err($crate::ErrorKind::from(e))); } _ => {} - }; + }; } else { - match $command.execute_winapi() { - Err(e) => { - error = Some(Err($crate::ErrorKind::from(e))); - } - _ => {} - }; + match $command.execute_winapi() { + Err(e) => { + error = Some(Err($crate::ErrorKind::from(e))); + } + _ => {} + }; }; } - #[cfg(unix)] - match write!($write, "{}",$command.get_ansi_code()) { - Err(e) => { - error = Some(Err($crate::ErrorKind::from(e))); - } - _ => {} - }; + #[cfg(unix)] + match write!($write, "{}", $command.get_ansi_code()) { + Err(e) => { + error = Some(Err($crate::ErrorKind::from(e))); + } + _ => {} + }; )* if let Some(error) = error { error - }else { + } else { Ok(()) } }} @@ -137,42 +137,43 @@ macro_rules! queue { /// Because of that there is no difference between `execute` and `queue` for those windows versions. #[macro_export] macro_rules! execute { - ($write:expr, $($command:expr), *) => - {{ - use $crate::{Command, write_cout}; + ($write:expr, $($command:expr), *) => {{ + // Silent warning when the macro is used inside the `command` module + #[allow(unused_imports)] + use $crate::{Command, write_cout}; let mut error = None; $( #[cfg(windows)] - { + { if $crate::supports_ansi() { - match write_cout!($write, $command.get_ansi_code()) { + match write_cout!($write, $command.get_ansi_code()) { Err(e) => { error = Some(Err($crate::ErrorKind::from(e))); } _ => {} - }; + }; } else { - match $command.execute_winapi() { - Err(e) => { - error = Some(Err($crate::ErrorKind::from(e))); - } - _ => {} - }; + match $command.execute_winapi() { + Err(e) => { + error = Some(Err($crate::ErrorKind::from(e))); + } + _ => {} + }; }; } - #[cfg(unix)] - match write_cout!($write, $command.get_ansi_code()) { - Err(e) => { - error = Some(Err($crate::ErrorKind::from(e))); - } - _ => {} - }; + #[cfg(unix)] + match write_cout!($write, $command.get_ansi_code()) { + Err(e) => { + error = Some(Err($crate::ErrorKind::from(e))); + } + _ => {} + }; )* if let Some(error) = error { error - }else { + } else { Ok(()) } }} diff --git a/examples/command.rs b/examples/command.rs index e5c53fc..7d36758 100644 --- a/examples/command.rs +++ b/examples/command.rs @@ -1,19 +1,19 @@ +#![allow(dead_code)] + extern crate crossterm; use crossterm::{ - execute, queue, Clear, ClearType, Color, Colorize, ExecutableCommand, Goto, Output, - PrintStyledFont, QueueableCommand, + execute, queue, Clear, ClearType, ExecutableCommand, Goto, Output, QueueableCommand, }; -use std::fmt::Display; -use std::io::{stdout, Stdout, Write}; +use std::io::{stdout, Write}; /// execute commands by using normal functions fn execute_command_directly_using_functions() { // single command - stdout().execute(Output("Text1 ".to_string())); + let _ = stdout().execute(Output("Text1 ".to_string())); // multiple commands - stdout() + let _ = stdout() .execute(Output("Text2 ".to_string())) .execute(Output("Text3 ".to_string())); } @@ -21,10 +21,10 @@ fn execute_command_directly_using_functions() { /// execute commands by using macro's fn execute_command_directly_using_macros() { // single command - execute!(stdout(), Output("Text1 ".to_string())); + let _ = execute!(stdout(), Output("Text1 ".to_string())); // multiple commands - execute!( + let _ = execute!( stdout(), Output("Text2 ".to_string()), Output("Text 3".to_string()) @@ -49,7 +49,7 @@ fn later_execution_command_using_functions() { ::std::thread::sleep(std::time::Duration::from_millis(2000)); // when you call this all commands will be executed - sdout.flush(); + let _ = sdout.flush(); } /// queue commands without executing them directly by using macro's @@ -57,10 +57,10 @@ fn later_execution_command_directly_using_macros() { let mut stdout = stdout(); // single command - queue!(stdout, Output("Text1 ".to_string())); + let _ = queue!(stdout, Output("Text1 ".to_string())); // multiple commands - queue!( + let _ = queue!( stdout, Clear(ClearType::All), Goto(5, 5), @@ -70,7 +70,7 @@ fn later_execution_command_directly_using_macros() { ::std::thread::sleep(std::time::Duration::from_millis(2000)); // when you call this all commands will be executed - stdout.flush(); + let _ = stdout.flush(); } fn main() {} diff --git a/examples/crossterm.rs b/examples/crossterm.rs index 7984a51..ea12cce 100644 --- a/examples/crossterm.rs +++ b/examples/crossterm.rs @@ -1,3 +1,6 @@ +// Remove once the TODO below is fixed +#![allow(unused_variables)] + extern crate crossterm; use crossterm::{Color, Crossterm}; diff --git a/examples/style.rs b/examples/style.rs index 8330221..88a0733 100644 --- a/examples/style.rs +++ b/examples/style.rs @@ -4,7 +4,6 @@ extern crate crossterm; use self::crossterm::{color, Attribute, Color, Colored, Colorize, Styler}; -use std::io::stdout; /// print some red text | demonstration. pub fn paint_foreground() { @@ -412,6 +411,5 @@ pub fn reset_fg_and_bg() { } fn main() { - use std::io::Write; print_all_background_colors_with_method() } diff --git a/examples/terminal.rs b/examples/terminal.rs index e709ab3..1148c92 100644 --- a/examples/terminal.rs +++ b/examples/terminal.rs @@ -136,5 +136,5 @@ pub fn exit() { } fn main() { - scroll_down(); + let _ = scroll_down(); }