From acccc4227fbf06fb31ce58c7f98f9ba9068876bc Mon Sep 17 00:00:00 2001 From: Kevin Reid Date: Sun, 16 Jun 2024 15:27:30 -0700 Subject: [PATCH] Enable `clippy::cast_possible_wrap`. I'd like to enable the other cast lints, but they are even more common and have no clean substitute since they lint int-to-float conversions. Further work for someday. --- Cargo.toml | 4 ++-- all-is-cubes-base/src/math/vol.rs | 11 ++--------- all-is-cubes-content/src/lib.rs | 1 + all-is-cubes-gpu/src/in_wgpu.rs | 1 + all-is-cubes-gpu/src/in_wgpu/light_texture.rs | 1 + .../src/in_wgpu/raytrace_to_texture.rs | 1 + all-is-cubes-port/src/mv.rs | 14 +++++++------- all-is-cubes-ui/src/vui/widgets/text.rs | 4 ++-- all-is-cubes-wasm/Cargo.toml | 4 ++-- all-is-cubes/src/character/cursor.rs | 4 ++-- all-is-cubes/src/content/load_image.rs | 7 ++++--- all-is-cubes/src/drawing.rs | 1 + all-is-cubes/src/universe.rs | 1 + test-renderers/src/test_cases.rs | 1 + 14 files changed, 28 insertions(+), 27 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 619423149..a06b6df0a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -159,8 +159,7 @@ clippy.needless_update = "allow" clippy.single_match = "allow" # clippy::pedantic lints that are set to allow -clippy.cast_possible_truncation = "allow" # consider enabling -clippy.cast_possible_wrap = "allow" # consider enabling +clippy.cast_possible_truncation = "allow" # we would need an alternative for intentional from-float saturation and to-float precision loss clippy.cast_precision_loss = "allow" # consider enabling clippy.cast_sign_loss = "allow" # consider enabling clippy.default_trait_access = "allow" @@ -201,6 +200,7 @@ clippy.should_panic_without_expect = "deny" clippy.pedantic = { level = "warn", priority = -1 } clippy.assigning_clones = "warn" clippy.cast_lossless = "warn" +clippy.cast_possible_wrap = "warn" clippy.doc_markdown = "warn" clippy.exhaustive_enums = "warn" clippy.exhaustive_structs = "warn" diff --git a/all-is-cubes-base/src/math/vol.rs b/all-is-cubes-base/src/math/vol.rs index e962bca82..28cf47b40 100644 --- a/all-is-cubes-base/src/math/vol.rs +++ b/all-is-cubes-base/src/math/vol.rs @@ -6,7 +6,7 @@ use core::ops::{Deref, DerefMut}; #[cfg(doc)] use alloc::vec::Vec; -use euclid::Point3D; +use euclid::{vec3, Point3D}; use manyfmt::Refmt as _; use crate::math::{Axis, Cube, GridAab, GridCoordinate, GridIter, GridPoint, GridVector}; @@ -207,14 +207,7 @@ where V: Clone, { Self::from_fn( - GridAab::from_lower_size( - [0, 0, 0], - [ - DX as GridCoordinate, - DY as GridCoordinate, - DZ as GridCoordinate, - ], - ), + GridAab::from_lower_size([0, 0, 0], vec3(DX, DY, DZ).to_i32()), |p| array[p.z as usize][(DY - 1) - (p.y as usize)][p.x as usize].clone(), ) } diff --git a/all-is-cubes-content/src/lib.rs b/all-is-cubes-content/src/lib.rs index 8ee8cdcd5..2b02e4897 100644 --- a/all-is-cubes-content/src/lib.rs +++ b/all-is-cubes-content/src/lib.rs @@ -10,6 +10,7 @@ #![no_std] // // Crate-specific lint settings. (General settings can be found in the workspace manifest.) +#![allow(clippy::cast_possible_wrap)] #![cfg_attr( not(test), warn(clippy::std_instead_of_core, clippy::std_instead_of_alloc) diff --git a/all-is-cubes-gpu/src/in_wgpu.rs b/all-is-cubes-gpu/src/in_wgpu.rs index 8c2684aec..c41ee396b 100644 --- a/all-is-cubes-gpu/src/in_wgpu.rs +++ b/all-is-cubes-gpu/src/in_wgpu.rs @@ -926,6 +926,7 @@ fn choose_surface_format(capabilities: &wgpu::SurfaceCapabilities) -> wgpu::Text // TODO: repro and report to wgpu, supposing that it is a wgpu bug is_float: matches!(format, Rgba16Float | Rgba32Float | Rgb9e5Ufloat) && !cfg!(target_family = "wasm"), + #[allow(clippy::cast_possible_wrap)] negated_original_order: -(index as isize), } }) diff --git a/all-is-cubes-gpu/src/in_wgpu/light_texture.rs b/all-is-cubes-gpu/src/in_wgpu/light_texture.rs index 319a93dcd..ea2c47bfc 100644 --- a/all-is-cubes-gpu/src/in_wgpu/light_texture.rs +++ b/all-is-cubes-gpu/src/in_wgpu/light_texture.rs @@ -84,6 +84,7 @@ impl LightTexture { space_bounds: GridAab, view_distance: FreeCoordinate, ) -> GridSize { + #[allow(clippy::cast_possible_wrap)] // protected by min() let max_texture_size = limits.max_texture_dimension_3d.min(i32::MAX as u32) as i32; // Extra volume of 1 extra cube around all sides automatically captures sky light. diff --git a/all-is-cubes-gpu/src/in_wgpu/raytrace_to_texture.rs b/all-is-cubes-gpu/src/in_wgpu/raytrace_to_texture.rs index 2f3c00db1..88f0d691b 100644 --- a/all-is-cubes-gpu/src/in_wgpu/raytrace_to_texture.rs +++ b/all-is-cubes-gpu/src/in_wgpu/raytrace_to_texture.rs @@ -260,6 +260,7 @@ impl PixelPicker { impl Iterator for PixelPicker { type Item = Point; + #[allow(clippy::cast_possible_wrap)] fn next(&mut self) -> Option { // `as usize` is safe because we would have failed earlier if it doesn't fit in usize. let size = self.viewport.framebuffer_size.map(|s| s as usize); diff --git a/all-is-cubes-port/src/mv.rs b/all-is-cubes-port/src/mv.rs index 93c035b39..428c4057f 100644 --- a/all-is-cubes-port/src/mv.rs +++ b/all-is-cubes-port/src/mv.rs @@ -5,7 +5,9 @@ use all_is_cubes::character::{Character, Spawn}; use all_is_cubes::content::free_editing_starter_inventory; use all_is_cubes::euclid::{vec3, Point3D, Vector3D}; use all_is_cubes::linking::InGenError; -use all_is_cubes::math::{Cube, GridAab, GridRotation, GridVector, Gridgid, Rgb, Rgba}; +use all_is_cubes::math::{ + Cube, GridAab, GridCoordinate, GridRotation, GridVector, Gridgid, Rgb, Rgba, +}; use all_is_cubes::space::{LightPhysics, SetCubeError, Space}; use all_is_cubes::universe::{self, Name, PartialUniverse, Universe}; use all_is_cubes::util::{ConciseDebug, Refmt, YieldProgress}; @@ -160,11 +162,7 @@ fn dot_vox_model_to_space( let transform = mv_to_aic_coordinate_transform(model.size); let bounds = GridAab::from_lower_size( [0, 0, 0], - [ - model.size.x as i32, - model.size.y as i32, - model.size.z as i32, - ], + vec3(model.size.x, model.size.y, model.size.z).to_i32(), ) .transform(transform) .expect("TODO: return error"); @@ -293,7 +291,9 @@ fn mv_to_aic_coordinate_transform(mv_size: dot_vox::Size) -> Gridgid { // (This is not a `GridRotation::to_positive_octant_matrix()` because the `sizes` are // not necessarily equal.) Gridgid { - translation: GridVector::new(0, 0, mv_size.y as i32), + // Unwrap OK-ish because the actual allowed data size is limited to much smaller values + // (1024?). Still, TODO: make this an import error instead. + translation: GridVector::new(0, 0, GridCoordinate::try_from(mv_size.y).unwrap()), rotation: GridRotation::RXzY, } } diff --git a/all-is-cubes-ui/src/vui/widgets/text.rs b/all-is-cubes-ui/src/vui/widgets/text.rs index cbdfd5c01..567ea0642 100644 --- a/all-is-cubes-ui/src/vui/widgets/text.rs +++ b/all-is-cubes-ui/src/vui/widgets/text.rs @@ -245,7 +245,7 @@ mod tests { use all_is_cubes::block::text::Font; use all_is_cubes::color_block; use all_is_cubes::euclid::size3; - use all_is_cubes::math::Rgba; + use all_is_cubes::math::{GridCoordinate, Rgba}; use all_is_cubes::space::{SpaceBuilder, SpacePhysics}; #[test] @@ -260,7 +260,7 @@ mod tests { assert_eq!( widget.requirements(), LayoutRequest { - minimum: size3(9 * text.len() as i32, 15, 1) + minimum: size3(9 * GridCoordinate::try_from(text.len()).unwrap(), 15, 1) } ); } diff --git a/all-is-cubes-wasm/Cargo.toml b/all-is-cubes-wasm/Cargo.toml index 8cc5e56a9..9eb1fc1d6 100644 --- a/all-is-cubes-wasm/Cargo.toml +++ b/all-is-cubes-wasm/Cargo.toml @@ -110,8 +110,7 @@ clippy.needless_update = "allow" clippy.single_match = "allow" # clippy::pedantic lints that are set to allow -clippy.cast_possible_truncation = "allow" # consider enabling -clippy.cast_possible_wrap = "allow" # consider enabling +clippy.cast_possible_truncation = "allow" # we would need an alternative for intentional from-float saturation and to-float precision loss clippy.cast_precision_loss = "allow" # consider enabling clippy.cast_sign_loss = "allow" # consider enabling clippy.default_trait_access = "allow" @@ -152,6 +151,7 @@ clippy.should_panic_without_expect = "deny" clippy.pedantic = { level = "warn", priority = -1 } clippy.assigning_clones = "warn" clippy.cast_lossless = "warn" +clippy.cast_possible_wrap = "warn" clippy.doc_markdown = "warn" clippy.exhaustive_enums = "warn" clippy.exhaustive_structs = "warn" diff --git a/all-is-cubes/src/character/cursor.rs b/all-is-cubes/src/character/cursor.rs index f7e83e81a..7c03e5536 100644 --- a/all-is-cubes/src/character/cursor.rs +++ b/all-is-cubes/src/character/cursor.rs @@ -322,11 +322,11 @@ mod tests { use crate::content::{make_slab, make_some_blocks}; use crate::math::{GridAab, Rgba}; use crate::universe::Universe; - use euclid::{Point3D, Vector3D}; + use euclid::{vec3, Point3D, Vector3D}; fn test_space(universe: &mut Universe, blocks: [&Block; N]) -> Handle { let mut space = - Space::builder(GridAab::from_lower_size([0, 0, 0], [N as i32, 1, 1])).build(); + Space::builder(GridAab::from_lower_size([0, 0, 0], vec3(N, 1, 1).to_i32())).build(); space .fill(space.bounds(), |p| Some(blocks[p.x as usize])) .unwrap(); diff --git a/all-is-cubes/src/content/load_image.rs b/all-is-cubes/src/content/load_image.rs index 526edee06..efcb043c1 100644 --- a/all-is-cubes/src/content/load_image.rs +++ b/all-is-cubes/src/content/load_image.rs @@ -14,7 +14,7 @@ use png_decoder::PngHeader; use crate::block::{Block, AIR}; use crate::drawing::{rectangle_to_aab, VoxelBrush}; -use crate::math::{GridAab, GridRotation, Rgba}; +use crate::math::{GridAab, GridCoordinate, GridRotation, Rgba}; use crate::space::{SetCubeError, Space, SpacePhysics}; /// Return type of `png_decoder::decode`. @@ -142,8 +142,9 @@ pub fn space_from_image<'b>( let header = &png.0; // TODO: let caller control the transform offsets (not necessarily positive-octant) - let transform = - rotation.to_positive_octant_transform(header.width.max(header.height) as i32 - 1); + let transform = rotation.to_positive_octant_transform( + GridCoordinate::try_from(header.width.max(header.height)).unwrap() - 1, + ); let ia = &PngAdapter::adapt(png, pixel_function); let eg_image = embedded_graphics::image::Image::new(&ia, Point::zero()); diff --git a/all-is-cubes/src/drawing.rs b/all-is-cubes/src/drawing.rs index 74cfda7b1..819d4b3da 100644 --- a/all-is-cubes/src/drawing.rs +++ b/all-is-cubes/src/drawing.rs @@ -73,6 +73,7 @@ pub fn rectangle_to_aab(rectangle: Rectangle, transform: Gridgid, max_brush: Gri // 2D-pixel. // TODO: propagate numeric overflow cases + #![allow(clippy::cast_possible_wrap)] if rectangle.size.width == 0 || rectangle.size.height == 0 { // Handle zero-sized rectangles — they don't draw any pixels, so don't enlarge them diff --git a/all-is-cubes/src/universe.rs b/all-is-cubes/src/universe.rs index bcdf2a103..416f14978 100644 --- a/all-is-cubes/src/universe.rs +++ b/all-is-cubes/src/universe.rs @@ -697,6 +697,7 @@ impl Universe { #[allow(clippy::unused_self)] fn log_rerun_time(&self) { #[cfg(feature = "rerun")] + #[allow(clippy::cast_possible_wrap)] self.rerun_destination .stream .set_time_sequence("session_step_time", self.session_step_time as i64); diff --git a/test-renderers/src/test_cases.rs b/test-renderers/src/test_cases.rs index 34c628ae7..d6ef3cc7f 100644 --- a/test-renderers/src/test_cases.rs +++ b/test-renderers/src/test_cases.rs @@ -1,6 +1,7 @@ //! Graphical test cases that can be run in any renderer. #![allow(clippy::unused_async)] +#![allow(clippy::cast_possible_wrap)] use std::future::Future; use std::str::FromStr;