Fix clippy warnings

This commit is contained in:
Alexandre Bury 2018-05-17 17:35:57 -07:00
parent 80f66262be
commit 755854963f
17 changed files with 57 additions and 92 deletions

View File

@ -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.

View File

@ -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;

View File

@ -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,
}
}

View File

@ -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;
}

View File

@ -82,7 +82,7 @@ where
debug_assert!(current_width <= available_width + delimiter_width);
Span {
length: length,
length,
width: current_width,
}
}

View File

@ -19,8 +19,8 @@ impl<R: Read> ProgressReader<R> {
/// many bytes should be received.
pub fn new(counter: Counter, reader: R) -> Self {
ProgressReader {
reader: reader,
counter: counter,
reader,
counter,
}
}

View File

@ -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)))
}
}

View File

@ -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,
}

View File

@ -58,10 +58,7 @@ impl<T: View + 'static> ViewWrapper for IdView<T> {
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<Self::V, Self>
@ -80,15 +77,14 @@ impl<T: View + 'static> ViewWrapper for IdView<T> {
// 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);
}
}
}
}

View File

@ -14,7 +14,7 @@ pub struct Layer<T: View> {
impl<T: View> Layer<T> {
/// Wraps the given view.
pub fn new(view: T) -> Self {
Layer { view: view }
Layer { view }
}
inner_getters!(self.view: T);

View File

@ -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;
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 {

View File

@ -784,7 +784,7 @@ impl<T: 'static> View for SelectView<T> {
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<T> {
impl<T> Item<T> {
fn new(label: String, value: T) -> Self {
Item {
label: label,
label,
value: Rc::new(value),
}
}

View File

@ -17,7 +17,7 @@ impl<T: View> ShadowView<T> {
/// Wraps the given view.
pub fn new(view: T) -> Self {
ShadowView {
view: view,
view,
top_padding: true,
left_padding: true,
}

View File

@ -15,7 +15,7 @@ impl<T> SizedView<T> {
/// Wraps the given view.
pub fn new(view: T) -> Self {
SizedView {
view: view,
view,
size: Vec2::zero(),
}
}

View File

@ -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 {

View File

@ -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<Row> {
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,
}

View File

@ -21,7 +21,7 @@ impl<T: View> TrackedView<T> {
/// Creates a new `TrackedView` around `view`.
pub fn new(view: T) -> Self {
TrackedView {
view: view,
view,
offset: Cell::new(Vec2::zero()),
}
}