From c97d712c5266ec2c4095c90ef3553bc9741a8b1a Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 14 Dec 2021 23:47:44 -0500 Subject: let's actually stop being so aggressive here --- src/cell.rs | 8 ++++---- src/grid.rs | 34 ++++++++++++++-------------------- src/lib.rs | 4 +--- src/row.rs | 14 +++++++------- src/screen.rs | 54 ++++++++++++++++++++++++++++-------------------------- src/term.rs | 8 ++++---- 6 files changed, 58 insertions(+), 64 deletions(-) (limited to 'src') diff --git a/src/cell.rs b/src/cell.rs index 3ebf8fc..aa7532e 100644 --- a/src/cell.rs +++ b/src/cell.rs @@ -20,8 +20,8 @@ impl PartialEq for Cell { } let len = self.len(); // self.len() always returns a valid value - self.contents.get(..len).unwrap_or_else(|| unreachable!()) - == other.contents.get(..len).unwrap_or_else(|| unreachable!()) + self.contents.get(..len).unwrap() + == other.contents.get(..len).unwrap() } } @@ -48,13 +48,13 @@ impl Cell { } if len == 0 { // 0 is always less than 6 - *self.contents.get_mut(0).unwrap_or_else(|| unreachable!()) = ' '; + *self.contents.get_mut(0).unwrap() = ' '; self.len += 1; } let len = self.len(); // we already checked that len < CODEPOINTS_IN_CELL - *self.contents.get_mut(len).unwrap_or_else(|| unreachable!()) = c; + *self.contents.get_mut(len).unwrap() = c; self.len += 1; } diff --git a/src/grid.rs b/src/grid.rs index f8aad21..a6a41df 100644 --- a/src/grid.rs +++ b/src/grid.rs @@ -153,7 +153,7 @@ impl Grid { pub fn current_row_mut(&mut self) -> &mut crate::row::Row { self.drawing_row_mut(self.pos.row) // we assume self.pos.row is always valid - .unwrap_or_else(|| unreachable!()) + .unwrap() } pub fn visible_cell(&self, pos: Pos) -> Option<&crate::cell::Cell> { @@ -212,7 +212,7 @@ impl Grid { for (i, row) in self.visible_rows().enumerate() { // we limit the number of cols to a u16 (see Size), so // visible_rows() can never return more rows than will fit - let i = i.try_into().unwrap_or_else(|_| unreachable!()); + let i = i.try_into().unwrap(); let (new_pos, new_attrs) = row.write_contents_formatted( contents, 0, @@ -250,7 +250,7 @@ impl Grid { { // we limit the number of cols to a u16 (see Size), so // visible_rows() can never return more rows than will fit - let i = i.try_into().unwrap_or_else(|_| unreachable!()); + let i = i.try_into().unwrap(); let (new_pos, new_attrs) = row.write_contents_diff( contents, prev_row, @@ -298,7 +298,7 @@ impl Grid { .drawing_cell(pos) // we assume self.pos.row is always valid, and self.size.cols // - 1 is always a valid column - .unwrap_or_else(|| unreachable!()) + .unwrap() .is_wide_continuation() { pos.col = self.size.cols - 2; @@ -310,7 +310,7 @@ impl Grid { // self.size.cols - 1 is a wide continuation character, which // means that the first half of the wide character must be // before it - self.drawing_cell(pos).unwrap_or_else(|| unreachable!()); + self.drawing_cell(pos).unwrap(); if cell.has_contents() { if let Some(prev_pos) = prev_pos { crate::term::MoveFromTo::new(prev_pos, pos) @@ -339,7 +339,7 @@ impl Grid { // i is always less than self.pos.row, which we assume // to be always valid, so it must also be valid. // self.size.cols - 1 is always a valid col. - .unwrap_or_else(|| unreachable!()) + .unwrap() .is_wide_continuation() { pos.col = self.size.cols - 2; @@ -354,7 +354,7 @@ impl Grid { // - 1 is a wide continuation character, meaning that // the first half of the wide character must be before // it - .unwrap_or_else(|| unreachable!()); + .unwrap(); if cell.has_contents() { if let Some(prev_pos) = prev_pos { if prev_pos.row != i @@ -417,7 +417,7 @@ impl Grid { .drawing_cell(pos) // we assume self.pos.row is always valid, and // self.size.cols - 1 is always a valid column - .unwrap_or_else(|| unreachable!()); + .unwrap(); end_cell .attrs() .write_escape_code_diff(contents, &prev_attrs); @@ -492,7 +492,7 @@ impl Grid { // we assume self.pos.row is always valid, and we know we are // not off the end of a row because we just checked pos.col < // size.cols - .unwrap_or_else(|| unreachable!()) + .unwrap() .is_wide_continuation(); let row = self.current_row_mut(); for _ in 0..count { @@ -530,11 +530,8 @@ impl Grid { for _ in 0..count { self.rows.remove(usize::from(self.scroll_bottom)); self.rows.insert(usize::from(self.pos.row), self.new_row()); - self.rows - .get_mut(usize::from(self.scroll_bottom)) - // self.scroll_bottom is maintained to always be a valid row - .unwrap_or_else(|| unreachable!()) - .wrap(false); + // self.scroll_bottom is maintained to always be a valid row + self.rows[usize::from(self.scroll_bottom)].wrap(false); } } @@ -569,11 +566,8 @@ impl Grid { self.rows.remove(usize::from(self.scroll_bottom)); self.rows .insert(usize::from(self.scroll_top), self.new_row()); - self.rows - .get_mut(usize::from(self.scroll_bottom)) - // self.scroll_bottom is maintained to always be a valid row - .unwrap_or_else(|| unreachable!()) - .wrap(false); + // self.scroll_bottom is maintained to always be a valid row + self.rows[usize::from(self.scroll_bottom)].wrap(false); } } @@ -681,7 +675,7 @@ impl Grid { // we assume self.pos.row is always valid, and so prev_pos.row // must be valid because it is always less than or equal to // self.pos.row - .unwrap_or_else(|| unreachable!()) + .unwrap() .wrap(wrap && prev_pos.row + 1 == new_pos.row); } } diff --git a/src/lib.rs b/src/lib.rs index e796153..cd37bb2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,10 +36,8 @@ #![warn(clippy::cargo)] #![warn(clippy::pedantic)] #![warn(clippy::nursery)] -#![warn(clippy::unwrap_used)] -#![warn(clippy::expect_used)] -#![warn(clippy::indexing_slicing)] #![warn(clippy::as_conversions)] +#![warn(clippy::get_unwrap)] #![allow(clippy::cognitive_complexity)] #![allow(clippy::missing_const_for_fn)] #![allow(clippy::similar_names)] diff --git a/src/row.rs b/src/row.rs index 1875286..6ad5ec5 100644 --- a/src/row.rs +++ b/src/row.rs @@ -19,7 +19,7 @@ impl Row { .len() .try_into() // we limit the number of cols to a u16 (see Size) - .unwrap_or_else(|_| unreachable!()) + .unwrap() } pub fn clear(&mut self, attrs: crate::attrs::Attrs) { @@ -53,9 +53,9 @@ impl Row { } pub fn erase(&mut self, i: u16, attrs: crate::attrs::Attrs) { - let wide = self.cells.get_mut(usize::from(i)).unwrap().is_wide(); + let wide = self.cells[usize::from(i)].is_wide(); self.clear_wide(i); - self.cells.get_mut(usize::from(i)).unwrap().clear(attrs); + self.cells[usize::from(i)].clear(attrs); if i == self.cols() - if wide { 2 } else { 1 } { self.wrapped = false; } @@ -64,7 +64,7 @@ impl Row { pub fn truncate(&mut self, len: u16) { self.cells.truncate(usize::from(len)); self.wrapped = false; - let last_cell = self.cells.get_mut(usize::from(len) - 1).unwrap(); + let last_cell = &mut self.cells[usize::from(len) - 1]; if last_cell.is_wide() { last_cell.clear(*last_cell.attrs()); } @@ -118,7 +118,7 @@ impl Row { prev_was_wide = cell.is_wide(); // we limit the number of cols to a u16 (see Size) - let col: u16 = col.try_into().unwrap_or_else(|_| unreachable!()); + let col: u16 = col.try_into().unwrap(); if cell.has_contents() { for _ in 0..(col - prev_col) { contents.push(' '); @@ -186,7 +186,7 @@ impl Row { prev_was_wide = cell.is_wide(); // we limit the number of cols to a u16 (see Size) - let col: u16 = col.try_into().unwrap_or_else(|_| unreachable!()); + let col: u16 = col.try_into().unwrap(); let pos = crate::grid::Pos { row, col }; if let Some((prev_col, attrs)) = erase { @@ -345,7 +345,7 @@ impl Row { prev_was_wide = cell.is_wide(); // we limit the number of cols to a u16 (see Size) - let col: u16 = col.try_into().unwrap_or_else(|_| unreachable!()); + let col: u16 = col.try_into().unwrap(); let pos = crate::grid::Pos { row, col }; if let Some((prev_col, attrs)) = erase { diff --git a/src/screen.rs b/src/screen.rs index d083d4e..13e71eb 100644 --- a/src/screen.rs +++ b/src/screen.rs @@ -285,6 +285,8 @@ impl Screen { /// You are responsible for positioning the cursor before printing each /// row, and the final cursor position after displaying each row is /// unspecified. + // the unwraps in this method shouldn't be reachable + #[allow(clippy::missing_panics_doc)] pub fn rows_formatted( &self, start: u16, @@ -294,7 +296,7 @@ impl Screen { self.grid().visible_rows().enumerate().map(move |(i, row)| { // number of rows in a grid is stored in a u16 (see Size), so // visible_rows can never return enough rows to overflow here - let i = i.try_into().unwrap_or_else(|_| unreachable!()); + let i = i.try_into().unwrap(); let mut contents = vec![]; row.write_contents_formatted( &mut contents, @@ -350,6 +352,8 @@ impl Screen { /// You are responsible for positioning the cursor before printing each /// row, and the final cursor position after displaying each row is /// unspecified. + // the unwraps in this method shouldn't be reachable + #[allow(clippy::missing_panics_doc)] pub fn rows_diff<'a>( &'a self, prev: &'a Self, @@ -363,7 +367,7 @@ impl Screen { .map(move |(i, (row, prev_row))| { // number of rows in a grid is stored in a u16 (see Size), so // visible_rows can never return enough rows to overflow here - let i = i.try_into().unwrap_or_else(|_| unreachable!()); + let i = i.try_into().unwrap(); let mut contents = vec![]; row.write_contents_diff( &mut contents, @@ -819,7 +823,7 @@ impl Screen { .unwrap_or(1) .try_into() // width() can only return 0, 1, or 2 - .unwrap_or_else(|_| unreachable!()); + .unwrap(); // it doesn't make any sense to wrap if the last column in a row // didn't already have contents. don't try to handle the case where a @@ -840,7 +844,7 @@ impl Screen { // pos.row is valid, since it comes directly from // self.grid().pos() which we assume to always have a valid // row value. size.cols - 1 is also always a valid column. - .unwrap_or_else(|| unreachable!()); + .unwrap(); if last_cell.has_contents() || last_cell.is_wide_continuation() { wrap = true; } @@ -860,7 +864,7 @@ impl Screen { // self.grid().pos() which we assume to always have a // valid row value. pos.col - 1 is valid because we just // checked for pos.col > 0. - .unwrap_or_else(|| unreachable!()); + .unwrap(); if prev_cell.is_wide_continuation() { prev_cell = self .grid_mut() @@ -874,7 +878,7 @@ impl Screen { // because the cell at pos.col - 1 is a wide // continuation character, which means there must be // the first half of the wide character before it. - .unwrap_or_else(|| unreachable!()); + .unwrap(); } prev_cell.append(c); } else if pos.row > 0 { @@ -885,7 +889,7 @@ impl Screen { // self.grid().pos() which we assume to always have a // valid row value. pos.row - 1 is valid because we just // checked for pos.row > 0. - .unwrap_or_else(|| unreachable!()); + .unwrap(); if prev_row.wrapped() { let mut prev_cell = self .grid_mut() @@ -898,7 +902,7 @@ impl Screen { // valid row value. pos.row - 1 is valid because we // just checked for pos.row > 0. col of size.cols - 1 // is always valid. - .unwrap_or_else(|| unreachable!()); + .unwrap(); if prev_cell.is_wide_continuation() { prev_cell = self .grid_mut() @@ -914,7 +918,7 @@ impl Screen { // size.cols - 1 is a wide continuation character, // so it must have the first half of the wide // character before it. - .unwrap_or_else(|| unreachable!()); + .unwrap(); } prev_cell.append(c); } @@ -927,7 +931,7 @@ impl Screen { // always have a valid row value. pos.col is valid because we // called col_wrap() immediately before this, which ensures // that self.grid().pos().col has a valid value. - .unwrap_or_else(|| unreachable!()) + .unwrap() .is_wide_continuation() { let prev_cell = self @@ -943,7 +947,7 @@ impl Screen { // pos.col - 1 is valid because the cell at pos.col is a // wide continuation character, so it must have the first // half of the wide character before it. - .unwrap_or_else(|| unreachable!()); + .unwrap(); prev_cell.clear(attrs); } @@ -954,7 +958,7 @@ impl Screen { // always have a valid row value. pos.col is valid because we // called col_wrap() immediately before this, which ensures // that self.grid().pos().col has a valid value. - .unwrap_or_else(|| unreachable!()) + .unwrap() .is_wide() { let next_cell = self @@ -970,7 +974,7 @@ impl Screen { // pos.col + 1 is valid because the cell at pos.col is a // wide character, so it must have the second half of the // wide character after it. - .unwrap_or_else(|| unreachable!()); + .unwrap(); next_cell.set(' ', attrs); } @@ -981,7 +985,7 @@ impl Screen { // always have a valid row value. pos.col is valid because we // called col_wrap() immediately before this, which ensures // that self.grid().pos().col has a valid value. - .unwrap_or_else(|| unreachable!()); + .unwrap(); cell.set(c, attrs); self.grid_mut().col_inc(1); if width > 1 { @@ -996,7 +1000,7 @@ impl Screen { // even though we just called col_inc, because this branch // only happens if width > 1, and col_wrap takes width // into account. - .unwrap_or_else(|| unreachable!()) + .unwrap() .is_wide() { let next_next_pos = crate::grid::Pos { @@ -1016,13 +1020,13 @@ impl Screen { // pos.col + 1 is valid because the cell at pos.col is // wide, and so it must have the second half of the // wide character after it. - .unwrap_or_else(|| unreachable!()); + .unwrap(); next_next_cell.clear(attrs); if next_next_pos.col == size.cols - 1 { self.grid_mut() .drawing_row_mut(pos.row) // we assume self.grid().pos().row is always valid - .unwrap_or_else(|| unreachable!()) + .unwrap() .wrap(false); } } @@ -1036,7 +1040,7 @@ impl Screen { // even though we just called col_inc, because this branch // only happens if width > 1, and col_wrap takes width // into account. - .unwrap_or_else(|| unreachable!()); + .unwrap(); next_cell.clear(crate::attrs::Attrs::default()); next_cell.set_wide_continuation(true); self.grid_mut().col_inc(1); @@ -1279,7 +1283,7 @@ impl Screen { "{}", // we just checked that ns.len() == 1, so 0 // must be valid - ns.get(0).unwrap_or_else(|| unreachable!()) + ns[0] ) } else { format!("{:?}", ns) @@ -1338,7 +1342,7 @@ impl Screen { "{}", // we just checked that ns.len() == 1, so 0 // must be valid - ns.get(0).unwrap_or_else(|| unreachable!()) + ns[0] ) } else { format!("{:?}", ns) @@ -1435,8 +1439,7 @@ impl Screen { "{}", // we just checked that ns.len() == 1, so // 0 must be valid - ns.get(0) - .unwrap_or_else(|| unreachable!()) + ns[0] ) } else { format!("{:?}", ns) @@ -1482,8 +1485,7 @@ impl Screen { "{}", // we just checked that ns.len() == 1, so // 0 must be valid - ns.get(0) - .unwrap_or_else(|| unreachable!()) + ns[0] ) } else { format!("{:?}", ns) @@ -1511,7 +1513,7 @@ impl Screen { "{}", // we just checked that ns.len() == 1, so 0 // must be valid - ns.get(0).unwrap_or_else(|| unreachable!()) + ns[0] ) } else { format!("{:?}", ns) @@ -1749,7 +1751,7 @@ fn u16_to_u8(i: u16) -> Option { None } else { // safe because we just ensured that the value fits in a u8 - Some(i.try_into().unwrap_or_else(|_| unreachable!())) + Some(i.try_into().unwrap()) } } diff --git a/src/term.rs b/src/term.rs index b6a0576..cd97365 100644 --- a/src/term.rs +++ b/src/term.rs @@ -88,11 +88,11 @@ impl BufWrite for MoveTo { buf.extend_from_slice(b"\x1b["); itoa::write(&mut *buf, self.row + 1) // write to a vec can never fail - .unwrap_or_else(|_| unreachable!()); + .unwrap(); buf.push(b';'); itoa::write(&mut *buf, self.col + 1) // write to a vec can never fail - .unwrap_or_else(|_| unreachable!()); + .unwrap(); buf.push(b'H'); } } @@ -293,7 +293,7 @@ impl BufWrite for MoveRight { n => { buf.extend_from_slice(b"\x1b["); // write to a vec can never fail - itoa::write(&mut *buf, n).unwrap_or_else(|_| unreachable!()); + itoa::write(&mut *buf, n).unwrap(); buf.push(b'C'); } } @@ -326,7 +326,7 @@ impl BufWrite for EraseChar { n => { buf.extend_from_slice(b"\x1b["); // write to a vec can never fail - itoa::write(&mut *buf, n).unwrap_or_else(|_| unreachable!()); + itoa::write(&mut *buf, n).unwrap(); buf.push(b'X'); } } -- cgit v1.2.3-54-g00ecf