From 297ff6b80b7625621f597f385c9e20b49a21d138 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 10 Mar 2021 16:09:46 -0500 Subject: [PATCH] Add more clippy warnings, and solve a bunch of them --- src/document.rs | 27 ++++++++++++++++++--------- src/editor.rs | 32 +++++++++++++++++++------------- src/main.rs | 10 +++++++++- src/row.rs | 2 ++ 4 files changed, 48 insertions(+), 23 deletions(-) diff --git a/src/document.rs b/src/document.rs index 52337dc..4614895 100644 --- a/src/document.rs +++ b/src/document.rs @@ -40,7 +40,7 @@ impl Document { } pub fn insert(&mut self, at: &Position, c: char) { - if at.y > self.len() { + if at.y > self.rows.len() { return; } @@ -52,18 +52,20 @@ impl Document { return; } - if at.y == self.len() { + if at.y == self.rows.len() { let mut row = Row::default(); row.insert(0, c); self.rows.push(row); } else { - let row = self.rows.get_mut(at.y).unwrap(); + #[allow(clippy::indexing_slicing)] + let row = &mut self.rows[at.y]; row.insert(at.x, c); } } + #[allow(clippy::integer_arithmetic, clippy::indexing_slicing)] pub fn delete(&mut self, at: &Position) { - let len = self.len(); + let len = self.rows.len(); if at.y >= len { return } @@ -71,12 +73,12 @@ impl Document { // File has been modified self.dirty = true; - if at.x == self.rows.get_mut(at.y).unwrap().len() && at.y < len - 1 { + if at.x == self.rows[at.y].len() && at.y + 1 < len { let next_row = self.rows.remove(at.y + 1); - let row = self.rows.get_mut(at.y).unwrap(); + let row = &mut self.rows[at.y]; row.append(&next_row); } else { - let row = self.rows.get_mut(at.y).unwrap(); + let row = &mut self.rows[at.y]; row.delete(at.x); } } @@ -102,12 +104,19 @@ impl Document { } fn insert_newline(&mut self, at: &Position) { - if at.y == self.len() { + if at.y > self.rows.len() { + return; + } + + if at.y == self.rows.len() { self.rows.push(Row::default()); return; } - let new_row = self.rows.get_mut(at.y).unwrap().split(at.x); + #[allow(clippy::indexing_slicing)] + let new_row = self.rows[at.y].split(at.x); + + #[allow(clippy::integer_arithmetic)] self.rows.insert(at.y + 1, new_row); } } \ No newline at end of file diff --git a/src/editor.rs b/src/editor.rs index f2f3bac..8c1baf8 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -64,11 +64,11 @@ impl Editor { pub fn default() -> Self { let args: Vec = env::args().collect(); let mut initial_status = String::from("HELP: Ctrl-S = save | Ctrl-Q = quit"); - let document = if args.len() > 1 { + let document = if let Some(file_name) = args.get(1) { let file_name = &args[1]; let doc = Document::open(&file_name); - if doc.is_ok() { - doc.unwrap() + if let Ok(doc) = doc { + doc } else { initial_status = format!("ERR: Could not open file: {}", file_name); Document::default() @@ -134,9 +134,11 @@ impl Editor { self.cursor_position.y.saturating_add(1), self.document.len() ); + + #[allow(clippy::integer_arithmetic)] let len = status.len() + line_indicator.len(); if width > len { - status.push_str(&" ".repeat(width - len)); + status.push_str(&" ".repeat(width.saturating_sub(len))); } status = format!("{}{}", status, line_indicator); status.truncate(width); @@ -235,11 +237,7 @@ impl Editor { self.refresh_screen()?; match Terminal::read_key()? { - Key::Backspace => { - if !result.is_empty() { - result.truncate(result.len() - 1); - } - } + Key::Backspace => result.truncate(result.len().saturating_sub(1)), Key::Char('\n') => break, Key::Char(c) => { if !c.is_control() { @@ -322,14 +320,14 @@ impl Editor { }, Key::PageUp => { y = if y > terminal_height { - y - terminal_height + y.saturating_sub(terminal_height) } else { 0 } }, Key::PageDown => { y = if y.saturating_add(terminal_height) < height { - y + terminal_height as usize + y.saturating_add(terminal_height) } else { height } @@ -356,28 +354,36 @@ impl Editor { let mut welcome_message = format!("Hecto editor -- version {}", VERSION); let width = self.terminal.size().width as usize; let len = welcome_message.len(); + + #[allow(clippy::integer_arithmetic, clippy::integer_division)] let padding = width.saturating_sub(len) / 2; let spaces = " ".repeat(padding.saturating_sub(1)); + welcome_message = format!("~{}{}", spaces, welcome_message); welcome_message.truncate(width); + println!("{}\r", welcome_message); } pub fn draw_row(&self, row: &Row) { let width = self.terminal.size().width as usize; let start = self.offset.x; - let end = self.offset.x + width; + let end = self.offset.x.saturating_add(width); let row = row.render(start, end); println!("{}\r", row); } + #[allow(clippy::integer_arithmetic, clippy::integer_division)] fn draw_rows(&self) { let height = self.terminal.size().height; for terminal_row in 0..height { Terminal::clear_current_line(); - if let Some(row) = self.document.row(terminal_row as usize + self.offset.y) { + if let Some(row) = self + .document + .row(self.offset.y.saturating_add(terminal_row as usize)) + { self.draw_row(row); } else if self.document.is_empty() && terminal_row == height / 3 { self.draw_welcome_message(); diff --git a/src/main.rs b/src/main.rs index 693fc5d..722bf4d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,12 @@ -#![warn(clippy::all, clippy::pedantic)] +#![warn(clippy::all, clippy::pedantic, clippy::restriction)] +#![allow( + clippy::missing_docs_in_private_items, + clippy::implicit_return, + clippy::shadow_reuse, + clippy::print_stdout, + clippy::wildcard_enum_match_arm, + clippy::else_if_without_else +)] mod document; mod editor; mod row; diff --git a/src/row.rs b/src/row.rs index ec980db..8661821 100644 --- a/src/row.rs +++ b/src/row.rs @@ -26,6 +26,7 @@ impl Row { let mut result = String::new(); + #[allow(clippy::integer_arithmetic)] for grapheme in self.string[..] .graphemes(true) .skip(start) @@ -69,6 +70,7 @@ impl Row { self.update_len(); } + #[allow(clippy::integer_arithmetic)] pub fn delete(&mut self, at: usize) { if at >= self.len() { return