From 6c501c66401d490a39fa27202a849eecebcd7428 Mon Sep 17 00:00:00 2001 From: Edgar Geier Date: Tue, 28 Feb 2023 19:42:53 +0100 Subject: [PATCH] Fix clippy warnings --- examples/editor-libcosmic/src/main.rs | 8 +++--- examples/editor-libcosmic/src/text.rs | 4 +-- examples/editor-libcosmic/src/text_box.rs | 26 ++++++++--------- examples/editor-orbclient/src/main.rs | 6 ++-- examples/editor-test/src/main.rs | 5 ++-- examples/terminal/src/main.rs | 34 ++++++++++++++--------- src/buffer.rs | 16 ++++++++--- src/edit/editor.rs | 23 +++++++++------ src/shape.rs | 17 +++++++----- 9 files changed, 80 insertions(+), 59 deletions(-) diff --git a/examples/editor-libcosmic/src/main.rs b/examples/editor-libcosmic/src/main.rs index 911e1ab..6855a78 100644 --- a/examples/editor-libcosmic/src/main.rs +++ b/examples/editor-libcosmic/src/main.rs @@ -74,7 +74,7 @@ impl fmt::Display for FontSize { } } -static WRAP_MODE: &'static [Wrap] = &[Wrap::None, Wrap::Glyph, Wrap::Word]; +static WRAP_MODE: &[Wrap] = &[Wrap::None, Wrap::Glyph, Wrap::Word]; fn main() -> cosmic::iced::Result { env_logger::init(); @@ -274,7 +274,7 @@ impl Application for Window { } fn view(&self) -> Element { - static THEMES: &'static [&'static str] = &["Dark", "Light"]; + static THEMES: &[&str] = &["Dark", "Light"]; let theme_picker = pick_list( THEMES, Some(match self.theme { @@ -375,11 +375,11 @@ fn update_alignment<'a, T: Edit<'a>>(editor: &mut T, align: Align) { std::cmp::Ordering::Less => (select.line, current_line), std::cmp::Ordering::Equal => (current_line, current_line), }; - editor.buffer_mut().lines.get_mut(start..=end).map(|lines| { + if let Some(lines) = editor.buffer_mut().lines.get_mut(start..=end) { for line in lines.iter_mut() { line.set_align(Some(align)); } - }); + } } else if let Some(line) = editor.buffer_mut().lines.get_mut(current_line) { line.set_align(Some(align)); } diff --git a/examples/editor-libcosmic/src/text.rs b/examples/editor-libcosmic/src/text.rs index 0392842..c543759 100644 --- a/examples/editor-libcosmic/src/text.rs +++ b/examples/editor-libcosmic/src/text.rs @@ -227,7 +227,7 @@ pub fn draw_pixel( let mut current = buffer[offset + 2] as u32 | (buffer[offset + 1] as u32) << 8 - | (buffer[offset + 0] as u32) << 16 + | (buffer[offset] as u32) << 16 | (buffer[offset + 3] as u32) << 24; if alpha >= 255 || current == 0 { @@ -244,7 +244,7 @@ pub fn draw_pixel( buffer[offset + 2] = current as u8; buffer[offset + 1] = (current >> 8) as u8; - buffer[offset + 0] = (current >> 16) as u8; + buffer[offset] = (current >> 16) as u8; buffer[offset + 3] = (current >> 24) as u8; } diff --git a/examples/editor-libcosmic/src/text_box.rs b/examples/editor-libcosmic/src/text_box.rs index d10081e..bbb3004 100644 --- a/examples/editor-libcosmic/src/text_box.rs +++ b/examples/editor-libcosmic/src/text_box.rs @@ -62,7 +62,7 @@ impl<'a, Editor> TextBox<'a, Editor> { } } -pub fn text_box<'a, Editor>(editor: &'a Mutex) -> TextBox<'a, Editor> { +pub fn text_box(editor: &Mutex) -> TextBox { TextBox::new(editor) } @@ -174,7 +174,7 @@ where &mut state.cache.lock().unwrap(), text_color, |x, y, w, h, color| { - if w <= 0 || h <= 0 { + if w == 0 || h == 0 { // Do not draw invalid sized rectangles return; } @@ -235,10 +235,7 @@ where let mut status = Status::Ignored; match event { - Event::Keyboard(KeyEvent::KeyPressed { - key_code, - modifiers, - }) => match key_code { + Event::Keyboard(KeyEvent::KeyPressed { key_code, .. }) => match key_code { KeyCode::Left => { editor.action(Action::Left); status = Status::Captured; @@ -318,15 +315,14 @@ where status = Status::Captured; } } - Event::Mouse(MouseEvent::WheelScrolled { delta }) => match delta { - ScrollDelta::Lines { x, y } => { - editor.action(Action::Scroll { - lines: (-y * 6.0) as i32, - }); - status = Status::Captured; - } - _ => (), - }, + Event::Mouse(MouseEvent::WheelScrolled { + delta: ScrollDelta::Lines { y, .. }, + }) => { + editor.action(Action::Scroll { + lines: (-y * 6.0) as i32, + }); + status = Status::Captured; + } _ => (), } diff --git a/examples/editor-orbclient/src/main.rs b/examples/editor-orbclient/src/main.rs index d203d97..719d942 100644 --- a/examples/editor-orbclient/src/main.rs +++ b/examples/editor-orbclient/src/main.rs @@ -14,7 +14,7 @@ fn main() { env_logger::init(); let path = if let Some(arg) = env::args().nth(1) { - arg.clone() + arg } else { String::new() }; @@ -35,7 +35,7 @@ fn main() { -1, 1024 * display_scale as u32, 768 * display_scale as u32, - &format!("COSMIC Text - {}", path), + &format!("COSMIC Text - {path}"), &[WindowFlag::Resizable], ) .unwrap(); @@ -110,7 +110,7 @@ fn main() { let mut end_line = 0; for run in editor.buffer().layout_runs() { end_line = run.line_i; - if start_line_opt == None { + if start_line_opt.is_none() { start_line_opt = Some(end_line); } } diff --git a/examples/editor-test/src/main.rs b/examples/editor-test/src/main.rs index 2144240..12cc6ab 100644 --- a/examples/editor-test/src/main.rs +++ b/examples/editor-test/src/main.rs @@ -131,9 +131,8 @@ fn main() { redraw(&mut window, &mut editor, &mut swash_cache); for event in window.events() { - match event.to_option() { - EventOption::Quit(_) => process::exit(1), - _ => (), + if let EventOption::Quit(_) = event.to_option() { + process::exit(1) } } } diff --git a/examples/terminal/src/main.rs b/examples/terminal/src/main.rs index bf6a610..a67318e 100644 --- a/examples/terminal/src/main.rs +++ b/examples/terminal/src/main.rs @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 use cosmic_text::{Attrs, Buffer, Color, FontSystem, Metrics, SwashCache}; -use std::cmp; +use std::cmp::{self, Ordering}; use termion::{color, cursor}; fn main() { @@ -66,21 +66,29 @@ fn main() { let scale = |c: u8| cmp::max(0, cmp::min(255, ((c as i32) * (a as i32)) / 255)) as u8; // Navigate to x coordinate - if x > last_x { - print!("{}", cursor::Right((x - last_x) as u16)); - last_x = x; - } else if x < last_x { - print!("{}", cursor::Left((last_x - x) as u16)); - last_x = x; + match x.cmp(&last_x) { + Ordering::Greater => { + print!("{}", cursor::Right((x - last_x) as u16)); + last_x = x; + } + Ordering::Less => { + print!("{}", cursor::Left((last_x - x) as u16)); + last_x = x; + } + Ordering::Equal => {} } // Navigate to y coordinate - if y > last_y { - print!("{}", cursor::Down((y - last_y) as u16)); - last_y = y; - } else if y < last_y { - print!("{}", cursor::Up((last_y - y) as u16)); - last_y = y; + match y.cmp(&last_y) { + Ordering::Greater => { + print!("{}", cursor::Down((y - last_y) as u16)); + last_y = y; + } + Ordering::Less => { + print!("{}", cursor::Up((last_y - y) as u16)); + last_y = y; + } + Ordering::Equal => {} } // Print a space with the expected color as the background diff --git a/src/buffer.rs b/src/buffer.rs index ac1729b..41acf49 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -113,9 +113,9 @@ pub struct LayoutRun<'a> { } impl<'a> LayoutRun<'a> { - /// Return the pixel span Some((x_left, x_width)) of the highlighted area between cursor_start - /// and cursor_end within this run, or None if the cursor range does not intersect this run. - /// This may return widths of zero if cursor_start == cursor_end, if the run is empty, or if the + /// Return the pixel span `Some((x_left, x_width))` of the highlighted area between `cursor_start` + /// and `cursor_end` within this run, or None if the cursor range does not intersect this run. + /// This may return widths of zero if `cursor_start == cursor_end`, if the run is empty, or if the /// region's left start boundary is the same as the cursor's end boundary or vice versa. pub fn highlight(&self, cursor_start: Cursor, cursor_end: Cursor) -> Option<(f32, f32)> { let mut x_start = None; @@ -208,7 +208,7 @@ impl<'a, 'b> LayoutRunIter<'a, 'b> { buffer, line_i: 0, layout_i: 0, - remaining_len: bottom_cropped_layout_lines as usize, + remaining_len: bottom_cropped_layout_lines, line_y: buffer.metrics.y_offset(), total_layout: 0, } @@ -311,6 +311,10 @@ pub struct Buffer<'a> { impl<'a> Buffer<'a> { /// Create a new [`Buffer`] with the provided [`FontSystem`] and [`Metrics`] + /// + /// # Panics + /// + /// Will panic if `metrics.line_height` is zero. pub fn new(font_system: &'a FontSystem, metrics: Metrics) -> Self { assert_ne!(metrics.line_height, 0.0, "line height cannot be 0"); @@ -497,6 +501,10 @@ impl<'a> Buffer<'a> { } /// Set the current [`Metrics`] + /// + /// # Panics + /// + /// Will panic if `metrics.font_size` is zero. pub fn set_metrics(&mut self, metrics: Metrics) { if metrics != self.metrics { assert_ne!(metrics.font_size, 0.0, "font size cannot be 0"); diff --git a/src/edit/editor.rs b/src/edit/editor.rs index 189aefb..381ab5f 100644 --- a/src/edit/editor.rs +++ b/src/edit/editor.rs @@ -2,7 +2,10 @@ #[cfg(not(feature = "std"))] use alloc::string::String; -use core::{cmp, iter::once}; +use core::{ + cmp::{self, Ordering}, + iter::once, +}; use unicode_segmentation::UnicodeSegmentation; #[cfg(feature = "swash")] @@ -432,14 +435,18 @@ impl<'a> Edit<'a> for Editor<'a> { Action::Vertical(px) => { // TODO more efficient let lines = px / self.buffer.metrics().line_height as i32; - if lines < 0 { - for _ in 0..-lines { - self.action(Action::Up); + match lines.cmp(&0) { + Ordering::Less => { + for _ in 0..-lines { + self.action(Action::Up); + } } - } else if lines > 0 { - for _ in 0..lines { - self.action(Action::Down); + Ordering::Greater => { + for _ in 0..lines { + self.action(Action::Down); + } } + Ordering::Equal => {} } } Action::Escape => { @@ -583,7 +590,7 @@ impl<'a> Edit<'a> for Editor<'a> { } else if self.cursor.line > 0 { self.cursor.line -= 1; self.cursor.index = self.buffer.lines[self.cursor.line].text().len(); - self.buffer.set_redraw(true) + self.buffer.set_redraw(true); } self.cursor_x_opt = None; } diff --git a/src/shape.rs b/src/shape.rs index 6f43ab0..4136d97 100644 --- a/src/shape.rs +++ b/src/shape.rs @@ -98,8 +98,8 @@ fn shape_fallback( (glyphs, missing) } -fn shape_run<'a>( - font_system: &'a FontSystem, +fn shape_run( + font_system: &FontSystem, line: &str, attrs_list: &AttrsList, start_run: usize, @@ -278,8 +278,8 @@ pub struct ShapeWord { } impl ShapeWord { - pub fn new<'a>( - font_system: &'a FontSystem, + pub fn new( + font_system: &FontSystem, line: &str, attrs_list: &AttrsList, word_range: Range, @@ -352,8 +352,8 @@ pub struct ShapeSpan { } impl ShapeSpan { - pub fn new<'a>( - font_system: &'a FontSystem, + pub fn new( + font_system: &FontSystem, line: &str, attrs_list: &AttrsList, span_range: Range, @@ -434,7 +434,10 @@ pub struct ShapeLine { type VlRange = (usize, (usize, usize), (usize, usize)); impl ShapeLine { - pub fn new<'a>(font_system: &'a FontSystem, line: &str, attrs_list: &AttrsList) -> Self { + /// # Panics + /// + /// Will panic if `line` contains more than one paragraph. + pub fn new(font_system: &FontSystem, line: &str, attrs_list: &AttrsList) -> Self { let mut spans = Vec::new(); let bidi = unicode_bidi::BidiInfo::new(line, None);