Fix clippy warnings

This commit is contained in:
Alexandre Bury 2019-03-04 10:31:36 -08:00
parent ed7fca5b54
commit 242a3c68a7
20 changed files with 65 additions and 42 deletions

View File

@ -27,7 +27,7 @@ pub mod termion;
/// ///
/// It usually delegates the work to a terminal-handling library like ncurses /// It usually delegates the work to a terminal-handling library like ncurses
/// or termion, or it can entirely simulate a terminal and show it as a /// or termion, or it can entirely simulate a terminal and show it as a
/// graphical window (BearLibTerminal). /// graphical window (`BearLibTerminal`).
/// ///
/// When creating a new cursive tree with `Cursive::new()`, you will need to /// When creating a new cursive tree with `Cursive::new()`, you will need to
/// provide a backend initializer - usually their `init()` function. /// provide a backend initializer - usually their `init()` function.

View File

@ -358,7 +358,7 @@ impl Cursive {
/// Clears the screen. /// Clears the screen.
/// ///
/// Users rarely have to call this directly. /// Users rarely have to call this directly.
pub fn clear(&self) { pub fn clear(&mut self) {
self.backend self.backend
.clear(self.theme.palette[theme::PaletteColor::Background]); .clear(self.theme.palette[theme::PaletteColor::Background]);
} }

View File

@ -112,7 +112,7 @@ impl MenuTree {
/// Adds a delimiter to the end of this tree - chainable variant. /// Adds a delimiter to the end of this tree - chainable variant.
pub fn delimiter(self) -> Self { pub fn delimiter(self) -> Self {
self.with(|menu| menu.add_delimiter()) self.with(Self::add_delimiter)
} }
/// Adds a actionnable leaf to the end of this tree. /// Adds a actionnable leaf to the end of this tree.

View File

@ -110,7 +110,7 @@ impl<'a, 'b> Printer<'a, 'b> {
// We accept requests between `content_offset` and // We accept requests between `content_offset` and
// `content_offset + output_size`. // `content_offset + output_size`.
if !(start < (self.output_size + self.content_offset)) { if !start.strictly_lt(self.output_size + self.content_offset) {
return; return;
} }
@ -176,7 +176,7 @@ impl<'a, 'b> Printer<'a, 'b> {
// Here again, we can abort if we're trying to print too far right or // Here again, we can abort if we're trying to print too far right or
// too low. // too low.
if !(start < (self.output_size + self.content_offset)) { if !start.strictly_lt(self.output_size + self.content_offset) {
return; return;
} }
@ -220,7 +220,7 @@ impl<'a, 'b> Printer<'a, 'b> {
let start = start.into(); let start = start.into();
// Nothing to be done if the start if too far to the bottom/right // Nothing to be done if the start if too far to the bottom/right
if !(start < (self.output_size + self.content_offset)) { if !start.strictly_lt(self.output_size + self.content_offset) {
return; return;
} }

View File

@ -23,7 +23,7 @@ where
_ => unreachable!(), _ => unreachable!(),
}; };
let mut res = 0u32; let mut res = 0_u32;
// First, get the data - only the few last bits // First, get the data - only the few last bits
res |= u32::from(first & make_mask(7 - n_bytes)); res |= u32::from(first & make_mask(7 - n_bytes));
@ -52,7 +52,7 @@ where
// Returns a simple bitmask with n 1s to the right. // Returns a simple bitmask with n 1s to the right.
#[allow(dead_code)] #[allow(dead_code)]
fn make_mask(n: usize) -> u8 { fn make_mask(n: usize) -> u8 {
let mut r = 0u8; let mut r = 0_u8;
for i in 0..n { for i in 0..n {
r |= 1 << i; r |= 1 << i;
} }

View File

@ -33,8 +33,7 @@ pub trait SpannedText {
fn spans(&self) -> &[Self::S]; fn spans(&self) -> &[Self::S];
/// Returns a `SpannedText` by reference. /// Returns a `SpannedText` by reference.
#[cfg_attr(feature = "cargo-clippy", allow(needless_lifetimes))] fn as_ref(&self) -> SpannedTextRef<'_, Self> {
fn as_ref<'a>(&'a self) -> SpannedTextRef<'a, Self> {
SpannedTextRef { r: self } SpannedTextRef { r: self }
} }
} }
@ -231,7 +230,7 @@ impl<T> SpannedString<T> {
} }
/// Gives access to the parsed styled spans. /// Gives access to the parsed styled spans.
pub fn spans<'a>(&'a self) -> impl Iterator<Item = Span<'a, T>> { pub fn spans(&self) -> impl Iterator<Item = Span<'_, T>> {
let source = &self.source; let source = &self.source;
self.spans.iter().map(move |span| span.resolve(source)) self.spans.iter().map(move |span| span.resolve(source))
} }

