From 87e3aacf35b24a0ace62962a4d2bd33df4272697 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Thu, 7 May 2020 10:47:03 +0200 Subject: [PATCH] enforce and fix a bunch of clippy issues --- build_glium.sh | 2 +- emigui/src/color.rs | 4 ++-- emigui/src/containers/collapsing_header.rs | 6 ++--- emigui/src/containers/floating.rs | 2 +- emigui/src/containers/resize.rs | 9 +++---- emigui/src/containers/scroll_area.rs | 2 +- emigui/src/context.rs | 6 ++--- emigui/src/font.rs | 2 +- emigui/src/fonts.rs | 2 +- emigui/src/id.rs | 12 ++++++---- emigui/src/input.rs | 6 ++--- emigui/src/layers.rs | 2 +- emigui/src/lib.rs | 28 +++++++++++++++++----- emigui/src/math.rs | 2 ++ emigui/src/memory.rs | 2 +- emigui/src/mesher.rs | 6 ++--- emigui/src/movement_tracker.rs | 2 +- emigui/src/region.rs | 24 ++++++++++--------- emigui/src/style.rs | 10 ++++---- emigui/src/texture_atlas.rs | 2 +- emigui/src/types.rs | 4 +++- emigui/src/widgets.rs | 12 +++++----- emigui/src/widgets/text_edit.rs | 7 ++---- emigui_glium/src/lib.rs | 2 +- example_glium/src/main.rs | 1 - 25 files changed, 90 insertions(+), 67 deletions(-) diff --git a/build_glium.sh b/build_glium.sh index b7a3eec2..9d8a008a 100755 --- a/build_glium.sh +++ b/build_glium.sh @@ -3,6 +3,6 @@ set -eu cargo fmt --all -- --check cargo check --all-features -cargo clippy +cargo clean -p emigui && cargo clippy cargo run --bin example_glium --release diff --git a/emigui/src/color.rs b/emigui/src/color.rs index 3ec537ae..4697d1ee 100644 --- a/emigui/src/color.rs +++ b/emigui/src/color.rs @@ -1,5 +1,5 @@ -/// 0-255 sRGBA -#[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd, Serialize)] +/// 0-255 `sRGBA`. TODO: rename `sRGBA` for clarity. +#[derive(Clone, Copy, Debug, Default, Eq, Ord, PartialEq, PartialOrd, serde_derive::Serialize)] pub struct Color { pub r: u8, pub g: u8, diff --git a/emigui/src/containers/collapsing_header.rs b/emigui/src/containers/collapsing_header.rs index 77b672e5..4520f3bf 100644 --- a/emigui/src/containers/collapsing_header.rs +++ b/emigui/src/containers/collapsing_header.rs @@ -1,6 +1,6 @@ use crate::{layout::Direction, *}; -#[derive(Clone, Copy, Debug, Deserialize, Serialize)] +#[derive(Clone, Copy, Debug, serde_derive::Deserialize, serde_derive::Serialize)] #[serde(default)] pub(crate) struct State { open: bool, @@ -136,8 +136,8 @@ impl CollapsingHeader { } fn paint_icon(region: &mut Region, state: &State, interact: &InteractInfo) { - let stroke_color = region.style().interact_stroke_color(&interact); - let stroke_width = region.style().interact_stroke_width(&interact); + let stroke_color = region.style().interact_stroke_color(interact); + let stroke_width = region.style().interact_stroke_width(interact); let (mut small_icon_rect, _) = region.style().icon_rectangles(interact.rect); small_icon_rect.set_center(pos2( diff --git a/emigui/src/containers/floating.rs b/emigui/src/containers/floating.rs index f6599184..b7dd7c5d 100644 --- a/emigui/src/containers/floating.rs +++ b/emigui/src/containers/floating.rs @@ -7,7 +7,7 @@ use std::{fmt::Debug, hash::Hash, sync::Arc}; use crate::*; -#[derive(Clone, Copy, Debug, Deserialize, Serialize)] +#[derive(Clone, Copy, Debug, serde_derive::Deserialize, serde_derive::Serialize)] pub(crate) struct State { /// Last known pos pub pos: Pos2, diff --git a/emigui/src/containers/resize.rs b/emigui/src/containers/resize.rs index c059b8bf..5d402de6 100644 --- a/emigui/src/containers/resize.rs +++ b/emigui/src/containers/resize.rs @@ -1,7 +1,8 @@ #![allow(unused_variables)] // TODO + use crate::*; -#[derive(Clone, Copy, Debug, Deserialize, Serialize)] +#[derive(Clone, Copy, Debug, serde_derive::Deserialize, serde_derive::Serialize)] pub(crate) struct State { size: Vec2, } @@ -142,7 +143,7 @@ impl Resize { self.max_size = self.max_size.max(self.min_size); let (is_new, mut state) = match region.memory().resize.get(&id) { - Some(state) => (false, state.clone()), + Some(state) => (false, *state), None => { let default_size = self.default_size.clamp(self.min_size..=self.max_size); (true, State { size: default_size }) @@ -234,8 +235,8 @@ impl Resize { } fn paint_resize_corner(region: &mut Region, interact: &InteractInfo) { - let color = region.style().interact_stroke_color(&interact); - let width = region.style().interact_stroke_width(&interact); + let color = region.style().interact_stroke_color(interact); + let width = region.style().interact_stroke_width(interact); let corner = interact.rect.right_bottom().round(); // TODO: round to pixels let mut w = 2.0; diff --git a/emigui/src/containers/scroll_area.rs b/emigui/src/containers/scroll_area.rs index 7c818452..cce4dd06 100644 --- a/emigui/src/containers/scroll_area.rs +++ b/emigui/src/containers/scroll_area.rs @@ -1,6 +1,6 @@ use crate::*; -#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)] +#[derive(Clone, Copy, Debug, Default, serde_derive::Deserialize, serde_derive::Serialize)] #[serde(default)] pub(crate) struct State { /// Positive offset means scrolling down/right diff --git a/emigui/src/context.rs b/emigui/src/context.rs index 5991ae0f..fad63cf4 100644 --- a/emigui/src/context.rs +++ b/emigui/src/context.rs @@ -59,15 +59,15 @@ impl Context { } } - pub fn memory(&self) -> parking_lot::MutexGuard { + pub fn memory(&self) -> parking_lot::MutexGuard<'_, Memory> { self.memory.lock() } - pub fn graphics(&self) -> parking_lot::MutexGuard { + pub fn graphics(&self) -> parking_lot::MutexGuard<'_, GraphicLayers> { self.graphics.lock() } - pub fn output(&self) -> parking_lot::MutexGuard { + pub fn output(&self) -> parking_lot::MutexGuard<'_, Output> { self.output.lock() } diff --git a/emigui/src/font.rs b/emigui/src/font.rs index e6188870..ac5ed690 100644 --- a/emigui/src/font.rs +++ b/emigui/src/font.rs @@ -288,7 +288,7 @@ impl Font { let mut cursor_y = 0.0; let mut text_fragments = Vec::new(); for line in text.split('\n') { - let mut line_fragments = self.layout_paragraph_max_width(&line, max_width_in_points); + let mut line_fragments = self.layout_paragraph_max_width(line, max_width_in_points); if let Some(last_word) = line_fragments.last() { let line_height = last_word.y_offset + line_spacing; for fragment in &mut line_fragments { diff --git a/emigui/src/fonts.rs b/emigui/src/fonts.rs index 02293b16..9b87f5b7 100644 --- a/emigui/src/fonts.rs +++ b/emigui/src/fonts.rs @@ -4,7 +4,7 @@ use std::{ sync::Arc, }; -use parking_lot::Mutex; +use {parking_lot::Mutex, serde_derive::Serialize}; use crate::{ font::Font, diff --git a/emigui/src/id.rs b/emigui/src/id.rs index aa6fc603..263251b0 100644 --- a/emigui/src/id.rs +++ b/emigui/src/id.rs @@ -1,11 +1,11 @@ //! Emigui tracks widgets frame-to-frame using `Id`s. //! //! For instance, if you start dragging a slider one frame, emigui stores -//! the sldiers Id as the current interact_id so that next frame when +//! the sldiers Id as the current `interact_id` so that next frame when //! you move the mouse the same slider changes, even if the mouse has //! moved outside the slider. //! -//! For some widgets `Id`s are also used to GUIpersist some state about the +//! For some widgets `Id`s are also used to persist some state about the //! widgets, such as Window position or wether not a collapsing header region is open. //! //! This implicated that the `Id`s must be unqiue. @@ -24,14 +24,16 @@ //! Then there are widgets that need no identifiers at all, like labels, //! because they have no state nor are interacted with. //! -//! So we have two type of Ids: PositionId and UniqueId. -//! TODO: have separate types for PositionId and UniqueId. +//! So we have two type of Ids: `PositionId` and `UniqueId`. +//! TODO: have separate types for `PositionId` and `UniqueId`. use std::{collections::hash_map::DefaultHasher, hash::Hash}; use crate::math::Pos2; -#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq, Deserialize, Serialize)] +#[derive( + Clone, Copy, Debug, Hash, Eq, PartialEq, serde_derive::Deserialize, serde_derive::Serialize, +)] pub struct Id(u64); impl Id { diff --git a/emigui/src/input.rs b/emigui/src/input.rs index e10e000c..68a2c419 100644 --- a/emigui/src/input.rs +++ b/emigui/src/input.rs @@ -2,7 +2,7 @@ use crate::math::*; /// What the integration gives to the gui. /// All coordinates in emigui is in point/logical coordinates. -#[derive(Clone, Debug, Default, Deserialize)] +#[derive(Clone, Debug, Default, serde_derive::Deserialize)] #[serde(default)] pub struct RawInput { /// Is the button currently down? @@ -84,7 +84,7 @@ pub struct GuiInput { pub events: Vec, } -#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Deserialize)] +#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, serde_derive::Deserialize)] #[serde(rename_all = "snake_case")] pub enum Event { Copy, @@ -97,7 +97,7 @@ pub enum Event { }, } -#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Deserialize)] +#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, serde_derive::Deserialize)] #[serde(rename_all = "snake_case")] pub enum Key { Alt, diff --git a/emigui/src/layers.rs b/emigui/src/layers.rs index 50cf8cbd..5cee0e36 100644 --- a/emigui/src/layers.rs +++ b/emigui/src/layers.rs @@ -13,7 +13,7 @@ pub enum Layer { Debug, } -/// Each PaintCmd is paired with a clip rectangle. +/// Each `PaintCmd` is paired with a clip rectangle. type PaintList = Vec<(Rect, PaintCmd)>; /// TODO: improve this diff --git a/emigui/src/lib.rs b/emigui/src/lib.rs index 1e6288f3..71a71b97 100644 --- a/emigui/src/lib.rs +++ b/emigui/src/lib.rs @@ -1,10 +1,26 @@ #![deny(warnings)] - -extern crate rusttype; -extern crate serde; - -#[macro_use] // TODO: get rid of this -extern crate serde_derive; +#![warn( + clippy::all, + clippy::dbg_macro, + clippy::doc_markdown, + clippy::empty_enum, + clippy::enum_glob_use, + clippy::filter_map_next, + clippy::fn_params_excessive_bools, + clippy::imprecise_flops, + clippy::lossy_float_literal, + clippy::mem_forget, + clippy::needless_borrow, + clippy::needless_continue, + clippy::pub_enum_variant_names, + clippy::rest_pat_in_fully_bound_structs, + // clippy::suboptimal_flops, // TODO + clippy::todo, + // clippy::use_self, + future_incompatible, + nonstandard_style, + rust_2018_idioms, +)] pub mod color; pub mod containers; diff --git a/emigui/src/math.rs b/emigui/src/math.rs index 9ff3cb5f..cf120b20 100644 --- a/emigui/src/math.rs +++ b/emigui/src/math.rs @@ -1,5 +1,7 @@ use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, RangeInclusive, Sub, SubAssign}; +use serde_derive::{Deserialize, Serialize}; + #[derive(Clone, Copy, Default, Deserialize, Serialize)] pub struct Vec2 { pub x: f32, diff --git a/emigui/src/memory.rs b/emigui/src/memory.rs index 8debf58d..55bd98a3 100644 --- a/emigui/src/memory.rs +++ b/emigui/src/memory.rs @@ -5,7 +5,7 @@ use crate::{ Id, Layer, Pos2, Rect, }; -#[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[derive(Clone, Debug, Default, serde_derive::Deserialize, serde_derive::Serialize)] #[serde(default)] pub struct Memory { /// The widget being interacted with (e.g. dragged, in case of a slider). diff --git a/emigui/src/mesher.rs b/emigui/src/mesher.rs index ef7794b7..51e71d4e 100644 --- a/emigui/src/mesher.rs +++ b/emigui/src/mesher.rs @@ -11,7 +11,7 @@ use crate::{ const WHITE_UV: (u16, u16) = (1, 1); -#[derive(Clone, Copy, Debug, Default, Serialize)] +#[derive(Clone, Copy, Debug, Default, serde_derive::Serialize)] pub struct Vertex { /// Pixel coordinates pub pos: Pos2, @@ -21,7 +21,7 @@ pub struct Vertex { pub color: Color, } -#[derive(Clone, Debug, Default, Serialize)] +#[derive(Clone, Debug, Default, serde_derive::Serialize)] pub struct Mesh { /// Draw as triangles (i.e. the length is a multiple of three) pub indices: Vec, @@ -238,7 +238,7 @@ pub enum PathType { Open, Closed, } -use self::PathType::*; +use self::PathType::{Closed, Open}; pub struct MesherOptions { pub anti_alias: bool, diff --git a/emigui/src/movement_tracker.rs b/emigui/src/movement_tracker.rs index cb450bfa..7cf480c9 100644 --- a/emigui/src/movement_tracker.rs +++ b/emigui/src/movement_tracker.rs @@ -50,7 +50,7 @@ where self.flush(now); } - /// Mean time difference between values in this MovementTracker. + /// Mean time difference between values in this `MovementTracker`. pub fn mean_time_interval(&self) -> Option { if let (Some(first), Some(last)) = (self.values.front(), self.values.back()) { let n = self.len(); diff --git a/emigui/src/region.rs b/emigui/src/region.rs index 580b482e..792e88d8 100644 --- a/emigui/src/region.rs +++ b/emigui/src/region.rs @@ -4,7 +4,7 @@ use crate::{color::*, containers::*, font::TextFragment, layout::*, widgets::*, /// Represents a region of the screen /// with a type of layout (horizontal or vertical). -/// TODO: make Region a trait so we can have type-safe HorizontalRegion etc? +/// TODO: make Region a trait so we can have type-safe `HorizontalRegion` etc? pub struct Region { /// How we access input, output and memory ctx: Arc, @@ -122,11 +122,11 @@ impl Region { self.ctx.input() } - pub fn memory(&self) -> parking_lot::MutexGuard { + pub fn memory(&self) -> parking_lot::MutexGuard<'_, Memory> { self.ctx.memory() } - pub fn output(&self) -> parking_lot::MutexGuard { + pub fn output(&self) -> parking_lot::MutexGuard<'_, Output> { self.ctx.output() } @@ -251,7 +251,7 @@ impl Region { /// Will warn if the returned id is not guaranteed unique. /// Use this to generate widget ids for widgets that have persistent state in Memory. - /// If the id_source is not unique within this region + /// If the `id_source` is not unique within this region /// then an error will be printed at the current cursor position. pub fn make_unique_id(&self, id_source: &IdSource) -> Id where @@ -304,7 +304,7 @@ impl Region { /// # How sizes are negotiated /// Each widget should have a *minimum desired size* and a *desired size*. /// When asking for space, ask AT LEAST for you minimum, and don't ask for more than you need. - /// If you want to fill the space, ask about available_space() and use that. + /// If you want to fill the space, ask about `available_space()` and use that. /// NOTE: we always get the size we ask for (at the moment). pub fn reserve_space(&mut self, child_size: Vec2, interaction_id: Option) -> InteractInfo { let child_size = self.round_vec_to_pixels(child_size); @@ -431,7 +431,7 @@ impl Region { /// Show some text anywhere in the region. /// To center the text at the given position, use `align: (Center, Center)`. /// If you want to draw text floating on top of everything, - /// consider using Context.floating_text instead. + /// consider using `Context.floating_text` instead. pub fn floating_text( &mut self, pos: Pos2, @@ -584,10 +584,10 @@ impl Region { &mut self, dir: Direction, align: Align, - add_contents: impl FnOnce(&mut Region), + add_contents: impl FnOnce(&mut Self), ) { let child_rect = Rect::from_min_max(self.cursor, self.bottom_right()); - let mut child_region = Region { + let mut child_region = Self { dir, align, ..self.child_region(child_rect) @@ -599,26 +599,28 @@ impl Region { /// Temporarily split split a vertical layout into several columns. /// + /// ``` ignore /// region.columns(2, |columns| { /// columns[0].add(emigui::widgets::label!("First column")); /// columns[1].add(emigui::widgets::label!("Second column")); /// }); + /// ``` pub fn columns(&mut self, num_columns: usize, add_contents: F) -> R where - F: FnOnce(&mut [Region]) -> R, + F: FnOnce(&mut [Self]) -> R, { // TODO: ensure there is space let spacing = self.style.item_spacing.x; let total_spacing = spacing * (num_columns as f32 - 1.0); let column_width = (self.available_width() - total_spacing) / (num_columns as f32); - let mut columns: Vec = (0..num_columns) + let mut columns: Vec = (0..num_columns) .map(|col_idx| { let pos = self.cursor + vec2((col_idx as f32) * (column_width + spacing), 0.0); let child_rect = Rect::from_min_max(pos, pos2(pos.x + column_width, self.bottom_right().y)); - Region { + Self { id: self.make_child_id(&("column", col_idx)), dir: Direction::Vertical, ..self.child_region(child_rect) diff --git a/emigui/src/style.rs b/emigui/src/style.rs index b887ade5..e7e1f969 100644 --- a/emigui/src/style.rs +++ b/emigui/src/style.rs @@ -1,8 +1,10 @@ #![allow(clippy::if_same_then_else)] +use serde_derive::{Deserialize, Serialize}; + use crate::{color::*, math::*, types::*}; -#[derive(Clone, Copy, Debug, Serialize)] +#[derive(Clone, Copy, Debug, Deserialize, Serialize)] pub struct Style { /// Horizontal and vertical padding within a window frame. pub window_padding: Vec2, @@ -45,14 +47,14 @@ pub struct Style { pub debug_regions: bool, } -#[derive(Clone, Copy, Debug, Serialize)] +#[derive(Clone, Copy, Debug, Deserialize, Serialize)] pub struct Window { pub corner_radius: f32, } impl Default for Style { fn default() -> Self { - Style { + Self { window_padding: vec2(6.0, 6.0), button_padding: vec2(5.0, 3.0), item_spacing: vec2(8.0, 4.0), @@ -72,7 +74,7 @@ impl Default for Style { impl Default for Window { fn default() -> Self { - Window { + Self { corner_radius: 10.0, } } diff --git a/emigui/src/texture_atlas.rs b/emigui/src/texture_atlas.rs index f4f2a2e2..c4733ba7 100644 --- a/emigui/src/texture_atlas.rs +++ b/emigui/src/texture_atlas.rs @@ -37,7 +37,7 @@ pub struct TextureAtlas { impl TextureAtlas { pub fn new(width: usize, height: usize) -> Self { - TextureAtlas { + Self { texture: Texture { id: 0, width, diff --git a/emigui/src/types.rs b/emigui/src/types.rs index 94e88732..e9c0a5b2 100644 --- a/emigui/src/types.rs +++ b/emigui/src/types.rs @@ -1,3 +1,5 @@ +use serde_derive::Serialize; + use crate::{ color::Color, fonts::TextStyle, @@ -30,7 +32,7 @@ pub enum CursorIcon { impl Default for CursorIcon { fn default() -> Self { - CursorIcon::Default + Self::Default } } diff --git a/emigui/src/widgets.rs b/emigui/src/widgets.rs index ee5d5cda..f8769de3 100644 --- a/emigui/src/widgets.rs +++ b/emigui/src/widgets.rs @@ -12,7 +12,7 @@ pub use {slider::*, text_edit::*}; // ---------------------------------------------------------------------------- -/// Anything implementing Widget can be added to a Region with Region::add +/// Anything implementing Widget can be added to a Region with `Region::add` pub trait Widget { fn ui(self, region: &mut Region) -> GuiResponse; } @@ -28,7 +28,7 @@ pub struct Label { impl Label { pub fn new(text: impl Into) -> Self { - Label { + Self { text: text.into(), multiline: true, text_style: TextStyle::Body, @@ -138,7 +138,7 @@ pub struct Button { impl Button { pub fn new(text: impl Into) -> Self { - Button { + Self { text: text.into(), text_color: None, } @@ -257,7 +257,7 @@ pub struct RadioButton { impl RadioButton { pub fn new(checked: bool, text: impl Into) -> Self { - RadioButton { + Self { checked, text: text.into(), text_color: None, @@ -328,8 +328,8 @@ pub struct Separator { } impl Separator { - pub fn new() -> Separator { - Separator { + pub fn new() -> Self { + Self { line_width: 2.0, min_length: 6.0, extra: 0.0, diff --git a/emigui/src/widgets/text_edit.rs b/emigui/src/widgets/text_edit.rs index 6d3604d4..11818cea 100644 --- a/emigui/src/widgets/text_edit.rs +++ b/emigui/src/widgets/text_edit.rs @@ -67,11 +67,8 @@ impl<'t> Widget for TextEdit<'t> { } } Event::Key { key, pressed: true } => { - match key { - Key::Backspace => { - self.text.pop(); // TODO: unicode aware - } - _ => {} + if *key == Key::Backspace { + self.text.pop(); // TODO: unicode aware } } _ => {} diff --git a/emigui_glium/src/lib.rs b/emigui_glium/src/lib.rs index e825f131..dcfd94c5 100644 --- a/emigui_glium/src/lib.rs +++ b/emigui_glium/src/lib.rs @@ -1,5 +1,5 @@ #![deny(warnings)] - +#![allow(clippy::single_match)] mod painter; pub use painter::Painter; diff --git a/example_glium/src/main.rs b/example_glium/src/main.rs index 09459d35..04bc691a 100644 --- a/example_glium/src/main.rs +++ b/example_glium/src/main.rs @@ -1,5 +1,4 @@ #![deny(warnings)] -#[allow(clippy::single_match)] use std::time::{Duration, Instant}; use {