From c73db2edbe6328316a228b81452d687e1db2c26e Mon Sep 17 00:00:00 2001 From: Nathan Vegdahl Date: Sat, 15 Dec 2018 23:26:12 -0800 Subject: [PATCH] Fix/silence a bunch of clippy warnings in the main crate. --- src/accel/bvh.rs | 2 +- src/accel/light_tree.rs | 6 +----- src/algorithm.rs | 8 ++++---- src/bbox.rs | 2 +- src/camera.rs | 16 ++++++++-------- src/hash.rs | 6 +++--- src/image.rs | 2 +- src/light/distant_disk_light.rs | 6 +++--- src/light/rectangle_light.rs | 4 ++-- src/light/sphere_light.rs | 2 +- src/main.rs | 27 ++++++++++++++++----------- src/math.rs | 14 +++++++------- src/parse/data_tree.rs | 4 ++-- src/parse/psy.rs | 10 +++++----- src/parse/psy_light.rs | 6 +++--- src/parse/psy_mesh_surface.rs | 6 +++--- src/renderer.rs | 2 +- src/shading/surface_closure.rs | 4 ++-- src/surface/triangle_mesh.rs | 8 ++++---- src/timer.rs | 14 +++++++------- sub_crates/oct32norm/src/lib.rs | 14 +++++++++----- 21 files changed, 84 insertions(+), 79 deletions(-) diff --git a/src/accel/bvh.rs b/src/accel/bvh.rs index 7f51d4d..30215b6 100644 --- a/src/accel/bvh.rs +++ b/src/accel/bvh.rs @@ -160,7 +160,7 @@ impl<'a> BVH<'a> { }); } - #[cfg_attr(feature = "cargo-clippy", allow(mut_from_ref))] + #[allow(clippy::mut_from_ref)] fn construct_from_base( arena: &'a MemArena, base: &BVHBase, diff --git a/src/accel/light_tree.rs b/src/accel/light_tree.rs index f8a2461..be3bab6 100644 --- a/src/accel/light_tree.rs +++ b/src/accel/light_tree.rs @@ -135,10 +135,6 @@ impl<'a> LightAccel for LightTree<'a> { time: f32, n: f32, ) -> Option<(usize, f32, f32)> { - if self.root.is_none() { - return None; - } - // Calculates the selection probability for a node let node_prob = |node_ref: &Node| { let bbox = lerp_slice(node_ref.bounds(), time); @@ -153,7 +149,7 @@ impl<'a> LightAccel for LightTree<'a> { }; // Traverse down the tree, keeping track of the relative probabilities - let mut node = self.root.unwrap(); + let mut node = self.root?; let mut tot_prob = 1.0; let mut n = n; while let Node::Inner { children, .. } = *node { diff --git a/src/algorithm.rs b/src/algorithm.rs index bd28066..017a561 100644 --- a/src/algorithm.rs +++ b/src/algorithm.rs @@ -46,7 +46,7 @@ where // performance out of the code. unsafe { let mut a = slc.as_mut_ptr(); - let mut b = a.offset(slc.len() as isize); + let mut b = a.add(slc.len()); let start = a as usize; loop { @@ -96,7 +96,7 @@ where // performance out of the code. unsafe { let mut a = slc.as_mut_ptr(); - let mut b = a.offset(slc.len() as isize); + let mut b = a.add(slc.len()); let start = a as usize; loop { @@ -151,8 +151,8 @@ where unsafe { let mut a1 = slc1.as_mut_ptr(); let mut a2 = slc2.as_mut_ptr(); - let mut b1 = a1.offset(slc1.len() as isize); - let mut b2 = a2.offset(slc2.len() as isize); + let mut b1 = a1.add(slc1.len()); + let mut b2 = a2.add(slc2.len()); let start = a1 as usize; loop { diff --git a/src/bbox.rs b/src/bbox.rs index 7dadc7a..45f4de3 100644 --- a/src/bbox.rs +++ b/src/bbox.rs @@ -8,7 +8,7 @@ use lerp::{lerp, lerp_slice, Lerp}; use math::{fast_minf32, Matrix4x4, Point}; use ray::AccelRay; -const BBOX_MAXT_ADJUST: f32 = 1.00000024; +const BBOX_MAXT_ADJUST: f32 = 1.000_000_24; /// A 3D axis-aligned bounding box. #[derive(Debug, Copy, Clone)] diff --git a/src/camera.rs b/src/camera.rs index 58e7648..17c4a56 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -19,18 +19,18 @@ pub struct Camera<'a> { impl<'a> Camera<'a> { pub fn new( arena: &'a MemArena, - transforms: Vec, - fovs: Vec, - mut aperture_radii: Vec, - mut focus_distances: Vec, + transforms: &[Matrix4x4], + fovs: &[f32], + mut aperture_radii: &[f32], + mut focus_distances: &[f32], ) -> Camera<'a> { assert!(!transforms.is_empty(), "Camera has no transform(s)!"); assert!(!fovs.is_empty(), "Camera has no fov(s)!"); // Aperture needs focus distance and vice-versa. if aperture_radii.is_empty() || focus_distances.is_empty() { - aperture_radii = vec![0.0]; - focus_distances = vec![1.0]; + aperture_radii = &[0.0]; + focus_distances = &[1.0]; if aperture_radii.is_empty() && !focus_distances.is_empty() { println!( @@ -50,8 +50,8 @@ impl<'a> Camera<'a> { if aperture_radii.iter().any(|a| *a > 0.0) { println!("WARNING: camera focal distance is zero or less. Disabling focal blur."); } - aperture_radii = vec![0.0]; - focus_distances = vec![1.0]; + aperture_radii = &[0.0]; + focus_distances = &[1.0]; } // Convert angle fov into linear fov. diff --git a/src/hash.rs b/src/hash.rs index ad52a76..3614bde 100644 --- a/src/hash.rs +++ b/src/hash.rs @@ -3,7 +3,7 @@ use std; pub fn hash_u32(n: u32, seed: u32) -> u32 { let mut hash = n; for _ in 0..3 { - hash = hash.wrapping_mul(1936502639); + hash = hash.wrapping_mul(1_936_502_639); hash ^= hash.wrapping_shr(16); hash = hash.wrapping_add(seed); } @@ -14,7 +14,7 @@ pub fn hash_u32(n: u32, seed: u32) -> u32 { pub fn hash_u64(n: u64, seed: u64) -> u64 { let mut hash = n; for _ in 0..4 { - hash = hash.wrapping_mul(32416190071 * 314604959); + hash = hash.wrapping_mul(32_416_190_071 * 314_604_959); hash ^= hash.wrapping_shr(32); hash = hash.wrapping_add(seed); } @@ -28,7 +28,7 @@ pub fn hash_u64(n: u64, seed: u64) -> u64 { pub fn hash_u32_to_f32(n: u32, seed: u32) -> f32 { let mut hash = n; for _ in 0..3 { - hash = hash.wrapping_mul(1936502639); + hash = hash.wrapping_mul(1_936_502_639); hash ^= hash.wrapping_shr(16); hash = hash.wrapping_add(seed); } diff --git a/src/image.rs b/src/image.rs index e834213..f6d9009 100644 --- a/src/image.rs +++ b/src/image.rs @@ -271,7 +271,7 @@ impl<'a> Drop for Bucket<'a> { } fn srgb_gamma(n: f32) -> f32 { - if n < 0.0031308 { + if n < 0.003_130_8 { n * 12.92 } else { (1.055 * n.powf(1.0 / 2.4)) - 0.055 diff --git a/src/light/distant_disk_light.rs b/src/light/distant_disk_light.rs index 8cc286a..6c8566a 100644 --- a/src/light/distant_disk_light.rs +++ b/src/light/distant_disk_light.rs @@ -21,9 +21,9 @@ pub struct DistantDiskLight<'a> { impl<'a> DistantDiskLight<'a> { pub fn new( arena: &'a MemArena, - radii: Vec, - directions: Vec, - colors: Vec, + radii: &[f32], + directions: &[Vector], + colors: &[XYZ], ) -> DistantDiskLight<'a> { DistantDiskLight { radii: arena.copy_slice(&radii), diff --git a/src/light/rectangle_light.rs b/src/light/rectangle_light.rs index df92df6..90704c7 100644 --- a/src/light/rectangle_light.rs +++ b/src/light/rectangle_light.rs @@ -28,8 +28,8 @@ pub struct RectangleLight<'a> { impl<'a> RectangleLight<'a> { pub fn new<'b>( arena: &'b MemArena, - dimensions: Vec<(f32, f32)>, - colors: Vec, + dimensions: &[(f32, f32)], + colors: &[XYZ], ) -> RectangleLight<'b> { let bbs: Vec<_> = dimensions .iter() diff --git a/src/light/sphere_light.rs b/src/light/sphere_light.rs index 2876f57..9401774 100644 --- a/src/light/sphere_light.rs +++ b/src/light/sphere_light.rs @@ -29,7 +29,7 @@ pub struct SphereLight<'a> { } impl<'a> SphereLight<'a> { - pub fn new<'b>(arena: &'b MemArena, radii: Vec, colors: Vec) -> SphereLight<'b> { + pub fn new<'b>(arena: &'b MemArena, radii: &[f32], colors: &[XYZ]) -> SphereLight<'b> { let bbs: Vec<_> = radii .iter() .map(|r| BBox { diff --git a/src/main.rs b/src/main.rs index b1cde9d..a24521a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,15 @@ -#![cfg_attr(feature = "cargo-clippy", allow(float_cmp))] -#![cfg_attr(feature = "cargo-clippy", allow(inline_always))] -#![cfg_attr(feature = "cargo-clippy", allow(many_single_char_names))] -#![cfg_attr(feature = "cargo-clippy", allow(needless_lifetimes))] -#![cfg_attr(feature = "cargo-clippy", allow(needless_return))] -#![cfg_attr(feature = "cargo-clippy", allow(or_fun_call))] -#![cfg_attr(feature = "cargo-clippy", allow(too_many_arguments))] +#![allow(clippy::float_cmp)] +#![allow(clippy::inline_always)] +#![allow(clippy::many_single_char_names)] +#![allow(clippy::needless_lifetimes)] +#![allow(clippy::needless_return)] +#![allow(clippy::or_fun_call)] +#![allow(clippy::too_many_arguments)] +#![allow(clippy::redundant_field_names)] +#![allow(clippy::enum_variant_names)] +#![allow(clippy::cast_lossless)] +#![allow(clippy::needless_range_loop)] +#![allow(clippy::excessive_precision)] extern crate bvh_order; extern crate color as color_util; extern crate float4; @@ -74,7 +79,7 @@ use renderer::LightPath; use surface::SurfaceIntersection; use timer::Timer; -const VERSION: &'static str = env!("CARGO_PKG_VERSION"); +const VERSION: &str = env!("CARGO_PKG_VERSION"); fn main() { let mut t = Timer::new(); @@ -349,8 +354,8 @@ fn main() { // Print memory stats if stats are wanted. if args.is_present("stats") { let arena_stats = arena.stats(); - let mib_occupied = arena_stats.0 as f64 / 1048576.0; - let mib_allocated = arena_stats.1 as f64 / 1048576.0; + let mib_occupied = arena_stats.0 as f64 / 1_048_576.0; + let mib_allocated = arena_stats.1 as f64 / 1_048_576.0; println!("MemArena stats:"); @@ -373,5 +378,5 @@ fn main() { } // End with blank line - println!(""); + println!(); } diff --git a/src/math.rs b/src/math.rs index dea460d..25275fe 100644 --- a/src/math.rs +++ b/src/math.rs @@ -133,8 +133,8 @@ pub fn fast_ln(x: f32) -> f32 { use std::mem::transmute_copy; let mut y = unsafe { transmute_copy::(&x) as f32 }; - y *= 8.2629582881927490e-8; - y - 87.989971088 + y *= 8.262_958_288_192_749_0e-8; + y - 87.989_971_088 } pub fn fast_pow2(p: f32) -> f32 { @@ -146,7 +146,7 @@ pub fn fast_pow2(p: f32) -> f32 { let z: f32 = clipp - w as f32 + offset; let i: u32 = ((1 << 23) as f32 - * (clipp + 121.2740575 + 27.7280233 / (4.84252568 - z) - 1.49012907 * z)) + * (clipp + 121.274_057_5 + 27.728_023_3 / (4.842_525_68 - z) - 1.490_129_07 * z)) as u32; unsafe { transmute_copy::(&i) } @@ -156,10 +156,10 @@ pub fn fast_log2(x: f32) -> f32 { use std::mem::transmute_copy; let xi = unsafe { transmute_copy::(&x) }; - let y = xi as f32 * 1.1920928955078125e-7; - let mx = unsafe { transmute_copy::(&((xi & 0x007FFFFF) | 0x3f000000)) }; + let y = xi as f32 * 1.192_092_895_507_812_5e-7; + let mx = unsafe { transmute_copy::(&((xi & 0x007F_FFFF) | 0x3f00_0000)) }; - y - 124.22551499 - 1.498030302 * mx - 1.72587999 / (0.3520887068 + mx) + y - 124.225_514_99 - 1.498_030_302 * mx - 1.725_879_99 / (0.352_088_706_8 + mx) } pub fn fast_exp(p: f32) -> f32 { @@ -174,7 +174,7 @@ pub fn faster_pow2(p: f32) -> f32 { use std::mem::transmute_copy; let clipp: f32 = if p < -126.0 { -126.0 } else { p }; - let i: u32 = ((1 << 23) as f32 * (clipp + 126.94269504)) as u32; + let i: u32 = ((1 << 23) as f32 * (clipp + 126.942_695_04)) as u32; unsafe { transmute_copy::(&i) } } diff --git a/src/parse/data_tree.rs b/src/parse/data_tree.rs index 1d00236..6f665ec 100644 --- a/src/parse/data_tree.rs +++ b/src/parse/data_tree.rs @@ -142,7 +142,7 @@ impl<'a> DataTree<'a> { type_name, ident, ref children, - byte_offset: _, + .. } = *self { (type_name, ident, children) @@ -154,7 +154,7 @@ impl<'a> DataTree<'a> { if let DataTree::Leaf { type_name, contents, - byte_offset: _, + .. } = *self { (type_name, contents) diff --git a/src/parse/psy.rs b/src/parse/psy.rs index ec67cb9..eb89597 100644 --- a/src/parse/psy.rs +++ b/src/parse/psy.rs @@ -217,7 +217,7 @@ fn parse_output_info(tree: &DataTree) -> Result { incorrect.", )); } - if tc.chars().nth(0).unwrap() != '"' || tc.chars().last().unwrap() != '"' { + if tc.chars().nth(0).unwrap() != '"' || !tc.ends_with('"') { return Err(PsyParseError::IncorrectLeafData( byte_offset, "File paths must be \ @@ -434,10 +434,10 @@ fn parse_camera<'a>(arena: &'a MemArena, tree: &'a DataTree) -> Result( } } - return Ok(DistantDiskLight::new(arena, radii, directions, colors)); + return Ok(DistantDiskLight::new(arena, &radii, &directions, &colors)); } else { return Err(PsyParseError::UnknownError(tree.byte_offset())); } @@ -133,7 +133,7 @@ pub fn parse_sphere_light<'a>( } } - return Ok(SphereLight::new(arena, radii, colors)); + return Ok(SphereLight::new(arena, &radii, &colors)); } else { return Err(PsyParseError::UnknownError(tree.byte_offset())); } @@ -189,7 +189,7 @@ pub fn parse_rectangle_light<'a>( } } - return Ok(RectangleLight::new(arena, dimensions, colors)); + return Ok(RectangleLight::new(arena, &dimensions, &colors)); } else { return Err(PsyParseError::UnknownError(tree.byte_offset())); } diff --git a/src/parse/psy_mesh_surface.rs b/src/parse/psy_mesh_surface.rs index 3d9a61b..8e694c0 100644 --- a/src/parse/psy_mesh_surface.rs +++ b/src/parse/psy_mesh_surface.rs @@ -123,12 +123,12 @@ pub fn parse_mesh_surface<'a>( Ok(TriangleMesh::from_verts_and_indices( arena, - verts, - if normals.is_empty() { + &verts, + &if normals.is_empty() { None } else { Some(normals) }, - tri_vert_indices, + &tri_vert_indices, )) } diff --git a/src/renderer.rs b/src/renderer.rs index e876eba..24a4c40 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -445,7 +445,7 @@ impl LightPath { // - Collect light from the emission. // - Terminate the path. use shading::surface_closure::SurfaceClosureUnion; - if let &SurfaceClosureUnion::EmitClosure(ref clsr) = closure { + if let SurfaceClosureUnion::EmitClosure(ref clsr) = *closure { if let LightPathEvent::CameraRay = self.event { self.color += clsr.emitted_color().e; } else { diff --git a/src/shading/surface_closure.rs b/src/shading/surface_closure.rs index 993d6c7..7e6a08d 100644 --- a/src/shading/surface_closure.rs +++ b/src/shading/surface_closure.rs @@ -141,7 +141,7 @@ fn dielectric_fresnel_from_fac(fresnel_fac: f32, c: f32) -> f32 { let tmp1 = fresnel_fac.sqrt() - 1.0; // Protect against divide by zero. - if tmp1.abs() < 0.000001 { + if tmp1.abs() < 0.000_001 { return 1.0; } @@ -410,7 +410,7 @@ impl GTRClosure { self.tail_shape = (0.0001f32).max(self.tail_shape); // When roughness is too small, but not zero, there are floating point accuracy issues - if self.roughness < 0.000244140625 { + if self.roughness < 0.000_244_140_625 { // (2^-12) self.roughness = 0.0; } diff --git a/src/surface/triangle_mesh.rs b/src/surface/triangle_mesh.rs index 09fb6a6..ed2eecc 100644 --- a/src/surface/triangle_mesh.rs +++ b/src/surface/triangle_mesh.rs @@ -27,9 +27,9 @@ pub struct TriangleMesh<'a> { impl<'a> TriangleMesh<'a> { pub fn from_verts_and_indices<'b>( arena: &'b MemArena, - verts: Vec>, - vert_normals: Option>>, - tri_indices: Vec<(usize, usize, usize)>, + verts: &[Vec], + vert_normals: &Option>>, + tri_indices: &[(usize, usize, usize)], ) -> TriangleMesh<'b> { let vert_count = verts[0].len(); let time_sample_count = verts.len(); @@ -80,7 +80,7 @@ impl<'a> TriangleMesh<'a> { // Create bounds array for use during BVH construction let bounds = { let mut bounds = Vec::with_capacity(indices.len() * time_sample_count); - for tri in &tri_indices { + for tri in tri_indices { for ti in 0..time_sample_count { let p0 = verts[ti][tri.0]; let p1 = verts[ti][tri.1]; diff --git a/src/timer.rs b/src/timer.rs index 352df15..a42cae6 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -24,23 +24,23 @@ impl Timer { let dt = n - self.last_time; self.last_time = n; - dt as f32 / 1000000000.0 + dt as f32 / 1_000_000_000.0 } /// Returns the time elapsed in seconds since the last call to tick(). - pub fn elapsed(&self) -> f32 { + pub fn elapsed(self) -> f32 { let dt = time::precise_time_ns() - self.last_time; - dt as f32 / 1000000000.0 + dt as f32 / 1_000_000_000.0 } /// Sleeps the current thread until n seconds after the last tick. - pub fn sleep_until(&self, n: f32) { + pub fn sleep_until(self, n: f32) { let dt = time::precise_time_ns() - self.last_time; - let target_dt = ((n as f64) * 1000000000.0) as u64; + let target_dt = ((n as f64) * 1_000_000_000.0) as u64; if dt < target_dt { let delay = target_dt - dt; - let seconds = delay / 1000000000; - let nanoseconds = delay % 1000000000; + let seconds = delay / 1_000_000_000; + let nanoseconds = delay % 1_000_000_000; thread::sleep(Duration::new(seconds, nanoseconds as u32)); } } diff --git a/sub_crates/oct32norm/src/lib.rs b/sub_crates/oct32norm/src/lib.rs index 81c0309..63c085f 100644 --- a/sub_crates/oct32norm/src/lib.rs +++ b/sub_crates/oct32norm/src/lib.rs @@ -14,13 +14,17 @@ pub fn encode(vec: (f32, f32, f32)) -> u32 { let (u, v) = if vec.2 < 0.0 { ( - to_snorm_16((1.0 - (vec.1 * inv_l1_norm).abs()) * sign(vec.0)) as u32, - to_snorm_16((1.0 - (vec.0 * inv_l1_norm).abs()) * sign(vec.1)) as u32, + u32::from(to_snorm_16( + (1.0 - (vec.1 * inv_l1_norm).abs()) * sign(vec.0), + )), + u32::from(to_snorm_16( + (1.0 - (vec.0 * inv_l1_norm).abs()) * sign(vec.1), + )), ) } else { ( - to_snorm_16(vec.0 * inv_l1_norm) as u32, - to_snorm_16(vec.1 * inv_l1_norm) as u32, + u32::from(to_snorm_16(vec.0 * inv_l1_norm)), + u32::from(to_snorm_16(vec.1 * inv_l1_norm)), ) }; @@ -53,7 +57,7 @@ fn to_snorm_16(n: f32) -> u16 { #[inline(always)] fn from_snorm_16(n: u16) -> f32 { - (n as i16 as f32) + f32::from(n as i16) * (1.0f32 / ((1u32 << (16 - 1)) - 1) as f32) .max(-1.0) .min(1.0)