From 898f4804b7b998ffeb1ff9f457b935e1364d6827 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 11 Jul 2022 23:08:48 +0200 Subject: [PATCH] Enable and fix a bunch more lints --- .cargo/config.toml | 16 ++++++++++++++++ eframe/src/epi.rs | 2 +- egui-winit/src/window_settings.rs | 4 ++-- egui/src/context.rs | 8 +++++--- egui/src/util/id_type_map.rs | 12 ++++++------ egui/src/widgets/drag_value.rs | 2 +- egui/src/widgets/plot/items/mod.rs | 5 ++--- egui/src/widgets/plot/legend.rs | 2 +- egui/src/widgets/plot/mod.rs | 2 +- egui/src/widgets/progress_bar.rs | 2 +- egui/src/widgets/spinner.rs | 2 +- egui_demo_app/src/lib.rs | 6 ++---- egui_demo_app/src/wrap_app.rs | 13 ++++++------- egui_demo_lib/src/demo/drag_and_drop.rs | 2 +- egui_demo_lib/src/demo/misc_demo_window.rs | 2 +- egui_demo_lib/src/demo/plot_demo.rs | 2 +- egui_glow/src/painter.rs | 8 ++++---- egui_glow/src/post_process.rs | 2 +- emath/src/numeric.rs | 10 ++++++++-- epaint/src/text/font.rs | 4 ++-- epaint/src/texture_atlas.rs | 7 ++----- 21 files changed, 65 insertions(+), 48 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index b663243b9ac..f34c83ac46b 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -10,6 +10,7 @@ rustflags = [ "-Wclippy::dbg_macro", "-Wclippy::debug_assert_with_mut_call", "-Wclippy::disallowed_methods", + "-Wclippy::disallowed_script_idents", "-Wclippy::doc_markdown", "-Wclippy::empty_enum", "-Wclippy::enum_glob_use", @@ -23,10 +24,12 @@ rustflags = [ "-Wclippy::flat_map_option", "-Wclippy::float_cmp_const", "-Wclippy::fn_params_excessive_bools", + "-Wclippy::fn_to_numeric_cast_any", "-Wclippy::from_iter_instead_of_collect", "-Wclippy::if_let_mutex", "-Wclippy::implicit_clone", "-Wclippy::imprecise_flops", + "-Wclippy::index_refutable_slice", "-Wclippy::inefficient_to_string", "-Wclippy::invalid_upcast_comparisons", "-Wclippy::iter_not_returning_iterator", @@ -56,6 +59,8 @@ rustflags = [ "-Wclippy::needless_continue", "-Wclippy::needless_for_each", "-Wclippy::needless_pass_by_value", + "-Wclippy::negative_feature_names", + "-Wclippy::nonstandard_macro_braces", "-Wclippy::option_option", "-Wclippy::path_buf_push_overwrite", "-Wclippy::ptr_as_ptr", @@ -65,13 +70,16 @@ rustflags = [ "-Wclippy::same_functions_in_if_condition", "-Wclippy::semicolon_if_nothing_returned", "-Wclippy::single_match_else", + "-Wclippy::str_to_string", "-Wclippy::string_add_assign", "-Wclippy::string_add", "-Wclippy::string_lit_as_bytes", "-Wclippy::string_to_string", "-Wclippy::todo", + "-Wclippy::trailing_empty_array", "-Wclippy::trait_duplication_in_bounds", "-Wclippy::unimplemented", + "-Wclippy::unnecessary_wraps", "-Wclippy::unnested_or_patterns", "-Wclippy::unused_self", "-Wclippy::useless_transmute", @@ -81,4 +89,12 @@ rustflags = [ "-Wnonstandard_style", "-Wrust_2018_idioms", "-Wrustdoc::missing_crate_level_docs", + "-Wsemicolon_in_expressions_from_macros", + "-Wtrivial_numeric_casts", + "-Wunused_extern_crates", + "-Wunused_import_braces", + # "-Wclippy::cloned_instead_of_copied", + # "-Wclippy::mod_module_files", + # "-Wtrivial_casts", + # "-Wunused_qualifications", ] diff --git a/eframe/src/epi.rs b/eframe/src/epi.rs index 2bbe82f8d15..b7e319b862e 100644 --- a/eframe/src/epi.rs +++ b/eframe/src/epi.rs @@ -374,7 +374,7 @@ impl Theme { // ---------------------------------------------------------------------------- -/// WebGl Context options +/// `WebGl` Context options #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] pub enum WebGlContextOption { diff --git a/egui-winit/src/window_settings.rs b/egui-winit/src/window_settings.rs index fd8afb94718..ece4d006dd7 100644 --- a/egui-winit/src/window_settings.rs +++ b/egui-winit/src/window_settings.rs @@ -30,8 +30,8 @@ impl WindowSettings { Self { position, inner_size_points: Some(egui::vec2( - inner_size_points.width as f32, - inner_size_points.height as f32, + inner_size_points.width, + inner_size_points.height, )), } } diff --git a/egui/src/context.rs b/egui/src/context.rs index dc58bb61eea..1c0b4e82428 100644 --- a/egui/src/context.rs +++ b/egui/src/context.rs @@ -51,7 +51,7 @@ struct ContextImpl { repaint_after: std::time::Duration, /// While positive, keep requesting repaints. Decrement at the end of each frame. repaint_requests: u32, - request_repaint_callbacks: Option>, + request_repaint_callback: Option>, requested_repaint_last_frame: bool, } @@ -570,7 +570,7 @@ impl Context { // request two frames of repaint, just to cover some corner cases (frame delays): let mut ctx = self.write(); ctx.repaint_requests = 2; - if let Some(callback) = &ctx.request_repaint_callbacks { + if let Some(callback) = &ctx.request_repaint_callback { (callback)(); } } @@ -611,9 +611,11 @@ impl Context { /// For integrations: this callback will be called when an egui user calls [`Self::request_repaint`]. /// /// This lets you wake up a sleeping UI thread. + /// + /// Note that only one callback can be set. Any new call overrides the previous callback. pub fn set_request_repaint_callback(&self, callback: impl Fn() + Send + Sync + 'static) { let callback = Box::new(callback); - self.write().request_repaint_callbacks = Some(callback); + self.write().request_repaint_callback = Some(callback); } /// Tell `egui` which fonts to use. diff --git a/egui/src/util/id_type_map.rs b/egui/src/util/id_type_map.rs index cc07a2afec1..fccbeaf0702 100644 --- a/egui/src/util/id_type_map.rs +++ b/egui/src/util/id_type_map.rs @@ -316,19 +316,19 @@ use crate::Id; /// /// // `b` associated with an f64 and a `&'static str` /// map.insert_persisted(b, 13.37); -/// map.insert_temp(b, "Hello World".to_string()); +/// map.insert_temp(b, "Hello World".to_owned()); /// /// // we can retrieve all four values: /// assert_eq!(map.get_temp::(a), Some(3.14)); /// assert_eq!(map.get_temp::(a), Some(42)); /// assert_eq!(map.get_temp::(b), Some(13.37)); -/// assert_eq!(map.get_temp::(b), Some("Hello World".to_string())); +/// assert_eq!(map.get_temp::(b), Some("Hello World".to_owned())); /// /// // we can retrieve them like so also: /// assert_eq!(map.get_persisted::(a), Some(3.14)); /// assert_eq!(map.get_persisted::(a), Some(42)); /// assert_eq!(map.get_persisted::(b), Some(13.37)); -/// assert_eq!(map.get_temp::(b), Some("Hello World".to_string())); +/// assert_eq!(map.get_temp::(b), Some("Hello World".to_owned())); /// ``` #[derive(Clone, Debug, Default)] // We store use `id XOR typeid` as a key, so we don't need to hash again! @@ -574,19 +574,19 @@ fn test_two_id_x_two_types() { // `b` associated with an f64 and a `&'static str` map.insert_persisted(b, 13.37); - map.insert_temp(b, "Hello World".to_string()); + map.insert_temp(b, "Hello World".to_owned()); // we can retrieve all four values: assert_eq!(map.get_temp::(a), Some(3.14)); assert_eq!(map.get_temp::(a), Some(42)); assert_eq!(map.get_temp::(b), Some(13.37)); - assert_eq!(map.get_temp::(b), Some("Hello World".to_string())); + assert_eq!(map.get_temp::(b), Some("Hello World".to_owned())); // we can retrieve them like so also: assert_eq!(map.get_persisted::(a), Some(3.14)); assert_eq!(map.get_persisted::(a), Some(42)); assert_eq!(map.get_persisted::(b), Some(13.37)); - assert_eq!(map.get_temp::(b), Some("Hello World".to_string())); + assert_eq!(map.get_temp::(b), Some("Hello World".to_owned())); } #[test] diff --git a/egui/src/widgets/drag_value.rs b/egui/src/widgets/drag_value.rs index 61a1f2b4861..657d2653414 100644 --- a/egui/src/widgets/drag_value.rs +++ b/egui/src/widgets/drag_value.rs @@ -249,7 +249,7 @@ impl<'a> Widget for DragValue<'a> { .then(|| drag_state.last_dragged_value) .flatten(); let stored_value = stored_value.unwrap_or(value); - let stored_value = stored_value + delta_value as f64; + let stored_value = stored_value + delta_value; let aim_delta = aim_rad * speed; let rounded_new_value = emath::smart_aim::best_in_range_f64( diff --git a/egui/src/widgets/plot/items/mod.rs b/egui/src/widgets/plot/items/mod.rs index a38bde7ce65..59fdaa1e24d 100644 --- a/egui/src/widgets/plot/items/mod.rs +++ b/egui/src/widgets/plot/items/mod.rs @@ -837,8 +837,7 @@ impl PlotItem for Points { stem_stroke.width *= 2.0; } - let y_reference = - stems.map(|y| transform.position_from_value(&Value::new(0.0, y)).y as f32); + let y_reference = stems.map(|y| transform.position_from_value(&Value::new(0.0, y)).y); series .values @@ -1619,7 +1618,7 @@ fn add_rulers_and_text( // Text let text = text.unwrap_or({ - let mut text = elem.name().to_string(); // could be empty + let mut text = elem.name().to_owned(); // could be empty if show_values { text.push_str(&elem.default_values_format(plot.transform)); diff --git a/egui/src/widgets/plot/legend.rs b/egui/src/widgets/plot/legend.rs index 41bd9d4f18d..c2beeac51bf 100644 --- a/egui/src/widgets/plot/legend.rs +++ b/egui/src/widgets/plot/legend.rs @@ -178,7 +178,7 @@ impl LegendWidget { .filter(|item| !item.name().is_empty()) .for_each(|item| { entries - .entry(item.name().to_string()) + .entry(item.name().to_owned()) .and_modify(|entry| { if entry.color != item.color() { // Multiple items with different colors diff --git a/egui/src/widgets/plot/mod.rs b/egui/src/widgets/plot/mod.rs index dc7c5e2a3d8..29da309acd3 100644 --- a/egui/src/widgets/plot/mod.rs +++ b/egui/src/widgets/plot/mod.rs @@ -370,7 +370,7 @@ impl Plot { /// if !name.is_empty() { /// format!("{}: {:.*}%", name, 1, value.y) /// } else { - /// "".to_string() + /// "".to_owned() /// } /// }) /// .show(ui, |plot_ui| plot_ui.line(line)); diff --git a/egui/src/widgets/progress_bar.rs b/egui/src/widgets/progress_bar.rs index 3c9aaa06183..417dafb0178 100644 --- a/egui/src/widgets/progress_bar.rs +++ b/egui/src/widgets/progress_bar.rs @@ -108,7 +108,7 @@ impl Widget for ProgressBar { if animate { let n_points = 20; - let start_angle = ui.input().time as f64 * 360f64.to_radians(); + let start_angle = ui.input().time * std::f64::consts::TAU; let end_angle = start_angle + 240f64.to_radians() * ui.input().time.sin(); let circle_radius = rounding - 2.0; let points: Vec = (0..n_points) diff --git a/egui/src/widgets/spinner.rs b/egui/src/widgets/spinner.rs index 1fbc19ca294..b2ce0d435d3 100644 --- a/egui/src/widgets/spinner.rs +++ b/egui/src/widgets/spinner.rs @@ -38,7 +38,7 @@ impl Widget for Spinner { let radius = (rect.height() / 2.0) - 2.0; let n_points = 20; - let start_angle = ui.input().time as f64 * 360f64.to_radians(); + let start_angle = ui.input().time * std::f64::consts::TAU; let end_angle = start_angle + 240f64.to_radians() * ui.input().time.sin(); let points: Vec = (0..n_points) .map(|i| { diff --git a/egui_demo_app/src/lib.rs b/egui_demo_app/src/lib.rs index a7d7ed9491c..8053baab11c 100644 --- a/egui_demo_app/src/lib.rs +++ b/egui_demo_app/src/lib.rs @@ -8,12 +8,10 @@ mod wrap_app; pub use wrap_app::WrapApp; /// Time of day as seconds since midnight. Used for clock in demo app. -pub(crate) fn seconds_since_midnight() -> Option { +pub(crate) fn seconds_since_midnight() -> f64 { use chrono::Timelike; let time = chrono::Local::now().time(); - let seconds_since_midnight = - time.num_seconds_from_midnight() as f64 + 1e-9 * (time.nanosecond() as f64); - Some(seconds_since_midnight) + time.num_seconds_from_midnight() as f64 + 1e-9 * (time.nanosecond() as f64) } // ---------------------------------------------------------------------------- diff --git a/egui_demo_app/src/wrap_app.rs b/egui_demo_app/src/wrap_app.rs index f1d021f5d50..5c522f41478 100644 --- a/egui_demo_app/src/wrap_app.rs +++ b/egui_demo_app/src/wrap_app.rs @@ -42,7 +42,8 @@ impl eframe::App for FractalClockApp { egui::CentralPanel::default() .frame(egui::Frame::dark_canvas(&ctx.style())) .show(ctx, |ui| { - self.fractal_clock.ui(ui, crate::seconds_since_midnight()); + self.fractal_clock + .ui(ui, Some(crate::seconds_since_midnight())); }); } } @@ -287,12 +288,10 @@ impl WrapApp { ui.with_layout(egui::Layout::right_to_left(), |ui| { if false { // TODO(emilk): fix the overlap on small screens - if let Some(seconds_since_midnight) = crate::seconds_since_midnight() { - if clock_button(ui, seconds_since_midnight).clicked() { - self.state.selected_anchor = "clock".to_owned(); - if frame.is_web() { - ui.output().open_url("#clock"); - } + if clock_button(ui, crate::seconds_since_midnight()).clicked() { + self.state.selected_anchor = "clock".to_owned(); + if frame.is_web() { + ui.output().open_url("#clock"); } } } diff --git a/egui_demo_lib/src/demo/drag_and_drop.rs b/egui_demo_lib/src/demo/drag_and_drop.rs index bc5fe4b47bf..c811ba0c821 100644 --- a/egui_demo_lib/src/demo/drag_and_drop.rs +++ b/egui_demo_lib/src/demo/drag_and_drop.rs @@ -147,7 +147,7 @@ impl super::View for DragAndDropDemo { let response = response.context_menu(|ui| { if ui.button("New Item").clicked() { - self.columns[col_idx].push("New Item".to_string()); + self.columns[col_idx].push("New Item".to_owned()); ui.close_menu(); } }); diff --git a/egui_demo_lib/src/demo/misc_demo_window.rs b/egui_demo_lib/src/demo/misc_demo_window.rs index b379239c20b..07ca8e1429b 100644 --- a/egui_demo_lib/src/demo/misc_demo_window.rs +++ b/egui_demo_lib/src/demo/misc_demo_window.rs @@ -647,7 +647,7 @@ fn text_layout_ui( ui.label("Overflow character"); }); - let mut job = LayoutJob::single_section(LOREM_IPSUM.to_string(), TextFormat::default()); + let mut job = LayoutJob::single_section(LOREM_IPSUM.to_owned(), TextFormat::default()); job.wrap = TextWrapping { max_rows: *max_rows, break_anywhere: *break_anywhere, diff --git a/egui_demo_lib/src/demo/plot_demo.rs b/egui_demo_lib/src/demo/plot_demo.rs index d23d6867045..9684c47dc93 100644 --- a/egui_demo_lib/src/demo/plot_demo.rs +++ b/egui_demo_lib/src/demo/plot_demo.rs @@ -621,7 +621,7 @@ impl InteractionDemo { let coordinate_text = if let Some(coordinate) = pointer_coordinate { format!("x: {:.02}, y: {:.02}", coordinate.x, coordinate.y) } else { - "None".to_string() + "None".to_owned() }; ui.label(format!("pointer coordinate: {}", coordinate_text)); let coordinate_text = format!( diff --git a/egui_glow/src/painter.rs b/egui_glow/src/painter.rs index 22d8559f175..cb83fe24120 100644 --- a/egui_glow/src/painter.rs +++ b/egui_glow/src/painter.rs @@ -684,10 +684,10 @@ pub fn clear(gl: &glow::Context, screen_size_in_pixels: [u32; 2], clear_color: e if true { // verified to be correct on eframe native (on Mac). gl.clear_color( - clear_color[0] as f32, - clear_color[1] as f32, - clear_color[2] as f32, - clear_color[3] as f32, + clear_color[0], + clear_color[1], + clear_color[2], + clear_color[3], ); } else { let clear_color: Color32 = clear_color.into(); diff --git a/egui_glow/src/post_process.rs b/egui_glow/src/post_process.rs index 5f525459b97..8d692eb4c26 100644 --- a/egui_glow/src/post_process.rs +++ b/egui_glow/src/post_process.rs @@ -141,7 +141,7 @@ impl PostProcess { let a_pos_loc = gl .get_attrib_location(program, "a_pos") - .ok_or_else(|| "failed to get location of a_pos".to_string())?; + .ok_or_else(|| "failed to get location of a_pos".to_owned())?; let vao = crate::vao::VertexArrayObject::new( &gl, pos_buffer, diff --git a/emath/src/numeric.rs b/emath/src/numeric.rs index a9f088d31bd..4689ecc19fb 100644 --- a/emath/src/numeric.rs +++ b/emath/src/numeric.rs @@ -23,12 +23,18 @@ macro_rules! impl_numeric_float { #[inline(always)] fn to_f64(self) -> f64 { - self as f64 + #[allow(trivial_numeric_casts)] + { + self as f64 + } } #[inline(always)] fn from_f64(num: f64) -> Self { - num as Self + #[allow(trivial_numeric_casts)] + { + num as Self + } } } }; diff --git a/epaint/src/text/font.rs b/epaint/src/text/font.rs index 87c4eb83f66..4cd02a38dff 100644 --- a/epaint/src/text/font.rs +++ b/epaint/src/text/font.rs @@ -217,7 +217,7 @@ type FontIndex = usize; pub struct Font { fonts: Vec>, /// Lazily calculated. - characters: Option>, + characters: Option>, replacement_glyph: (FontIndex, GlyphInfo), pixels_per_point: f32, row_height: f32, @@ -382,7 +382,7 @@ fn allocate_glyph( } }); - let offset_in_pixels = vec2(bb.min.x as f32, scale_in_pixels + bb.min.y as f32); + let offset_in_pixels = vec2(bb.min.x, scale_in_pixels + bb.min.y); let offset = offset_in_pixels / pixels_per_point + y_offset * Vec2::Y; UvRect { offset, diff --git a/epaint/src/texture_atlas.rs b/epaint/src/texture_atlas.rs index 73c9479e618..9e2983fecca 100644 --- a/epaint/src/texture_atlas.rs +++ b/epaint/src/texture_atlas.rs @@ -104,11 +104,8 @@ impl TextureAtlas { for dx in -hw..=hw { for dy in -hw..=hw { let distance_to_center = ((dx * dx + dy * dy) as f32).sqrt(); - let coverage = remap_clamp( - distance_to_center, - (r as f32 - 0.5)..=(r as f32 + 0.5), - 1.0..=0.0, - ); + let coverage = + remap_clamp(distance_to_center, (r - 0.5)..=(r + 0.5), 1.0..=0.0); image[((x as i32 + hw + dx) as usize, (y as i32 + hw + dy) as usize)] = coverage; }