Skip to content

Commit

Permalink
Enable clippy::cast_possible_wrap.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kpreid committed Jun 17, 2024
1 parent 541a663 commit acccc42
Show file tree
Hide file tree
Showing 14 changed files with 28 additions and 27 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
11 changes: 2 additions & 9 deletions all-is-cubes-base/src/math/vol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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(),
)
}
Expand Down
1 change: 1 addition & 0 deletions all-is-cubes-content/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions all-is-cubes-gpu/src/in_wgpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
})
Expand Down
1 change: 1 addition & 0 deletions all-is-cubes-gpu/src/in_wgpu/light_texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions all-is-cubes-gpu/src/in_wgpu/raytrace_to_texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ impl PixelPicker {
impl Iterator for PixelPicker {
type Item = Point;

#[allow(clippy::cast_possible_wrap)]
fn next(&mut self) -> Option<Self::Item> {
// `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);
Expand Down
14 changes: 7 additions & 7 deletions all-is-cubes-port/src/mv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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,
}
}
Expand Down
4 changes: 2 additions & 2 deletions all-is-cubes-ui/src/vui/widgets/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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)
}
);
}
Expand Down
4 changes: 2 additions & 2 deletions all-is-cubes-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions all-is-cubes/src/character/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<const N: usize>(universe: &mut Universe, blocks: [&Block; N]) -> Handle<Space> {
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();
Expand Down
7 changes: 4 additions & 3 deletions all-is-cubes/src/content/load_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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());
Expand Down
1 change: 1 addition & 0 deletions all-is-cubes/src/drawing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions all-is-cubes/src/universe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions test-renderers/src/test_cases.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit acccc42

Please sign in to comment.