From 93982517aee914c143d74acc7f45279893398cf6 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Fri, 13 Oct 2017 11:22:02 -0700 Subject: [PATCH] Fix clippy warnings --- src/backend/blt.rs | 4 ++-- src/backend/curses/n.rs | 4 ++-- src/backend/termion.rs | 6 ++---- src/event.rs | 14 ++------------ src/printer.rs | 2 +- src/views/checkbox.rs | 2 +- src/views/dialog.rs | 4 ++-- src/views/edit_view.rs | 6 +++--- src/views/id_view.rs | 3 ++- src/views/linear_layout.rs | 21 +++++++++++---------- src/views/list_view.rs | 8 +++----- src/views/menu_popup.rs | 28 +++++++++++----------------- src/views/menubar.rs | 7 +++---- src/views/on_event_view.rs | 2 +- src/views/radio.rs | 4 ++-- src/views/select_view.rs | 23 ++++++++++------------- src/views/slider_view.rs | 3 +-- src/views/text_view.rs | 9 +++------ 18 files changed, 62 insertions(+), 88 deletions(-) diff --git a/src/backend/blt.rs b/src/backend/blt.rs index 1f4eb7e..e246138 100644 --- a/src/backend/blt.rs +++ b/src/backend/blt.rs @@ -6,9 +6,9 @@ use self::bear_lib_terminal::terminal::{self, state, Event as BltEvent, KeyCode}; use backend; use event::{Event, Key, MouseButton, MouseEvent}; +use std::collections::HashSet; use theme::{BaseColor, Color, ColorPair, Effect}; use vec::Vec2; -use std::collections::HashSet; enum ColorRole { Foreground, @@ -235,7 +235,7 @@ impl backend::Backend for Concrete { event: MouseEvent::Hold(*btn), position: self.mouse_position, offset: Vec2::zero(), - } + }, } } BltEvent::MouseScroll { delta } => Event::Mouse { diff --git a/src/backend/curses/n.rs b/src/backend/curses/n.rs index fdf47d6..3252d1b 100644 --- a/src/backend/curses/n.rs +++ b/src/backend/curses/n.rs @@ -104,9 +104,9 @@ impl Concrete { // The event is either a mouse drag event, // or a weird double-release event. :S self.last_mouse_button - .map(|b| MouseEvent::Hold(b)) + .map(MouseEvent::Hold) .map(&make_event) - .unwrap_or(Event::Unknown(vec![])) + .unwrap_or_else(|| Event::Unknown(vec![])) } else { // Identify the button let mut bare_event = mevent.bstate & ((1 << 25) - 1); diff --git a/src/backend/termion.rs b/src/backend/termion.rs index 2e5aa0c..bfeac22 100644 --- a/src/backend/termion.rs +++ b/src/backend/termion.rs @@ -149,16 +149,14 @@ impl backend::Backend for Concrete { } }); - let backend = Concrete { + Concrete { terminal: terminal, current_style: Cell::new(theme::ColorPair::from_256colors(0, 0)), input: receiver, resize: resize, timeout: None, last_button: None, - }; - - backend + } } fn finish(&mut self) { diff --git a/src/event.rs b/src/event.rs index cfd05f9..ee57a79 100644 --- a/src/event.rs +++ b/src/event.rs @@ -313,12 +313,7 @@ pub enum Event { impl Event { /// Returns the position of the mouse, if `self` is a mouse event. pub fn mouse_position(&self) -> Option { - if let Event::Mouse { - offset: _, - position, - event: _, - } = *self - { + if let Event::Mouse { position, .. } = *self { Some(position) } else { None @@ -333,12 +328,7 @@ impl Event { where V: Into, { - if let Event::Mouse { - ref mut offset, - position: _, - event: _, - } = *self - { + if let Event::Mouse { ref mut offset, .. } = *self { *offset = *offset + top_left; } } diff --git a/src/printer.rs b/src/printer.rs index 158bf92..0eaa642 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -275,7 +275,7 @@ impl<'a> Printer<'a> { focused: self.focused && focused, theme: self.theme, backend: self.backend, - new: self.new.clone(), + new: Rc::clone(&self.new), } } diff --git a/src/views/checkbox.rs b/src/views/checkbox.rs index b95e7a7..282bb64 100644 --- a/src/views/checkbox.rs +++ b/src/views/checkbox.rs @@ -90,7 +90,7 @@ impl Checkbox { pub fn set_checked(&mut self, checked: bool) -> EventResult { self.checked = checked; if let Some(ref on_change) = self.on_change { - let on_change = on_change.clone(); + let on_change = Rc::clone(on_change); EventResult::with_cb(move |s| on_change(s, checked)) } else { EventResult::Consumed(None) diff --git a/src/views/dialog.rs b/src/views/dialog.rs index a2ca983..47b02b3 100644 --- a/src/views/dialog.rs +++ b/src/views/dialog.rs @@ -362,11 +362,11 @@ impl Dialog { } fn check_focus_grab(&mut self, event: &Event) { - if let &Event::Mouse { + if let Event::Mouse { offset, position, event, - } = event + } = *event { if !event.grabs_focus() { return; diff --git a/src/views/edit_view.rs b/src/views/edit_view.rs index fd8f6d8..584dab9 100644 --- a/src/views/edit_view.rs +++ b/src/views/edit_view.rs @@ -335,7 +335,7 @@ impl EditView { /// Get the current text. pub fn get_content(&self) -> Rc { - self.content.clone() + Rc::clone(&self.content) } /// Sets the current content to the given value. @@ -573,7 +573,7 @@ impl View for EditView { } Event::Key(Key::Enter) if self.on_submit.is_some() => { let cb = self.on_submit.clone().unwrap(); - let content = self.content.clone(); + let content = Rc::clone(&self.content); return EventResult::with_cb(move |s| { cb(s, &content); }); @@ -599,7 +599,7 @@ impl View for EditView { let cb = self.on_edit.clone().map(|cb| { // Get a new Rc on the content - let content = self.content.clone(); + let content = Rc::clone(&self.content); let cursor = self.cursor; Callback::from_fn(move |s| { diff --git a/src/views/id_view.rs b/src/views/id_view.rs index 33d2f72..94e817e 100644 --- a/src/views/id_view.rs +++ b/src/views/id_view.rs @@ -34,7 +34,7 @@ impl IdView { /// /// Panics if another reference for this view already exists. pub fn get_mut(&mut self) -> ViewRef { - let cell_ref = RcRef::new(self.view.clone()); + let cell_ref = RcRef::new(Rc::clone(&self.view)); OwningHandle::new_mut(cell_ref) } @@ -57,6 +57,7 @@ impl ViewWrapper for IdView { self.view.try_borrow_mut().ok().map(|mut v| f(&mut *v)) } + // Some for<'b> weirdness here to please the borrow checker gods... fn wrap_call_on_any<'a>( &mut self, selector: &Selector, mut callback: Box FnMut(&'b mut Any) + 'a>, diff --git a/src/views/linear_layout.rs b/src/views/linear_layout.rs index f50c8c4..26a67fa 100644 --- a/src/views/linear_layout.rs +++ b/src/views/linear_layout.rs @@ -43,7 +43,8 @@ struct ChildIterator { orientation: direction::Orientation, } -impl <'a,T: Deref, I: Iterator> Iterator for ChildIterator { +impl<'a, T: Deref, I: Iterator> Iterator + for ChildIterator { type Item = (usize, T); fn next(&mut self) -> Option { @@ -184,11 +185,11 @@ impl LinearLayout { } fn check_focus_grab(&mut self, event: &Event) { - if let &Event::Mouse { + if let Event::Mouse { offset, position, event, - } = event + } = *event { if !event.grabs_focus() { return; @@ -208,13 +209,13 @@ impl LinearLayout { }; for (i, (offset, child)) in iterator.enumerate() { let child_size = child.size.get(self.orientation); - // eprintln!("Offset {:?}, size {:?}, position: {:?}", offset, child_size, position); - if offset + child_size > position { - if child.view.take_focus(direction::Direction::none()) { - // eprintln!("It's a match!"); - self.focus = i; - return; - } + // eprintln!("Offset {:?}, size {:?}, position: {:?}", offset, child_size, position); + if (offset + child_size > position) + && child.view.take_focus(direction::Direction::none()) + { + // eprintln!("It's a match!"); + self.focus = i; + return; } } } diff --git a/src/views/list_view.rs b/src/views/list_view.rs index f857194..42a5aba 100644 --- a/src/views/list_view.rs +++ b/src/views/list_view.rs @@ -206,11 +206,11 @@ impl ListView { } fn check_focus_grab(&mut self, event: &Event) { - if let &Event::Mouse { + if let Event::Mouse { offset, position, event, - } = event + } = *event { if !event.grabs_focus() { return; @@ -229,9 +229,7 @@ impl ListView { // Now that we have a relative position, checks for buttons? let focus = position.y + self.scrollbase.start_line; - if let &mut ListChild::Row(_, ref mut view) = - &mut self.children[focus] - { + if let ListChild::Row(_, ref mut view) = self.children[focus] { if view.take_focus(direction::Direction::none()) { self.focus = focus; } diff --git a/src/views/menu_popup.rs b/src/views/menu_popup.rs index 8f08cdc..d2d963a 100644 --- a/src/views/menu_popup.rs +++ b/src/views/menu_popup.rs @@ -139,7 +139,7 @@ impl MenuPopup { } fn make_subtree_cb(&self, tree: &Rc) -> EventResult { - let tree = tree.clone(); + let tree = Rc::clone(tree); let max_width = 4 + self.menu .children @@ -155,7 +155,7 @@ impl MenuPopup { s.screen_mut().add_layer_at( Position::parent(offset), OnEventView::new( - MenuPopup::new(tree.clone()).on_action(move |s| { + MenuPopup::new(Rc::clone(&tree)).on_action(move |s| { // This will happen when the subtree popup // activates something; // First, remove ourselve. @@ -271,9 +271,6 @@ impl View for MenuPopup { fn on_event(&mut self, event: Event) -> EventResult { let mut fix_scroll = true; match event { - Event::Key(Key::Esc) => { - return self.dismiss(); - } Event::Key(Key::Up) => self.scroll_up(1, true), Event::Key(Key::PageUp) => self.scroll_up(5, false), Event::Key(Key::Down) => self.scroll_down(1, true), @@ -301,8 +298,7 @@ impl View for MenuPopup { } Event::Mouse { event: MouseEvent::WheelUp, - position: _, - offset: _, + .. } if self.scrollbase.can_scroll_up() => { fix_scroll = false; @@ -310,8 +306,7 @@ impl View for MenuPopup { } Event::Mouse { event: MouseEvent::WheelDown, - position: _, - offset: _, + .. } if self.scrollbase.can_scroll_down() => { fix_scroll = false; @@ -373,20 +368,19 @@ impl View for MenuPopup { if let Some(position) = position.checked_sub(offset + (1, 1)) { - if position < self.last_size.saturating_sub((2, 2)) { - if position.y + self.scrollbase.start_line - == self.focus - { - return self.submit(); - } + if position < self.last_size.saturating_sub((2, 2)) + && (position.y + self.scrollbase.start_line + == self.focus) + { + return self.submit(); } } } } + Event::Key(Key::Esc) | Event::Mouse { event: MouseEvent::Press(_), - position: _, - offset: _, + .. } => { return self.dismiss(); } diff --git a/src/views/menubar.rs b/src/views/menubar.rs index 2b09667..b0e676d 100644 --- a/src/views/menubar.rs +++ b/src/views/menubar.rs @@ -156,7 +156,7 @@ impl Menubar { fn select_child(&mut self) -> EventResult { // First, we need a new Rc to send the callback, // since we don't know when it will be called. - let menu = self.menus[self.focus].1.clone(); + let menu = Rc::clone(&self.menus[self.focus].1); self.state = State::Submenu; let offset = ( self.menus[..self.focus] @@ -167,7 +167,7 @@ impl Menubar { ); // Since the closure will be called multiple times, // we also need a new Rc on every call. - EventResult::with_cb(move |s| show_child(s, offset, menu.clone())) + EventResult::with_cb(move |s| show_child(s, offset, Rc::clone(&menu))) } } @@ -264,8 +264,7 @@ impl View for Menubar { } Event::Mouse { event: MouseEvent::Press(_), - position: _, - offset: _, + .. } => { self.hide(); return EventResult::with_cb(|s| s.clear()); diff --git a/src/views/on_event_view.rs b/src/views/on_event_view.rs index e06bf3a..ac83548 100644 --- a/src/views/on_event_view.rs +++ b/src/views/on_event_view.rs @@ -34,7 +34,7 @@ impl Clone for Action { fn clone(&self) -> Self { Action { phase: self.phase.clone(), - callback: self.callback.clone(), + callback: Rc::clone(&self.callback), } } } diff --git a/src/views/radio.rs b/src/views/radio.rs index 19ab3ae..f0b1445 100644 --- a/src/views/radio.rs +++ b/src/views/radio.rs @@ -14,7 +14,7 @@ struct SharedState { impl SharedState { pub fn selection(&self) -> Rc { - self.values[self.selection].clone() + Rc::clone(&self.values[self.selection]) } } @@ -56,7 +56,7 @@ impl RadioGroup { ) -> RadioButton { let count = self.state.borrow().values.len(); self.state.borrow_mut().values.push(Rc::new(value)); - RadioButton::new(self.state.clone(), count, label.into()) + RadioButton::new(Rc::clone(&self.state), count, label.into()) } /// Returns the id of the selected button. diff --git a/src/views/select_view.rs b/src/views/select_view.rs index eb4c53d..63df8c5 100644 --- a/src/views/select_view.rs +++ b/src/views/select_view.rs @@ -200,7 +200,7 @@ impl SelectView { /// /// Panics if the list is empty. pub fn selection(&self) -> Rc { - self.items[self.focus()].value.clone() + Rc::clone(&self.items[self.focus()].value) } /// Removes all items from this view. @@ -341,8 +341,7 @@ impl SelectView { } Event::Mouse { event: MouseEvent::WheelDown, - position: _, - offset: _, + .. } if self.scrollbase.can_scroll_down() => { fix_scroll = false; @@ -350,8 +349,7 @@ impl SelectView { } Event::Mouse { event: MouseEvent::WheelUp, - position: _, - offset: _, + .. } if self.scrollbase.can_scroll_up() => { fix_scroll = false; @@ -400,12 +398,11 @@ impl SelectView { self.scrollbase.release_grab(); if self.on_submit.is_some() { if let Some(position) = position.checked_sub(offset) { - if position < self.last_size { - if position.y + self.scrollbase.start_line + if position < self.last_size + && (position.y + self.scrollbase.start_line) == self.focus() - { - return self.submit(); - } + { + return self.submit(); } } } @@ -447,9 +444,9 @@ impl SelectView { // TODO: cache it? let mut tree = MenuTree::new(); for (i, item) in self.items.iter().enumerate() { - let focus = self.focus.clone(); + let focus = Rc::clone(&self.focus); let on_submit = self.on_submit.as_ref().cloned(); - let value = item.value.clone(); + let value = Rc::clone(&item.value); tree.add_leaf(item.label.clone(), move |s| { focus.set(i); if let Some(ref on_submit) = on_submit { @@ -480,7 +477,7 @@ impl SelectView { // And now, we can return the callback that will create the popup. EventResult::with_cb(move |s| { // The callback will want to work with a fresh Rc - let tree = tree.clone(); + let tree = Rc::clone(&tree); // We'll relativise the absolute position, // So that we are locked to the parent view. // A nice effect is that window resizes will keep both diff --git a/src/views/slider_view.rs b/src/views/slider_view.rs index c382863..b6f7875 100644 --- a/src/views/slider_view.rs +++ b/src/views/slider_view.rs @@ -194,8 +194,7 @@ impl View for SliderView { } Event::Mouse { event: MouseEvent::Release(MouseButton::Left), - position: _, - offset: _, + .. } => { self.dragging = false; EventResult::Ignored diff --git a/src/views/text_view.rs b/src/views/text_view.rs index 34cb555..33a7d91 100644 --- a/src/views/text_view.rs +++ b/src/views/text_view.rs @@ -270,16 +270,14 @@ impl View for TextView { } Event::Mouse { event: MouseEvent::WheelDown, - position: _, - offset: _, + .. } if self.scrollbase.can_scroll_down() => { self.scrollbase.scroll_down(5) } Event::Mouse { event: MouseEvent::WheelUp, - position: _, - offset: _, + .. } if self.scrollbase.can_scroll_up() => { self.scrollbase.scroll_up(5) @@ -312,8 +310,7 @@ impl View for TextView { } Event::Mouse { event: MouseEvent::Release(MouseButton::Left), - position: _, - offset: _, + .. } => { self.scrollbase.release_grab(); }