View File

@ -186,6 +186,18 @@ impl<T: Ord> XY<T> {
self.x >= other.x && self.y >= other.y self.x >= other.x && self.y >= other.y
} }
/// Returns `true` if `other` is strictly less than `self` in each axis.
pub fn strictly_lt<O: Into<Self>>(&self, other: O) -> bool {
let other = other.into();
self < &other
}
/// Returns `true` if `other` is strictly greater than `self` in each axis.
pub fn strictly_gt<O: Into<Self>>(&self, other: O) -> bool {
let other = other.into();
self > &other
}
/// Returns a new Vec2 that is a maximum per coordinate. /// Returns a new Vec2 that is a maximum per coordinate.
/// ///
/// # Examples /// # Examples

View File

@ -45,8 +45,8 @@ mod margins;
mod position; mod position;
mod size_cache; mod size_cache;
mod size_constraint; mod size_constraint;
mod view;
mod view_path; mod view_path;
mod view_trait;
// Helper bases // Helper bases
mod boxable; mod boxable;
@ -67,6 +67,6 @@ pub use self::scroll::{ScrollBase, ScrollStrategy};
pub use self::scrollable::Scrollable; pub use self::scrollable::Scrollable;
pub use self::size_cache::SizeCache; pub use self::size_cache::SizeCache;
pub use self::size_constraint::SizeConstraint; pub use self::size_constraint::SizeConstraint;
pub use self::view::View;
pub use self::view_path::ViewPath; pub use self::view_path::ViewPath;
pub use self::view_trait::View;
pub use self::view_wrapper::ViewWrapper; pub use self::view_wrapper::ViewWrapper;

View File

@ -92,7 +92,7 @@ pub trait ViewWrapper: 'static {
/// Wraps the `needs_relayout` method. /// Wraps the `needs_relayout` method.
fn wrap_needs_relayout(&self) -> bool { fn wrap_needs_relayout(&self) -> bool {
self.with_view(|v| v.needs_relayout()).unwrap_or(true) self.with_view(View::needs_relayout).unwrap_or(true)
} }
/// Wraps the `important_area` method. /// Wraps the `important_area` method.

View File

@ -220,9 +220,7 @@ impl<T: View> ViewWrapper for BoxView<T> {
.size .size
.zip_map(child_size.zip(req), SizeConstraint::result); .zip_map(child_size.zip(req), SizeConstraint::result);
if !self.squishable { if self.squishable {
result
} else {
// When we're squishable, special behaviour: // When we're squishable, special behaviour:
// //
@ -232,6 +230,8 @@ impl<T: View> ViewWrapper for BoxView<T> {
// If we respect the request, keep the result // If we respect the request, keep the result
// Otherwise, take the child as squish attempt. // Otherwise, take the child as squish attempt.
respect_req.select_or(result, child_size) respect_req.select_or(result, child_size)
} else {
result
} }
} }

View File

