From 755854963f675df304f14423e064cfea821e0397 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Thu, 17 May 2018 17:35:57 -0700 Subject: [PATCH] Fix clippy warnings --- src/align.rs | 2 +- src/backend/curses/mod.rs | 2 +- src/cursive.rs | 4 +-- src/printer.rs | 2 +- src/utils/lines/simple/mod.rs | 2 +- src/utils/reader.rs | 4 +-- src/view/view_wrapper.rs | 2 +- src/views/edit_view.rs | 4 +-- src/views/id_view.rs | 14 +++------ src/views/layer.rs | 2 +- src/views/menu_popup.rs | 36 +++++++++------------- src/views/select_view.rs | 4 +-- src/views/shadow_view.rs | 2 +- src/views/sized_view.rs | 2 +- src/views/slider_view.rs | 8 ++--- src/views/text_area.rs | 57 +++++++++++------------------------ src/views/tracked_view.rs | 2 +- 17 files changed, 57 insertions(+), 92 deletions(-) diff --git a/src/align.rs b/src/align.rs index 3c56e1d..3291d23 100644 --- a/src/align.rs +++ b/src/align.rs @@ -12,7 +12,7 @@ pub struct Align { impl Align { /// Creates a new Align object from the given alignments. pub fn new(h: HAlign, v: VAlign) -> Self { - Align { h: h, v: v } + Align { h, v } } /// Creates a top-left alignment. diff --git a/src/backend/curses/mod.rs b/src/backend/curses/mod.rs index 7458b31..66935aa 100644 --- a/src/backend/curses/mod.rs +++ b/src/backend/curses/mod.rs @@ -95,7 +95,7 @@ fn find_closest(color: &Color, max_colors: i16) -> i16 { // (r - 8) / 10 = n // let n = (r - 8) / 10; - (232 + n) as i16 + i16::from(232 + n) } else { // Generic RGB let r = 6 * u16::from(r) / 256; diff --git a/src/cursive.rs b/src/cursive.rs index 7d09407..e8555fb 100644 --- a/src/cursive.rs +++ b/src/cursive.rs @@ -97,7 +97,7 @@ impl Cursive { let (tx, rx) = mpsc::channel(); Cursive { - theme: theme, + theme, screens: vec![views::StackView::new()], last_sizes: Vec::new(), global_callbacks: HashMap::new(), @@ -106,7 +106,7 @@ impl Cursive { running: true, cb_source: rx, cb_sink: tx, - backend: backend, + backend, } } diff --git a/src/printer.rs b/src/printer.rs index 1f5cac4..0a30662 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -100,7 +100,7 @@ impl<'a> Printer<'a> { // We accept requests between `content_offset` and // `content_offset + size` - if !(start < self.output_size + self.content_offset) { + if start >= self.output_size + self.content_offset { return; } diff --git a/src/utils/lines/simple/mod.rs b/src/utils/lines/simple/mod.rs index 55dc1bd..553c6a3 100644 --- a/src/utils/lines/simple/mod.rs +++ b/src/utils/lines/simple/mod.rs @@ -82,7 +82,7 @@ where debug_assert!(current_width <= available_width + delimiter_width); Span { - length: length, + length, width: current_width, } } diff --git a/src/utils/reader.rs b/src/utils/reader.rs index 1f5485f..8cf2c6c 100644 --- a/src/utils/reader.rs +++ b/src/utils/reader.rs @@ -19,8 +19,8 @@ impl ProgressReader { /// many bytes should be received. pub fn new(counter: Counter, reader: R) -> Self { ProgressReader { - reader: reader, - counter: counter, + reader, + counter, } } diff --git a/src/view/view_wrapper.rs b/src/view/view_wrapper.rs index aa940b8..442355c 100644 --- a/src/view/view_wrapper.rs +++ b/src/view/view_wrapper.rs @@ -98,7 +98,7 @@ pub trait ViewWrapper: 'static { /// Wraps the `important_area` method. fn wrap_important_area(&self, size: Vec2) -> Rect { self.with_view(|v| v.important_area(size)) - .unwrap_or(Rect::from((0, 0))) + .unwrap_or_else(|| Rect::from((0, 0))) } } diff --git a/src/views/edit_view.rs b/src/views/edit_view.rs index d824024..56b334e 100644 --- a/src/views/edit_view.rs +++ b/src/views/edit_view.rs @@ -667,13 +667,13 @@ impl View for EditView { offset, } if position.fits_in_rect(offset, (self.last_length, 1)) => { - position.checked_sub(offset).map(|position| { + if let Some(position) = position.checked_sub(offset) { self.cursor = self.offset + simple_prefix( &self.content[self.offset..], position.x, ).length; - }); + } } _ => return EventResult::Ignored, } diff --git a/src/views/id_view.rs b/src/views/id_view.rs index deab24a..703e265 100644 --- a/src/views/id_view.rs +++ b/src/views/id_view.rs @@ -58,10 +58,7 @@ impl ViewWrapper for IdView { where F: FnOnce(&mut Self::V) -> R, { - self.view - .try_borrow_mut() - .ok() - .map(|mut v| f(&mut *v)) + self.view.try_borrow_mut().ok().map(|mut v| f(&mut *v)) } fn into_inner(mut self) -> Result @@ -80,15 +77,14 @@ impl ViewWrapper for IdView { // Some for<'b> weirdness here to please the borrow checker gods... fn wrap_call_on_any<'a>( - &mut self, selector: &Selector, mut callback: BoxedCallback<'a> + &mut self, selector: &Selector, mut callback: BoxedCallback<'a>, ) { match selector { &Selector::Id(id) if id == self.id => callback(self), s => { - self.view - .try_borrow_mut() - .ok() - .map(|mut v| v.deref_mut().call_on_any(s, callback)); + if let Ok(mut v) = self.view.try_borrow_mut() { + v.deref_mut().call_on_any(s, callback); + } } } } diff --git a/src/views/layer.rs b/src/views/layer.rs index 14ee200..2815cdb 100644 --- a/src/views/layer.rs +++ b/src/views/layer.rs @@ -14,7 +14,7 @@ pub struct Layer { impl Layer { /// Wraps the given view. pub fn new(view: T) -> Self { - Layer { view: view } + Layer { view } } inner_getters!(self.view: T); diff --git a/src/views/menu_popup.rs b/src/views/menu_popup.rs index 42aa9d2..4f1c8e1 100644 --- a/src/views/menu_popup.rs +++ b/src/views/menu_popup.rs @@ -1,6 +1,3 @@ -use Cursive; -use Printer; -use With; use align::Align; use event::{Callback, Event, EventResult, Key, MouseButton, MouseEvent}; use menu::{MenuItem, MenuTree}; @@ -11,6 +8,9 @@ use unicode_width::UnicodeWidthStr; use vec::Vec2; use view::{Position, ScrollBase, View}; use views::OnEventView; +use Cursive; +use Printer; +use With; /// Popup that shows a list of items. pub struct MenuPopup { @@ -29,9 +29,7 @@ impl MenuPopup { MenuPopup { menu, focus: 0, - scrollbase: ScrollBase::new() - .scrollbar_offset(1) - .right_padding(0), + scrollbase: ScrollBase::new().scrollbar_offset(1).right_padding(0), align: Align::top_left(), on_dismiss: None, on_action: None, @@ -318,8 +316,7 @@ impl View for MenuPopup { && position .checked_sub(offset + (0, 1)) .map(|position| { - self.scrollbase - .start_drag(position, self.last_size.x) + self.scrollbase.start_drag(position, self.last_size.x) }) .unwrap_or(false) => { @@ -344,19 +341,16 @@ impl View for MenuPopup { // eprintln!("Position: {:?} / {:?}", position, offset); // eprintln!("Last size: {:?}", self.last_size); let inner_size = self.last_size.saturating_sub((2, 2)); - position.checked_sub(offset + (1, 1)).map( + if let Some(position) = position.checked_sub(offset + (1, 1)) { // `position` is not relative to the content // (It's inside the border) - |position| { - if position < inner_size { - let focus = - position.y + self.scrollbase.start_line; - if !self.menu.children[focus].is_delimiter() { - self.focus = focus; - } + if position < inner_size { + let focus = position.y + self.scrollbase.start_line; + if !self.menu.children[focus].is_delimiter() { + self.focus = focus; } - }, - ); + } + } } Event::Mouse { event: MouseEvent::Release(MouseButton::Left), @@ -398,10 +392,8 @@ impl View for MenuPopup { fn layout(&mut self, size: Vec2) { self.last_size = size; - self.scrollbase.set_heights( - size.y.saturating_sub(2), - self.menu.children.len(), - ); + self.scrollbase + .set_heights(size.y.saturating_sub(2), self.menu.children.len()); } fn important_area(&self, size: Vec2) -> Rect { diff --git a/src/views/select_view.rs b/src/views/select_view.rs index 8277149..4555ef6 100644 --- a/src/views/select_view.rs +++ b/src/views/select_view.rs @@ -784,7 +784,7 @@ impl View for SelectView { fn important_area(&self, size: Vec2) -> Rect { self.selected_id() .map(|i| Rect::from_size((0, i), (size.x, 1))) - .unwrap_or(Rect::from((0, 0))) + .unwrap_or_else(|| Rect::from((0, 0))) } } @@ -796,7 +796,7 @@ struct Item { impl Item { fn new(label: String, value: T) -> Self { Item { - label: label, + label, value: Rc::new(value), } } diff --git a/src/views/shadow_view.rs b/src/views/shadow_view.rs index 911b3fc..246269d 100644 --- a/src/views/shadow_view.rs +++ b/src/views/shadow_view.rs @@ -17,7 +17,7 @@ impl ShadowView { /// Wraps the given view. pub fn new(view: T) -> Self { ShadowView { - view: view, + view, top_padding: true, left_padding: true, } diff --git a/src/views/sized_view.rs b/src/views/sized_view.rs index 5c13b71..2855634 100644 --- a/src/views/sized_view.rs +++ b/src/views/sized_view.rs @@ -15,7 +15,7 @@ impl SizedView { /// Wraps the given view. pub fn new(view: T) -> Self { SizedView { - view: view, + view, size: Vec2::zero(), } } diff --git a/src/views/slider_view.rs b/src/views/slider_view.rs index 01476ef..b496545 100644 --- a/src/views/slider_view.rs +++ b/src/views/slider_view.rs @@ -24,9 +24,9 @@ impl SliderView { /// with one tick per block. pub fn new(orientation: Orientation, max_value: usize) -> Self { SliderView { - orientation: orientation, + orientation, value: 0, - max_value: max_value, + max_value, on_change: None, on_enter: None, dragging: false, @@ -186,10 +186,10 @@ impl View for SliderView { offset, } if position.fits_in_rect(offset, self.req_size()) => { - position.checked_sub(offset).map(|position| { + if let Some(position) = position.checked_sub(offset) { self.dragging = true; self.value = self.orientation.get(&position); - }); + } self.get_change_result() } Event::Mouse { diff --git a/src/views/text_area.rs b/src/views/text_area.rs index 9ea5be7..ca38a46 100644 --- a/src/views/text_area.rs +++ b/src/views/text_area.rs @@ -1,4 +1,3 @@ -use {Printer, With, XY}; use direction::Direction; use event::{Event, EventResult, Key, MouseButton, MouseEvent}; use rect::Rect; @@ -9,6 +8,7 @@ use unicode_width::UnicodeWidthStr; use utils::lines::simple::{prefix, simple_prefix, LinesIterator, Row}; use vec::Vec2; use view::{ScrollBase, SizeCache, View}; +use {Printer, With, XY}; /// Multi-lines text editor. /// @@ -36,9 +36,7 @@ pub struct TextArea { } fn make_rows(text: &str, width: usize) -> Vec { - LinesIterator::new(text, width) - .show_spaces() - .collect() + LinesIterator::new(text, width).show_spaces().collect() } new_default!(TextArea); @@ -290,8 +288,7 @@ impl TextArea { fn compute_rows(&mut self, size: Vec2) { self.soft_compute_rows(size); - self.scrollbase - .set_heights(size.y, self.rows.len()); + self.scrollbase.set_heights(size.y, self.rows.len()); } fn backspace(&mut self) { @@ -388,16 +385,11 @@ impl TextArea { let last_byte = self.content[self.cursor..] .find('\n') .map(|i| 1 + i + self.cursor); - let last_row = last_byte.map_or(self.rows.len(), |last_byte| { - self.row_at(last_byte) - }); + let last_row = last_byte + .map_or(self.rows.len(), |last_byte| self.row_at(last_byte)); let last_byte = last_byte.unwrap_or_else(|| self.content.len()); - debug!( - "Content: `{}` (len={})", - self.content, - self.content.len() - ); + debug!("Content: `{}` (len={})", self.content, self.content.len()); debug!("start/end: {}/{}", first_byte, last_byte); debug!("start/end rows: {}/{}", first_row, last_row); @@ -431,14 +423,11 @@ impl TextArea { // Otherwise, replace stuff. let affected_rows = first_row..last_row; - let replacement_rows = new_rows - .into_iter() - .map(|row| row.shifted(first_byte)); - self.rows - .splice(affected_rows, replacement_rows); + let replacement_rows = + new_rows.into_iter().map(|row| row.shifted(first_byte)); + self.rows.splice(affected_rows, replacement_rows); self.fix_ghost_row(); - self.scrollbase - .set_heights(size.y, self.rows.len()); + self.scrollbase.set_heights(size.y, self.rows.len()); } } @@ -451,18 +440,10 @@ impl View for TextArea { // (we always keep a space at the end) // And y = number of rows debug!("{:?}", self.rows); - let scroll_width = if self.rows.len() > constraint.y { - 1 - } else { - 0 - }; + let scroll_width = if self.rows.len() > constraint.y { 1 } else { 0 }; Vec2::new( scroll_width + 1 - + self.rows - .iter() - .map(|r| r.width) - .max() - .unwrap_or(1), + + self.rows.iter().map(|r| r.width).max().unwrap_or(1), self.rows.len(), ) } @@ -573,8 +554,7 @@ impl View for TextArea { } if position .checked_sub(offset) .map(|position| { - self.scrollbase - .start_drag(position, self.last_size.x) + self.scrollbase.start_drag(position, self.last_size.x) }) .unwrap_or(false) => { @@ -593,13 +573,10 @@ impl View for TextArea { event: MouseEvent::Press(_), position, offset, - } if position.fits_in_rect(offset, self.last_size) => + } if !self.rows.is_empty() + && position.fits_in_rect(offset, self.last_size) => { - position.checked_sub(offset).map(|position| { - if self.rows.is_empty() { - return; - } - + if let Some(position) = position.checked_sub(offset) { let y = position.y + self.scrollbase.start_line; let y = min(y, self.rows.len() - 1); let x = position.x; @@ -607,7 +584,7 @@ impl View for TextArea { let content = &self.content[row.start..row.end]; self.cursor = row.start + simple_prefix(content, x).length; - }); + } } _ => return EventResult::Ignored, } diff --git a/src/views/tracked_view.rs b/src/views/tracked_view.rs index 8378ce5..0f9ce85 100644 --- a/src/views/tracked_view.rs +++ b/src/views/tracked_view.rs @@ -21,7 +21,7 @@ impl TrackedView { /// Creates a new `TrackedView` around `view`. pub fn new(view: T) -> Self { TrackedView { - view: view, + view, offset: Cell::new(Vec2::zero()), } }