From 6069d89dee8e43b99c3938c2f747c5274ce4aef0 Mon Sep 17 00:00:00 2001 From: Florian Dehau Date: Thu, 23 Dec 2021 18:46:25 +0100 Subject: [PATCH] chore: fix all clippy warnings --- examples/barchart.rs | 7 +++---- examples/block.rs | 5 ++--- examples/chart.rs | 7 +++---- examples/custom_widget.rs | 12 +++--------- examples/demo/termion.rs | 10 ++++------ examples/gauge.rs | 7 +++---- examples/layout.rs | 5 ++--- examples/paragraph.rs | 7 +++---- examples/popup.rs | 2 +- examples/sparkline.rs | 7 +++---- examples/tabs.rs | 2 +- examples/user_input.rs | 2 +- src/buffer.rs | 11 +---------- src/layout.rs | 13 +------------ src/text.rs | 16 ++-------------- src/widgets/canvas/world.rs | 1 + src/widgets/chart.rs | 17 +---------------- src/widgets/list.rs | 11 +---------- src/widgets/reflow.rs | 4 ++-- src/widgets/table.rs | 11 +---------- 20 files changed, 39 insertions(+), 118 deletions(-) diff --git a/examples/barchart.rs b/examples/barchart.rs index 9109589..8708d48 100644 --- a/examples/barchart.rs +++ b/examples/barchart.rs @@ -94,16 +94,15 @@ fn run_app( ) -> io::Result<()> { let mut last_tick = Instant::now(); loop { - terminal.draw(|f| ui(f, &mut app))?; + terminal.draw(|f| ui(f, &app))?; let timeout = tick_rate .checked_sub(last_tick.elapsed()) .unwrap_or_else(|| Duration::from_secs(0)); if crossterm::event::poll(timeout)? { if let Event::Key(key) = event::read()? { - match key.code { - KeyCode::Char('q') => return Ok(()), - _ => {} + if let KeyCode::Char('q') = key.code { + return Ok(()); } } } diff --git a/examples/block.rs b/examples/block.rs index 60c82d4..27827ee 100644 --- a/examples/block.rs +++ b/examples/block.rs @@ -45,9 +45,8 @@ fn run_app(terminal: &mut Terminal) -> io::Result<()> { terminal.draw(ui)?; if let Event::Key(key) = event::read()? { - match key.code { - KeyCode::Char('q') => return Ok(()), - _ => {} + if let KeyCode::Char('q') = key.code { + return Ok(()); } } } diff --git a/examples/chart.rs b/examples/chart.rs index 9e574ac..c81cbdf 100644 --- a/examples/chart.rs +++ b/examples/chart.rs @@ -130,16 +130,15 @@ fn run_app( ) -> io::Result<()> { let mut last_tick = Instant::now(); loop { - terminal.draw(|f| ui(f, &mut app))?; + terminal.draw(|f| ui(f, &app))?; let timeout = tick_rate .checked_sub(last_tick.elapsed()) .unwrap_or_else(|| Duration::from_secs(0)); if crossterm::event::poll(timeout)? { if let Event::Key(key) = event::read()? { - match key.code { - KeyCode::Char('q') => return Ok(()), - _ => {} + if let KeyCode::Char('q') = key.code { + return Ok(()); } } } diff --git a/examples/custom_widget.rs b/examples/custom_widget.rs index 8e7b48c..14c1895 100644 --- a/examples/custom_widget.rs +++ b/examples/custom_widget.rs @@ -13,16 +13,11 @@ use tui::{ Frame, Terminal, }; +#[derive(Default)] struct Label<'a> { text: &'a str, } -impl<'a> Default for Label<'a> { - fn default() -> Label<'a> { - Label { text: "" } - } -} - impl<'a> Widget for Label<'a> { fn render(self, area: Rect, buf: &mut Buffer) { buf.set_string(area.left(), area.top(), self.text, Style::default()); @@ -68,9 +63,8 @@ fn run_app(terminal: &mut Terminal) -> io::Result<()> { terminal.draw(ui)?; if let Event::Key(key) = event::read()? { - match key.code { - KeyCode::Char('q') => return Ok(()), - _ => {} + if let KeyCode::Char('q') = key.code { + return Ok(()); } } } diff --git a/examples/demo/termion.rs b/examples/demo/termion.rs index 4d87c94..b0b4215 100644 --- a/examples/demo/termion.rs +++ b/examples/demo/termion.rs @@ -62,12 +62,10 @@ fn events(tick_rate: Duration) -> mpsc::Receiver { let keys_tx = tx.clone(); thread::spawn(move || { let stdin = io::stdin(); - for evt in stdin.keys() { - if let Ok(key) = evt { - if let Err(err) = keys_tx.send(Event::Input(key)) { - eprintln!("{}", err); - return; - } + for key in stdin.keys().flatten() { + if let Err(err) = keys_tx.send(Event::Input(key)) { + eprintln!("{}", err); + return; } } }); diff --git a/examples/gauge.rs b/examples/gauge.rs index de0d969..13e0ca1 100644 --- a/examples/gauge.rs +++ b/examples/gauge.rs @@ -90,16 +90,15 @@ fn run_app( ) -> io::Result<()> { let mut last_tick = Instant::now(); loop { - terminal.draw(|f| ui(f, &mut app))?; + terminal.draw(|f| ui(f, &app))?; let timeout = tick_rate .checked_sub(last_tick.elapsed()) .unwrap_or_else(|| Duration::from_secs(0)); if crossterm::event::poll(timeout)? { if let Event::Key(key) = event::read()? { - match key.code { - KeyCode::Char('q') => return Ok(()), - _ => {} + if let KeyCode::Char('q') = key.code { + return Ok(()); } } } diff --git a/examples/layout.rs b/examples/layout.rs index baf3626..4bdc462 100644 --- a/examples/layout.rs +++ b/examples/layout.rs @@ -43,9 +43,8 @@ fn run_app(terminal: &mut Terminal) -> io::Result<()> { terminal.draw(|f| ui(f))?; if let Event::Key(key) = event::read()? { - match key.code { - KeyCode::Char('q') => return Ok(()), - _ => {} + if let KeyCode::Char('q') = key.code { + return Ok(()); } } } diff --git a/examples/paragraph.rs b/examples/paragraph.rs index 407d176..1ec8b86 100644 --- a/examples/paragraph.rs +++ b/examples/paragraph.rs @@ -68,16 +68,15 @@ fn run_app( ) -> io::Result<()> { let mut last_tick = Instant::now(); loop { - terminal.draw(|f| ui(f, &mut app))?; + terminal.draw(|f| ui(f, &app))?; let timeout = tick_rate .checked_sub(last_tick.elapsed()) .unwrap_or_else(|| Duration::from_secs(0)); if crossterm::event::poll(timeout)? { if let Event::Key(key) = event::read()? { - match key.code { - KeyCode::Char('q') => return Ok(()), - _ => {} + if let KeyCode::Char('q') = key.code { + return Ok(()); } } } diff --git a/examples/popup.rs b/examples/popup.rs index 0ec9d95..3ae8cc4 100644 --- a/examples/popup.rs +++ b/examples/popup.rs @@ -54,7 +54,7 @@ fn main() -> Result<(), Box> { fn run_app(terminal: &mut Terminal, mut app: App) -> io::Result<()> { loop { - terminal.draw(|f| ui(f, &mut app))?; + terminal.draw(|f| ui(f, &app))?; if let Event::Key(key) = event::read()? { match key.code { diff --git a/examples/sparkline.rs b/examples/sparkline.rs index 999f9e0..6ceaff6 100644 --- a/examples/sparkline.rs +++ b/examples/sparkline.rs @@ -112,16 +112,15 @@ fn run_app( ) -> io::Result<()> { let mut last_tick = Instant::now(); loop { - terminal.draw(|f| ui(f, &mut app))?; + terminal.draw(|f| ui(f, &app))?; let timeout = tick_rate .checked_sub(last_tick.elapsed()) .unwrap_or_else(|| Duration::from_secs(0)); if crossterm::event::poll(timeout)? { if let Event::Key(key) = event::read()? { - match key.code { - KeyCode::Char('q') => return Ok(()), - _ => {} + if let KeyCode::Char('q') = key.code { + return Ok(()); } } } diff --git a/examples/tabs.rs b/examples/tabs.rs index 4ca0f9a..8cb9078 100644 --- a/examples/tabs.rs +++ b/examples/tabs.rs @@ -69,7 +69,7 @@ fn main() -> Result<(), Box> { fn run_app(terminal: &mut Terminal, mut app: App) -> io::Result<()> { loop { - terminal.draw(|f| ui(f, &mut app))?; + terminal.draw(|f| ui(f, &app))?; if let Event::Key(key) = event::read()? { match key.code { diff --git a/examples/user_input.rs b/examples/user_input.rs index 786d0d2..330d502 100644 --- a/examples/user_input.rs +++ b/examples/user_input.rs @@ -80,7 +80,7 @@ fn main() -> Result<(), Box> { fn run_app(terminal: &mut Terminal, mut app: App) -> io::Result<()> { loop { - terminal.draw(|f| ui(f, &mut app))?; + terminal.draw(|f| ui(f, &app))?; if let Event::Key(key) = event::read()? { match app.input_mode { diff --git a/src/buffer.rs b/src/buffer.rs index 14699d8..b856376 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -105,7 +105,7 @@ impl Default for Cell { /// buf.get_mut(5, 0).set_char('x'); /// assert_eq!(buf.get(5, 0).symbol, "x"); /// ``` -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Default)] pub struct Buffer { /// The area represented by this buffer pub area: Rect, @@ -114,15 +114,6 @@ pub struct Buffer { pub content: Vec, } -impl Default for Buffer { - fn default() -> Buffer { - Buffer { - area: Default::default(), - content: Vec::new(), - } - } -} - impl Buffer { /// Returns a Buffer with all cells set to the default one pub fn empty(area: Rect) -> Buffer { diff --git a/src/layout.rs b/src/layout.rs index b1df157..624c22b 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -363,7 +363,7 @@ impl Element { /// A simple rectangle used in the computation of the layout and to give widgets an hint about the /// area they are supposed to render to. -#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, Default)] pub struct Rect { pub x: u16, pub y: u16, @@ -371,17 +371,6 @@ pub struct Rect { pub height: u16, } -impl Default for Rect { - fn default() -> Rect { - Rect { - x: 0, - y: 0, - width: 0, - height: 0, - } - } -} - impl Rect { /// Creates a new rect, with width and height limited to keep the area under max u16. /// If clipped, aspect ratio will be preserved. diff --git a/src/text.rs b/src/text.rs index 70a71d0..7d99343 100644 --- a/src/text.rs +++ b/src/text.rs @@ -194,15 +194,9 @@ impl<'a> From<&'a str> for Span<'a> { } /// A string composed of clusters of graphemes, each with their own style. -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Default)] pub struct Spans<'a>(pub Vec>); -impl<'a> Default for Spans<'a> { - fn default() -> Spans<'a> { - Spans(Vec::new()) - } -} - impl<'a> Spans<'a> { /// Returns the width of the underlying string. /// @@ -279,17 +273,11 @@ impl<'a> From> for String { /// text.extend(Text::styled("Some more lines\nnow with more style!", style)); /// assert_eq!(6, text.height()); /// ``` -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Default)] pub struct Text<'a> { pub lines: Vec>, } -impl<'a> Default for Text<'a> { - fn default() -> Text<'a> { - Text { lines: Vec::new() } - } -} - impl<'a> Text<'a> { /// Create some text (potentially multiple lines) with no style. /// diff --git a/src/widgets/canvas/world.rs b/src/widgets/canvas/world.rs index cb5ce6d..b15b7c3 100644 --- a/src/widgets/canvas/world.rs +++ b/src/widgets/canvas/world.rs @@ -6122,6 +6122,7 @@ pub static WORLD_LOW_RESOLUTION: [(f64, f64); 1166] = [ (120.43, 16.43), (121.72, 18.40), (125.34, 9.79), + #[allow(clippy::approx_constant)] (125.56, 6.28), (122.38, 7.00), (125.10, 9.38), diff --git a/src/widgets/chart.rs b/src/widgets/chart.rs index e74c19f..ee879dc 100644 --- a/src/widgets/chart.rs +++ b/src/widgets/chart.rs @@ -141,7 +141,7 @@ impl<'a> Dataset<'a> { /// A container that holds all the infos about where to display each elements of the chart (axis, /// labels, legend, ...). -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Default)] struct ChartLayout { /// Location of the title of the x axis title_x: Option<(u16, u16)>, @@ -161,21 +161,6 @@ struct ChartLayout { graph_area: Rect, } -impl Default for ChartLayout { - fn default() -> ChartLayout { - ChartLayout { - title_x: None, - title_y: None, - label_x: None, - label_y: None, - axis_x: None, - axis_y: None, - legend_area: None, - graph_area: Rect::default(), - } - } -} - /// A widget to plot one or more dataset in a cartesian coordinate system /// /// # Examples diff --git a/src/widgets/list.rs b/src/widgets/list.rs index 5f6a559..b0279d7 100644 --- a/src/widgets/list.rs +++ b/src/widgets/list.rs @@ -7,21 +7,12 @@ use crate::{ }; use unicode_width::UnicodeWidthStr; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Default)] pub struct ListState { offset: usize, selected: Option, } -impl Default for ListState { - fn default() -> ListState { - ListState { - offset: 0, - selected: None, - } - } -} - impl ListState { pub fn selected(&self) -> Option { self.selected diff --git a/src/widgets/reflow.rs b/src/widgets/reflow.rs index 94ff733..16760fa 100644 --- a/src/widgets/reflow.rs +++ b/src/widgets/reflow.rs @@ -403,8 +403,8 @@ mod test { let text = "コンピュータ上で文字を扱う場合、典型的には文字による通信を行う場合にその両端点\ では、"; let (word_wrapper, word_wrapper_width) = - run_composer(Composer::WordWrapper { trim: true }, &text, width); - let (line_truncator, _) = run_composer(Composer::LineTruncator, &text, width); + run_composer(Composer::WordWrapper { trim: true }, text, width); + let (line_truncator, _) = run_composer(Composer::LineTruncator, text, width); assert_eq!(line_truncator, vec!["コンピュータ上で文字"]); let wrapped = vec![ "コンピュータ上で文字", diff --git a/src/widgets/table.rs b/src/widgets/table.rs index fa75e77..f696114 100644 --- a/src/widgets/table.rs +++ b/src/widgets/table.rs @@ -335,21 +335,12 @@ impl<'a> Table<'a> { } } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Default)] pub struct TableState { offset: usize, selected: Option, } -impl Default for TableState { - fn default() -> TableState { - TableState { - offset: 0, - selected: None, - } - } -} - impl TableState { pub fn selected(&self) -> Option { self.selected