@ -144,10 +144,10 @@ impl View for Button {
let style = if !(self.enabled && printer.enabled) { let style = if !(self.enabled && printer.enabled) {
ColorStyle::secondary() ColorStyle::secondary()
} else if !printer.focused { } else if printer.focused {
ColorStyle::primary()
} else {
ColorStyle::highlight() ColorStyle::highlight()
} else {
ColorStyle::primary()
}; };
let offset = let offset =

View File

@ -18,6 +18,12 @@ impl DebugView {
} }
} }
impl Default for DebugView {
fn default() -> Self {
Self::new()
}
}
impl View for DebugView { impl View for DebugView {
fn draw(&self, printer: &Printer<'_, '_>) { fn draw(&self, printer: &Printer<'_, '_>) {
let logs = logger::LOGS.lock().unwrap(); let logs = logger::LOGS.lock().unwrap();

View File

@ -298,7 +298,9 @@ impl Dialog {
event.relativized((self.padding + self.borders).top_left()), event.relativized((self.padding + self.borders).top_left()),
) { ) {
EventResult::Ignored => { EventResult::Ignored => {
if !self.buttons.is_empty() { if self.buttons.is_empty() {
EventResult::Ignored
} else {
match event { match event {
Event::Key(Key::Down) | Event::Key(Key::Tab) => { Event::Key(Key::Down) | Event::Key(Key::Tab) => {
// Default to leftmost button when going down. // Default to leftmost button when going down.
@ -307,8 +309,6 @@ impl Dialog {
} }
_ => EventResult::Ignored, _ => EventResult::Ignored,
} }
} else {
EventResult::Ignored
} }
} }
res => res, res => res,
@ -630,11 +630,11 @@ impl View for Dialog {
if self.content.take_focus(source) { if self.content.take_focus(source) {
self.focus = DialogFocus::Content; self.focus = DialogFocus::Content;
true true
} else if !self.buttons.is_empty() { } else if self.buttons.is_empty() {
false
} else {
self.focus = DialogFocus::Button(0); self.focus = DialogFocus::Button(0);
true true
} else {
false
} }
} }
Direction::Rel(Relative::Back) Direction::Rel(Relative::Back)

View File

@ -454,7 +454,7 @@ impl EditView {
// (either a char, or _) // (either a char, or _)
let c_len = self.content[self.cursor..] let c_len = self.content[self.cursor..]
.graphemes(true) .graphemes(true)
.map(|g| g.width()) .map(UnicodeWidthStr::width)
.next() .next()
.unwrap_or(1); .unwrap_or(1);
@ -544,7 +544,7 @@ impl View for EditView {
Some(g) Some(g)
} }
}) })
.map(|g| g.len()) .map(str::len)
.sum(); .sum();
let content = &content[..display_bytes]; let content = &content[..display_bytes];

View File

@ -234,7 +234,7 @@ fn show_child(s: &mut Cursive, offset: Vec2, menu: Rc<MenuTree>) {
Position::absolute(offset), Position::absolute(offset),
OnEventView::new( OnEventView::new(
MenuPopup::new(menu) MenuPopup::new(menu)
.on_dismiss(|s| s.select_menubar()) .on_dismiss(Cursive::select_menubar)
.on_action(|s| s.menubar().state = State::Inactive), .on_action(|s| s.menubar().state = State::Inactive),
) )
.on_event(Key::Right, |s| { .on_event(Key::Right, |s| {
@ -289,7 +289,7 @@ impl View for Menubar {
match event { match event {
Event::Key(Key::Esc) => { Event::Key(Key::Esc) => {
self.hide(); self.hide();
return EventResult::with_cb(|s| s.clear()); return EventResult::with_cb(Cursive::clear);
} }
Event::Key(Key::Left) => loop { Event::Key(Key::Left) => loop {
if self.focus > 0 { if self.focus > 0 {
@ -356,7 +356,7 @@ impl View for Menubar {
.. ..
} => { } => {
self.hide(); self.hide();
return EventResult::with_cb(|s| s.clear()); return EventResult::with_cb(Cursive::clear);
} }
_ => return EventResult::Ignored, _ => return EventResult::Ignored,
} }

View File

@ -45,7 +45,7 @@ pub struct ProgressBar {
fn make_percentage(value: usize, (min, max): (usize, usize)) -> String { fn make_percentage(value: usize, (min, max): (usize, usize)) -> String {
if value < min { if value < min {
return format!("0 %"); return String::from("0 %");
} }
let (percentage, extra) = ratio(value - min, max - min, 100); let (percentage, extra) = ratio(value - min, max - min, 100);

View File

@ -343,14 +343,15 @@ where
/// Returns `(inner_size, desired_size)` /// Returns `(inner_size, desired_size)`
fn sizes(&mut self, constraint: Vec2, strict: bool) -> (Vec2, Vec2) { fn sizes(&mut self, constraint: Vec2, strict: bool) -> (Vec2, Vec2) {
// First: try the cache // First: try the cache
if !self.inner.needs_relayout() let valid_cache = !self.inner.needs_relayout()
&& self && self
.size_cache .size_cache
.map(|cache| { .map(|cache| {
cache.zip_map(constraint, SizeCache::accept).both() cache.zip_map(constraint, SizeCache::accept).both()
}) })
.unwrap_or(false) .unwrap_or(false);
{
if valid_cache {
// eprintln!("Cache: {:?}; constraint: {:?}", self.size_cache, constraint); // eprintln!("Cache: {:?}; constraint: {:?}", self.size_cache, constraint);
// The new constraint shouldn't change much, // The new constraint shouldn't change much,
@ -374,7 +375,10 @@ where
// Try again with some space for the scrollbar. // Try again with some space for the scrollbar.
let (inner_size, size, new_scrollable) = let (inner_size, size, new_scrollable) =
self.sizes_when_scrolling(constraint, scrollable, strict); self.sizes_when_scrolling(constraint, scrollable, strict);
if scrollable != new_scrollable { if scrollable == new_scrollable {
// Yup, scrolling did it. We're good to go now.
(inner_size, size)
} else {
// Again? We're now scrolling in a new direction? // Again? We're now scrolling in a new direction?
// There is no end to this! // There is no end to this!
let (inner_size, size, _) = self.sizes_when_scrolling( let (inner_size, size, _) = self.sizes_when_scrolling(
@ -386,9 +390,6 @@ where
// That's enough. If the inner view changed again, ignore it! // That's enough. If the inner view changed again, ignore it!
// That'll teach it. // That'll teach it.
(inner_size, size) (inner_size, size)
} else {
// Yup, scrolling did it. We're goot to go now.
(inner_size, size)
} }
} else { } else {
// We're not showing any scrollbar, either because we don't scroll // We're not showing any scrollbar, either because we don't scroll

View File

@ -680,10 +680,10 @@ impl<T: 'static> View for SelectView<T> {
// We'll draw the full list in a popup if needed. // We'll draw the full list in a popup if needed.
let style = if !(self.enabled && printer.enabled) { let style = if !(self.enabled && printer.enabled) {
ColorStyle::secondary() ColorStyle::secondary()
} else if !printer.focused { } else if printer.focused {
ColorStyle::primary()
} else {
ColorStyle::highlight() ColorStyle::highlight()
} else {
ColorStyle::primary()
}; };
let x = match printer.size.x.checked_sub(1) { let x = match printer.size.x.checked_sub(1) {
Some(x) => x, Some(x) => x,

View File

@ -222,6 +222,11 @@ impl StackView {
self.layers.len() self.layers.len()
} }
/// Returns `true` if there is no layer in this `StackView`.
pub fn is_empty(&self) -> bool {
self.layers.is_empty()
}
/// Returns `true` if `position` points to a valid layer. /// Returns `true` if `position` points to a valid layer.
/// ///
/// Returns `false` if it exceeds the bounds. /// Returns `false` if it exceeds the bounds.