chore: Minor lints, making Clippy happier (#189)

- `Default::default` is hard to read
- a few `map` -> `map_or`
- simplified `match` -> `let-if`

Signed-off-by: Yuri Astrakhan <YuriAstrakhan@gmail.com>
This commit is contained in:
Yuri Astrakhan 2023-05-21 23:45:37 -04:00 committed by GitHub
parent ef8bc7c5a8
commit 3f9935bbcc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 57 additions and 81 deletions

View file

@ -7,9 +7,9 @@ use crate::{
use std::{error::Error, io};
use termwiz::{
caps::Capabilities,
cell::*,
color::*,
surface::*,
cell::{AttributeChange, Blink, Intensity, Underline},
color::{AnsiColor, ColorAttribute, SrgbaTuple},
surface::{Change, CursorVisibility, Position},
terminal::{buffered::BufferedTerminal, SystemTerminal, Terminal},
};

View file

@ -30,7 +30,7 @@ fn buffer_view(buffer: &Buffer) -> String {
if skip == 0 {
view.push_str(&c.symbol);
} else {
overwritten.push((x, &c.symbol))
overwritten.push((x, &c.symbol));
}
skip = std::cmp::max(skip, c.symbol.width()).saturating_sub(1);
}

View file

@ -121,7 +121,7 @@ pub struct Buffer {
impl Buffer {
/// Returns a Buffer with all cells set to the default one
pub fn empty(area: Rect) -> Buffer {
let cell: Cell = Default::default();
let cell = Cell::default();
Buffer::filled(area, &cell)
}
@ -257,8 +257,8 @@ impl Buffer {
self.content.len()
);
(
self.area.x + i as u16 % self.area.width,
self.area.y + i as u16 / self.area.width,
self.area.x + (i as u16) % self.area.width,
self.area.y + (i as u16) / self.area.width,
)
}
@ -385,7 +385,7 @@ impl Buffer {
if self.content.len() > length {
self.content.truncate(length);
} else {
self.content.resize(length, Default::default());
self.content.resize(length, Cell::default());
}
self.area = area;
}
@ -400,7 +400,7 @@ impl Buffer {
/// Merge an other buffer into this one
pub fn merge(&mut self, other: &Buffer) {
let area = self.area.union(other.area);
let cell: Cell = Default::default();
let cell = Cell::default();
self.content.resize(area.area() as usize, cell.clone());
// Move original content to the appropriate space
@ -558,7 +558,7 @@ impl Debug for Buffer {
if skip == 0 {
f.write_str(&c.symbol)?;
} else {
overwritten.push((x, &c.symbol))
overwritten.push((x, &c.symbol));
}
skip = std::cmp::max(skip, c.symbol.width()).saturating_sub(1);
let style = (c.fg, c.bg, c.modifier);

View file

@ -4,7 +4,7 @@ use std::collections::HashMap;
use std::rc::Rc;
use cassowary::strength::{MEDIUM, REQUIRED, WEAK};
use cassowary::WeightedRelation::*;
use cassowary::WeightedRelation::{EQ, GE, LE};
use cassowary::{Constraint as CassowaryConstraint, Expression, Solver, Variable};
#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq)]
@ -266,10 +266,7 @@ fn split(area: Rect, layout: &Layout) -> Rc<[Rect]> {
});
match *size {
Constraint::Min(v) => {
ccs.push(elements[i].width | EQ(WEAK) | f64::from(v));
}
Constraint::Max(v) => {
Constraint::Min(v) | Constraint::Max(v) => {
ccs.push(elements[i].width | EQ(WEAK) | f64::from(v));
}
_ => {}
@ -298,10 +295,7 @@ fn split(area: Rect, layout: &Layout) -> Rc<[Rect]> {
});
match *size {
Constraint::Min(v) => {
ccs.push(elements[i].height | EQ(WEAK) | f64::from(v));
}
Constraint::Max(v) => {
Constraint::Min(v) | Constraint::Max(v) => {
ccs.push(elements[i].height | EQ(WEAK) | f64::from(v));
}
_ => {}

View file

@ -451,7 +451,7 @@ where
T: Into<Line<'a>>,
{
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
let lines = iter.into_iter().map(|s| s.into());
let lines = iter.into_iter().map(Into::into);
self.lines.extend(lines);
}
}

View file

@ -63,9 +63,9 @@ impl<'a> Default for BarChart<'a> {
bar_width: 1,
bar_gap: 1,
bar_set: symbols::bar::NINE_LEVELS,
value_style: Default::default(),
label_style: Default::default(),
style: Default::default(),
value_style: Style::default(),
label_style: Style::default(),
style: Style::default(),
}
}
}

View file

@ -125,9 +125,9 @@ impl<'a> Default for Block<'a> {
title_alignment: Alignment::Left,
title_on_bottom: false,
borders: Borders::NONE,
border_style: Default::default(),
border_style: Style::default(),
border_type: BorderType::Plain,
style: Default::default(),
style: Style::default(),
padding: Padding::zero(),
}
}
@ -313,8 +313,8 @@ impl<'a> Widget for Block<'a> {
// Title
if let Some(title) = self.title {
let left_border_dx = self.borders.intersects(Borders::LEFT) as u16;
let right_border_dx = self.borders.intersects(Borders::RIGHT) as u16;
let left_border_dx = u16::from(self.borders.intersects(Borders::LEFT));
let right_border_dx = u16::from(self.borders.intersects(Borders::RIGHT));
let title_area_width = area
.width

View file

@ -171,7 +171,7 @@ pub trait DateStyler {
fn get_style(&self, date: Date) -> Style;
}
/// A simple DateStyler based on a [HashMap]
/// A simple `DateStyler` based on a [`HashMap`]
pub struct CalendarEventStore(pub HashMap<Date, Style>);
impl CalendarEventStore {

View file

@ -15,14 +15,8 @@ pub struct Line {
impl Shape for Line {
fn draw(&self, painter: &mut Painter) {
let (x1, y1) = match painter.get_point(self.x1, self.y1) {
Some(c) => c,
None => return,
};
let (x2, y2) = match painter.get_point(self.x2, self.y2) {
Some(c) => c,
None => return,
};
let Some((x1, y1)) = painter.get_point(self.x1, self.y1) else { return };
let Some((x2, y2)) = painter.get_point(self.x2, self.y2) else { return };
let (dx, x_range) = if x2 >= x1 {
(x2 - x1, x1..=x2)
} else {

View file

@ -313,7 +313,7 @@ impl<'a> Context<'a> {
/// Push the last layer if necessary
fn finish(&mut self) {
if self.dirty {
self.layer()
self.layer();
}
}
}
@ -457,10 +457,7 @@ where
let width = canvas_area.width as usize;
let painter = match self.painter {
Some(ref p) => p,
None => return,
};
let Some(ref painter) = self.painter else { return };
// Create a blank context that match the size of the canvas
let mut ctx = Context::new(

View file

@ -36,7 +36,7 @@ impl<'a> Default for Axis<'a> {
title: None,
bounds: [0.0, 0.0],
labels: None,
style: Default::default(),
style: Style::default(),
labels_alignment: Alignment::Left,
}
}
@ -234,7 +234,7 @@ impl<'a> Chart<'a> {
block: None,
x_axis: Axis::default(),
y_axis: Axis::default(),
style: Default::default(),
style: Style::default(),
datasets,
hidden_legend_constraints: (Constraint::Ratio(1, 4), Constraint::Ratio(1, 4)),
}
@ -366,7 +366,7 @@ impl<'a> Chart<'a> {
let width_left_of_y_axis = match self.x_axis.labels_alignment {
Alignment::Left => {
// The last character of the label should be below the Y-Axis when it exists, not on its left
let y_axis_offset = has_y_axis as u16;
let y_axis_offset = u16::from(has_y_axis);
first_label_width.saturating_sub(y_axis_offset)
}
Alignment::Center => first_label_width / 2,
@ -385,10 +385,7 @@ impl<'a> Chart<'a> {
chart_area: Rect,
graph_area: Rect,
) {
let y = match layout.label_x {
Some(y) => y,
None => return,
};
let Some(y) = layout.label_x else { return };
let labels = self.x_axis.labels.as_ref().unwrap();
let labels_len = labels.len() as u16;
if labels_len < 2 {
@ -470,10 +467,7 @@ impl<'a> Chart<'a> {
chart_area: Rect,
graph_area: Rect,
) {
let x = match layout.label_y {
Some(x) => x,
None => return,
};
let Some(x) = layout.label_y else { return };
let labels = self.y_axis.labels.as_ref().unwrap();
let labels_len = labels.len() as u16;
for (i, label) in labels.iter().enumerate() {
@ -563,7 +557,7 @@ impl<'a> Widget for Chart<'a> {
x2: data[1].0,
y2: data[1].1,
color: dataset.style.fg.unwrap_or(Color::Reset),
})
});
}
}
})

View file

@ -235,16 +235,13 @@ impl<'a> StatefulWidget for List<'a> {
.skip(state.offset)
.take(end - start)
{
let (x, y) = match self.start_corner {
Corner::BottomLeft => {
current_height += item.height() as u16;
(list_area.left(), list_area.bottom() - current_height)
}
_ => {
let pos = (list_area.left(), list_area.top() + current_height);
current_height += item.height() as u16;
pos
}
let (x, y) = if self.start_corner == Corner::BottomLeft {
current_height += item.height() as u16;
(list_area.left(), list_area.bottom() - current_height)
} else {
let pos = (list_area.left(), list_area.top() + current_height);
current_height += item.height() as u16;
pos
};
let area = Rect {
x,
@ -255,7 +252,7 @@ impl<'a> StatefulWidget for List<'a> {
let item_style = self.style.patch(item.style);
buf.set_style(area, item_style);
let is_selected = state.selected.map(|s| s == i).unwrap_or(false);
let is_selected = state.selected.map_or(false, |s| s == i);
for (j, line) in item.content.lines.iter().enumerate() {
// if the item is selected, we need to display the highlight symbol:
// - either for the first line of the item only,

View file

@ -98,7 +98,7 @@ impl<'a> Paragraph<'a> {
{
Paragraph {
block: None,
style: Default::default(),
style: Style::default(),
wrap: None,
text: text.into(),
scroll: (0, 0),

View file

@ -87,10 +87,10 @@ impl<'a, 'b> LineComposer<'a> for WordWrapper<'a, 'b> {
if current_line_width > self.max_line_width {
// If there was no word break in the text, wrap at the end of the line.
let (truncate_at, truncated_width) = if symbols_to_last_word_end != 0 {
(symbols_to_last_word_end, width_to_last_word_end)
} else {
let (truncate_at, truncated_width) = if symbols_to_last_word_end == 0 {
(self.current_line.len() - 1, self.max_line_width)
} else {
(symbols_to_last_word_end, width_to_last_word_end)
};
// Push the remainder to the next line but strip leading whitespace:
@ -232,6 +232,7 @@ fn trim_offset(src: &str, mut offset: usize) -> &str {
#[cfg(test)]
mod test {
use super::*;
use crate::style::Style;
use unicode_segmentation::UnicodeSegmentation;
enum Composer {
@ -240,7 +241,7 @@ mod test {
}
fn run_composer(which: Composer, text: &str, text_area_width: u16) -> (Vec<String>, Vec<u16>) {
let style = Default::default();
let style = Style::default();
let mut styled =
UnicodeSegmentation::graphemes(text, true).map(|g| StyledGrapheme { symbol: g, style });
let mut composer: Box<dyn LineComposer> = match which {
@ -379,7 +380,7 @@ mod test {
assert_eq!(line_truncator, vec!["", "a"]);
}
/// Tests WordWrapper with words some of which exceed line length and some not.
/// Tests `WordWrapper` with words some of which exceed line length and some not.
#[test]
fn line_composer_word_wrapper_mixed_length() {
let width = 20;

View file

@ -47,7 +47,7 @@ impl<'a> Default for Sparkline<'a> {
fn default() -> Sparkline<'a> {
Sparkline {
block: None,
style: Default::default(),
style: Style::default(),
data: &[],
max: None,
bar_set: symbols::bar::NINE_LEVELS,
@ -113,10 +113,10 @@ impl<'a> Widget for Sparkline<'a> {
.iter()
.take(max_index)
.map(|e| {
if max != 0 {
e * u64::from(spark_area.height) * 8 / max
} else {
if max == 0 {
0
} else {
e * u64::from(spark_area.height) * 8 / max
}
})
.collect::<Vec<u64>>();

View file

@ -103,7 +103,7 @@ impl<'a> Row<'a> {
{
Self {
height: 1,
cells: cells.into_iter().map(|c| c.into()).collect(),
cells: cells.into_iter().map(Into::into).collect(),
style: Style::default(),
bottom_margin: 0,
}
@ -269,8 +269,7 @@ impl<'a> Table<'a> {
fn get_columns_widths(&self, max_width: u16, has_selection: bool) -> Vec<u16> {
let mut constraints = Vec::with_capacity(self.widths.len() * 2 + 1);
if has_selection {
let highlight_symbol_width =
self.highlight_symbol.map(|s| s.width() as u16).unwrap_or(0);
let highlight_symbol_width = self.highlight_symbol.map_or(0, |s| s.width() as u16);
constraints.push(Constraint::Length(highlight_symbol_width));
}
for constraint in self.widths {
@ -452,7 +451,7 @@ impl<'a> StatefulWidget for Table<'a> {
height: table_row.height,
};
buf.set_style(table_row_area, table_row.style);
let is_selected = state.selected.map(|s| s == i).unwrap_or(false);
let is_selected = state.selected.map_or(false, |s| s == i);
let table_row_start_col = if has_selection {
let symbol = if is_selected {
highlight_symbol

View file

@ -48,8 +48,8 @@ impl<'a> Tabs<'a> {
block: None,
titles: titles.into_iter().map(Into::into).collect(),
selected: 0,
style: Default::default(),
highlight_style: Default::default(),
style: Style::default(),
highlight_style: Style::default(),
divider: Span::raw(symbols::line::VERTICAL),
}
}

View file

@ -38,5 +38,5 @@ fn widgets_canvas_draw_labels() {
for col in 0..4 {
expected.get_mut(col, 4).set_fg(Color::Blue);
}
terminal.backend().assert_buffer(&expected)
terminal.backend().assert_buffer(&expected);
}