From 31b3998d9188ffdfa0d23d508975f5cf36e1f4fa Mon Sep 17 00:00:00 2001 From: Josh McKinney Date: Thu, 21 Nov 2024 16:52:25 -0800 Subject: [PATCH] Fix warnings and clippy lints (#943) * Move InternalEventFilter into tests mod This is only used in test code, so should be defined there * Remove winapi feature flag checks These checks were impossible to trigger as there is no separate feature flag for winapi and crossterm_winapi. The windows feature flag automatically enables these dependencies. * Fix clippy lints for byte char slices https://rust-lang.github.io/rust-clippy/master/index.html\#byte_char_slices --- src/event/filter.rs | 20 ++++++++++---------- src/event/read.rs | 14 ++++++++++---- src/event/sys/unix/parse.rs | 36 ++++++++++++++++++------------------ src/lib.rs | 6 ------ 4 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/event/filter.rs b/src/event/filter.rs index f78730dcd..cd7abce2b 100644 --- a/src/event/filter.rs +++ b/src/event/filter.rs @@ -61,23 +61,23 @@ impl Filter for EventFilter { } } -#[derive(Debug, Clone)] -pub(crate) struct InternalEventFilter; - -impl Filter for InternalEventFilter { - fn eval(&self, _: &InternalEvent) -> bool { - true - } -} - #[cfg(test)] #[cfg(unix)] mod tests { use super::{ super::Event, CursorPositionFilter, EventFilter, Filter, InternalEvent, - InternalEventFilter, KeyboardEnhancementFlagsFilter, PrimaryDeviceAttributesFilter, + KeyboardEnhancementFlagsFilter, PrimaryDeviceAttributesFilter, }; + #[derive(Debug, Clone)] + pub(crate) struct InternalEventFilter; + + impl Filter for InternalEventFilter { + fn eval(&self, _: &InternalEvent) -> bool { + true + } + } + #[test] fn test_cursor_position_filter_filters_cursor_position() { assert!(!CursorPositionFilter.eval(&InternalEvent::Event(Event::Resize(10, 10)))); diff --git a/src/event/read.rs b/src/event/read.rs index 6ddbacef6..22f989b4d 100644 --- a/src/event/read.rs +++ b/src/event/read.rs @@ -132,10 +132,16 @@ mod tests { #[cfg(unix)] use super::super::filter::CursorPositionFilter; - use super::{ - super::{filter::InternalEventFilter, Event}, - EventSource, InternalEvent, InternalEventReader, - }; + use super::{super::Event, EventSource, Filter, InternalEvent, InternalEventReader}; + + #[derive(Debug, Clone)] + pub(crate) struct InternalEventFilter; + + impl Filter for InternalEventFilter { + fn eval(&self, _: &InternalEvent) -> bool { + true + } + } #[test] fn test_poll_fails_without_event_source() { diff --git a/src/event/sys/unix/parse.rs b/src/event/sys/unix/parse.rs index 2019b5f22..29377438f 100644 --- a/src/event/sys/unix/parse.rs +++ b/src/event/sys/unix/parse.rs @@ -135,7 +135,7 @@ fn char_code_to_event(code: KeyCode) -> KeyEvent { } pub(crate) fn parse_csi(buffer: &[u8]) -> io::Result> { - assert!(buffer.starts_with(&[b'\x1B', b'['])); // ESC [ + assert!(buffer.starts_with(b"\x1B[")); // ESC [ if buffer.len() == 2 { return Ok(None); @@ -242,8 +242,8 @@ pub(crate) fn parse_csi_cursor_position(buffer: &[u8]) -> io::Result io::Result io::Result> { // ESC [ ? flags u - assert!(buffer.starts_with(&[b'\x1B', b'[', b'?'])); // ESC [ ? - assert!(buffer.ends_with(&[b'u'])); + assert!(buffer.starts_with(b"\x1B[?")); // ESC [ ? + assert!(buffer.ends_with(b"u")); if buffer.len() < 5 { return Ok(None); @@ -290,8 +290,8 @@ fn parse_csi_keyboard_enhancement_flags(buffer: &[u8]) -> io::Result io::Result> { // ESC [ 64 ; attr1 ; attr2 ; ... ; attrn ; c - assert!(buffer.starts_with(&[b'\x1B', b'[', b'?'])); - assert!(buffer.ends_with(&[b'c'])); + assert!(buffer.starts_with(b"\x1B[?")); + assert!(buffer.ends_with(b"c")); // This is a stub for parsing the primary device attributes. This response is not // exposed in the crossterm API so we don't need to parse the individual attributes yet. @@ -346,8 +346,8 @@ fn parse_key_event_kind(kind: u8) -> KeyEventKind { } pub(crate) fn parse_csi_modifier_key_code(buffer: &[u8]) -> io::Result> { - assert!(buffer.starts_with(&[b'\x1B', b'['])); // ESC [ - // + assert!(buffer.starts_with(b"\x1B[")); // ESC [ + // let s = std::str::from_utf8(&buffer[2..buffer.len() - 1]) .map_err(|_| could_not_parse_event_error())?; let mut split = s.split(';'); @@ -495,8 +495,8 @@ fn translate_functional_key_code(codepoint: u32) -> Option<(KeyCode, KeyEventSta } pub(crate) fn parse_csi_u_encoded_key_code(buffer: &[u8]) -> io::Result> { - assert!(buffer.starts_with(&[b'\x1B', b'['])); // ESC [ - assert!(buffer.ends_with(&[b'u'])); + assert!(buffer.starts_with(b"\x1B[")); // ESC [ + assert!(buffer.ends_with(b"u")); // This function parses `CSI … u` sequences. These are sequences defined in either // the `CSI u` (a.k.a. "Fix Keyboard Input on Terminals - Please", https://www.leonerd.org.uk/hacks/fixterms/) @@ -617,8 +617,8 @@ pub(crate) fn parse_csi_u_encoded_key_code(buffer: &[u8]) -> io::Result io::Result> { - assert!(buffer.starts_with(&[b'\x1B', b'['])); // ESC [ - assert!(buffer.ends_with(&[b'~'])); + assert!(buffer.starts_with(b"\x1B[")); // ESC [ + assert!(buffer.ends_with(b"~")); let s = std::str::from_utf8(&buffer[2..buffer.len() - 1]) .map_err(|_| could_not_parse_event_error())?; @@ -664,8 +664,8 @@ pub(crate) fn parse_csi_rxvt_mouse(buffer: &[u8]) -> io::Result io::Result io::Result> { // Normal mouse encoding: ESC [ M CB Cx Cy (6 characters only). - assert!(buffer.starts_with(&[b'\x1B', b'[', b'M'])); // ESC [ M + assert!(buffer.starts_with(b"\x1B[M")); // ESC [ M if buffer.len() < 6 { return Ok(None); @@ -718,9 +718,9 @@ pub(crate) fn parse_csi_normal_mouse(buffer: &[u8]) -> io::Result io::Result> { // ESC [ < Cb ; Cx ; Cy (;) (M or m) - assert!(buffer.starts_with(&[b'\x1B', b'[', b'<'])); // ESC [ < + assert!(buffer.starts_with(b"\x1B[<")); // ESC [ < - if !buffer.ends_with(&[b'm']) && !buffer.ends_with(&[b'M']) { + if !buffer.ends_with(b"m") && !buffer.ends_with(b"M") { return Ok(None); } diff --git a/src/lib.rs b/src/lib.rs index 0217bef1c..453382a7c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -252,9 +252,3 @@ pub(crate) mod macros; #[cfg(all(windows, not(feature = "windows")))] compile_error!("Compiling on Windows with \"windows\" feature disabled. Feature \"windows\" should only be disabled when project will never be compiled on Windows."); - -#[cfg(all(winapi, not(feature = "winapi")))] -compile_error!("Compiling on Windows with \"winapi\" feature disabled. Feature \"winapi\" should only be disabled when project will never be compiled on Windows."); - -#[cfg(all(crossterm_winapi, not(feature = "crossterm_winapi")))] -compile_error!("Compiling on Windows with \"crossterm_winapi\" feature disabled. Feature \"crossterm_winapi\" should only be disabled when project will never be compiled on Windows.");