Fix all clippy warnings or block them.

This commit is contained in:
Bill Thiede 2018-10-11 19:53:07 -07:00
parent fa7d79b112
commit 66d599b50d
13 changed files with 60 additions and 80 deletions

View File

@ -52,7 +52,7 @@ impl AABB {
if yd >= xd && yd >= zd {
return 1;
}
return 2;
2
}
pub fn mid_point(&self) -> Vec3 {
@ -153,7 +153,7 @@ impl AABB {
tmax = tmax.min(t1.max(t2));
}
return tmax > tmin.max(0.0);
tmax > tmin.max(0.0)
}
}

View File

@ -16,8 +16,6 @@ extern crate structopt;
extern crate rtiow;
use actix_web::Path as WebPath;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::time::SystemTime;
@ -135,27 +133,6 @@ struct NoiseParamsMarble {
}
impl NoiseParams {
fn compact_string(&self) -> String {
let mut s = format!("w{}-h{}", self.width, self.height);
match self.noise {
NoiseType::Scale(scale) => s.push_str(&format!("-s{:.2}", scale)),
NoiseType::Turbulence(turbulence) => s.push_str(&format!("-t{}", turbulence)),
NoiseType::Marble {
period,
power,
size,
} => s.push_str(&format!(
"-m{}-{}-{}",
period.to_string().replace(" ", "-"),
power,
size
)),
}
s
}
fn url(&self) -> String {
format!(
"/noise/{}/{}/{}",
@ -168,15 +145,11 @@ impl NoiseParams {
fn big_url(&self) -> String {
format!("/noise/1024/1024/{}", self.noise.to_url())
}
fn image_name(&self) -> String {
format!("noise-{}.png", self.compact_string())
}
}
fn render_noise(noise_params: &NoiseParams) -> image::GrayImage {
const SEED: [u8; 16] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
let ref mut rng: XorShiftRng = SeedableRng::from_seed(SEED);
let rng: &mut XorShiftRng = &mut SeedableRng::from_seed(SEED);
let mut img = image::GrayImage::new(noise_params.width, noise_params.height);
let tex = Noise::new(rng);
@ -210,18 +183,6 @@ fn render_noise(noise_params: &NoiseParams) -> image::GrayImage {
img
}
fn render_noise_to_disk<P>(output_dir: P, noise_params: &NoiseParams) -> Result<(), std::io::Error>
where
P: AsRef<Path>,
{
let img = render_noise(noise_params);
let ref output_dir: &Path = output_dir.as_ref();
fs::create_dir_all(output_dir)?;
let path = output_dir.join(noise_params.image_name());
info!("Saving {}", path.display());
img.save(path)
}
struct Specimen {
title: String,
params: Vec<NoiseParams>,
@ -366,7 +327,7 @@ fn index(_req: &HttpRequest) -> Result<HttpResponse> {
.body(render_html(specimens).unwrap()))
}
fn noise(np: NoiseParams) -> Result<HttpResponse> {
fn noise(np: &NoiseParams) -> Result<HttpResponse> {
let img = render_noise(&np);
let mut buf = Vec::new();
let img = image::DynamicImage::ImageLuma8(img);
@ -376,24 +337,28 @@ fn noise(np: NoiseParams) -> Result<HttpResponse> {
Ok(HttpResponse::Ok().content_type("image/png").body(buf))
}
// Making parameter breaks WebPath::From magic.
#[cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))]
fn noise_scale(np: WebPath<NoiseParamsScale>) -> Result<HttpResponse> {
noise(NoiseParams {
noise(&NoiseParams {
width: np.width,
height: np.height,
noise: NoiseType::Scale(np.scale),
})
}
#[cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))]
fn noise_turbulence(np: WebPath<NoiseParamsTurbulence>) -> Result<HttpResponse> {
noise(NoiseParams {
noise(&NoiseParams {
width: np.width,
height: np.height,
noise: NoiseType::Turbulence(np.turbulence),
})
}
#[cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))]
fn noise_marble(np: WebPath<NoiseParamsMarble>) -> Result<HttpResponse> {
noise(NoiseParams {
noise(&NoiseParams {
width: np.width,
height: np.height,
noise: NoiseType::Marble {

View File

@ -41,28 +41,32 @@ impl fmt::Display for BVHNode {
}
}
// Lint is wrong when dealing with Box<Trait>
#[cfg_attr(feature = "cargo-clippy", allow(borrowed_box))]
fn box_x_compare(ah: &Box<Hit>, bh: &Box<Hit>) -> std::cmp::Ordering {
match (ah.bounding_box(0., 0.), bh.bounding_box(0., 0.)) {
(Some(box_left), Some(box_right)) => {
return box_left.min().x.partial_cmp(&box_right.min().x).unwrap();
box_left.min().x.partial_cmp(&box_right.min().x).unwrap()
}
_ => panic!("hit missing bounding box"),
}
}
#[cfg_attr(feature = "cargo-clippy", allow(borrowed_box))]
fn box_y_compare(ah: &Box<Hit>, bh: &Box<Hit>) -> std::cmp::Ordering {
match (ah.bounding_box(0., 0.), bh.bounding_box(0., 0.)) {
(Some(box_left), Some(box_right)) => {
return box_left.min().y.partial_cmp(&box_right.min().y).unwrap();
box_left.min().y.partial_cmp(&box_right.min().y).unwrap()
}
_ => panic!("hit missing bounding box"),
}
}
#[cfg_attr(feature = "cargo-clippy", allow(borrowed_box))]
fn box_z_compare(ah: &Box<Hit>, bh: &Box<Hit>) -> std::cmp::Ordering {
match (ah.bounding_box(0., 0.), bh.bounding_box(0., 0.)) {
(Some(box_left), Some(box_right)) => {
return box_left.min().z.partial_cmp(&box_right.min().z).unwrap();
box_left.min().z.partial_cmp(&box_right.min().z).unwrap()
}
_ => panic!("hit missing bounding box"),
}
@ -78,10 +82,7 @@ fn vec_first_into<T>(v: Vec<T>) -> T {
v.len()
));
}
for i in v.into_iter() {
return i;
}
panic!("Unreachable");
v.into_iter().next().unwrap()
}
fn vec_split_into<T>(v: Vec<T>, offset: usize) -> (Vec<T>, Vec<T>) {
@ -100,7 +101,7 @@ fn vec_split_into<T>(v: Vec<T>, offset: usize) -> (Vec<T>, Vec<T>) {
impl BVHNode {
fn new(l: Vec<Box<Hit>>, t_min: f32, t_max: f32) -> BVHNode {
if l.len() == 1 {
return BVHNode::Leaf(vec_first_into(l));
BVHNode::Leaf(vec_first_into(l))
} else {
let mut l: Vec<Box<Hit>> = l.into_iter().collect();
let mut rng = rand::thread_rng();
@ -108,7 +109,7 @@ impl BVHNode {
0 => l.sort_by(box_x_compare),
1 => l.sort_by(box_y_compare),
2 => l.sort_by(box_z_compare),
val @ _ => panic!("unknown axis {}", val),
val => panic!("unknown axis {}", val),
}
let half = l.len() / 2;
@ -116,7 +117,7 @@ impl BVHNode {
let left = Box::new(BVHNode::new(left_half, t_min, t_max));
let right = Box::new(BVHNode::new(right_half, t_min, t_max));
let bbox = BVHNode::surrounding_box(left.as_ref(), right.as_ref(), t_min, t_max);
return BVHNode::Branch { left, right, bbox };
BVHNode::Branch { left, right, bbox }
}
}
@ -144,7 +145,6 @@ impl Hit for BVHNode {
fn hit(&self, r: Ray, t_min: f32, t_max: f32) -> Option<HitRecord> {
match self {
BVHNode::Leaf(ref hit) => hit.hit(r, t_min, t_max),
// TODO(wathiede): call bbox.hit() before recursing down both paths of tree.
BVHNode::Branch {
ref left,
ref right,
@ -154,17 +154,16 @@ impl Hit for BVHNode {
if bbox.hit(r, t_min, t_max) {
match (left.hit(r, t_min, t_max), right.hit(r, t_min, t_max)) {
(Some(hit_left), Some(hit_right)) => if hit_left.t < hit_right.t {
return Some(hit_left);
Some(hit_left)
} else {
return Some(hit_right);
Some(hit_right)
},
(Some(hit_left), None) => return Some(hit_left),
(None, Some(hit_right)) => return Some(hit_right),
(None, None) => return None,
(Some(hit_left), None) => Some(hit_left),
(None, Some(hit_right)) => Some(hit_right),
(None, None) => None,
};
} else {
return None;
}
None
}
None => None,
},
@ -203,7 +202,7 @@ impl BVH {
fn print_tree(f: &mut fmt::Formatter, depth: usize, bvhn: &BVHNode) -> fmt::Result {
let vol = bvhn.volume();
write!(f, "{:.*}{}{}\n", 2, vol, " ".repeat(depth * 2), bvhn)?;
writeln!(f, "{:.*}{}{}", 2, vol, " ".repeat(depth * 2), bvhn)?;
if let BVHNode::Branch { left, right, .. } = bvhn {
print_tree(f, depth + 1, left)?;
print_tree(f, depth + 1, right)?;
@ -213,7 +212,7 @@ fn print_tree(f: &mut fmt::Formatter, depth: usize, bvhn: &BVHNode) -> fmt::Resu
impl fmt::Display for BVH {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Root\n")?;
writeln!(f, "Root")?;
print_tree(f, 1, &self.root)
}
}

View File

@ -35,6 +35,8 @@ pub struct Camera {
}
impl Camera {
// Parameters match the example C++ code.
#[cfg_attr(feature = "cargo-clippy", allow(too_many_arguments))]
// vfov is top to bottom in degrees
pub fn new(
lookfrom: Vec3,

View File

@ -19,6 +19,8 @@ pub struct Cuboid {
}
impl Cuboid {
// This clippy doesn't work right with Arc.
#[cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))]
pub fn new(p_min: Vec3, p_max: Vec3, material: Arc<Material>) -> Cuboid {
Cuboid {
p_min,

View File

@ -43,7 +43,7 @@ fn vec_split_into<T>(v: Vec<T>, offset: usize) -> (Vec<T>, Vec<T>) {
impl KDTree {
pub fn new(l: Vec<Box<Hit>>, t_min: f32, t_max: f32) -> KDTree {
if l.len() == 0 {
if l.is_empty() {
panic!("Attempt to build k-d tree with no Hit objects");
}
@ -119,15 +119,15 @@ fn print_tree(f: &mut fmt::Formatter, depth: usize, kdt: &KDTree) -> fmt::Result
let vol = kdt.volume();
write!(f, "{:8.2}{}", vol, " ".repeat(depth * 2))?;
match kdt {
KDTree::Leaf(ref hit) => write!(
KDTree::Leaf(ref hit) => writeln!(
f,
"Leaf: {}\n",
"Leaf: {}",
hit.bounding_box(0., 0.)
.map_or("NO BBOX".to_owned(), |bb| bb.to_string())
)?,
KDTree::Branch { bbox, .. } => write!(
KDTree::Branch { bbox, .. } => writeln!(
f,
"Branch: {}\n",
"Branch: {}",
// TODO(wathiede): removing this .clone() results in a complaint about moving out
// of a borrow.
bbox.clone()
@ -143,7 +143,7 @@ fn print_tree(f: &mut fmt::Formatter, depth: usize, kdt: &KDTree) -> fmt::Result
impl fmt::Display for KDTree {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "kd-tree\n")?;
writeln!(f, "kd-tree")?;
print_tree(f, 1, &self)
}
}

View File

@ -17,6 +17,8 @@ impl Noise {
R: rand::Rng,
{
let mut noise = vec![vec![vec![0.; NOISE_SIZE]; NOISE_SIZE]; NOISE_SIZE];
// Squelch warning about only being used to index, as this way reads more consistently.
#[cfg_attr(feature = "cargo-clippy", allow(needless_range_loop))]
for x in 0..NOISE_SIZE {
for y in 0..NOISE_SIZE {
for z in 0..NOISE_SIZE {

View File

@ -1,3 +1,5 @@
// There are many math functions in this file, so we allow single letter variable names.
#![cfg_attr(feature = "cargo-clippy", allow(many_single_char_names))]
use rand::Rng;
use vec3::dot;
@ -131,6 +133,9 @@ impl Perlin {
let k = p.z.floor() as usize;
let mut c: [[[Vec3; 2]; 2]; 2] = Default::default();
// Squelch warning about di only being used to index, as this way reads more consistently.
#[cfg_attr(feature = "cargo-clippy", allow(needless_range_loop))]
for di in 0..2 {
for dj in 0..2 {
for dk in 0..2 {

View File

@ -1,3 +1,5 @@
// There are many math functions in this file, so we allow single letter variable names.
#![cfg_attr(feature = "cargo-clippy", allow(many_single_char_names))]
use aabb::AABB;
use hitable::Hit;
use hitable::HitRecord;

View File

@ -48,7 +48,7 @@ pub fn new(opt: &Opt) -> Scene {
let nb = 20;
let ref mut rng = rand::thread_rng();
let rng = &mut rand::thread_rng();
let mut boxlist: Vec<Box<Hit>> = Vec::with_capacity(10000);
let mut list: Vec<Box<Hit>> = Vec::new();
let white: Arc<Material> = Arc::new(Lambertian::new(ConstantTexture::new([0.73, 0.73, 0.73])));

View File

@ -33,7 +33,7 @@ pub fn new(opt: &Opt) -> Scene {
time_max,
);
// TODO(wathiede): Use XOR rng for predictability?
let ref mut rng = rand::thread_rng();
let rng = &mut rand::thread_rng();
let pertext: Arc<Texture> = Arc::new(NoiseTexture::with_scale(rng, 10.));
let objects: Vec<Box<Hit>> = vec![

View File

@ -36,7 +36,7 @@ pub fn new(opt: &Opt) -> Scene {
time_min,
time_max,
);
let ref mut rng = rand::thread_rng();
let rng = &mut rand::thread_rng();
let _ground_color = if opt.use_accel {
Box::new(ConstantTexture::new(Vec3::new(1.0, 0.4, 0.4)))
} else {

View File

@ -114,8 +114,8 @@ impl ImageTexture {
let (w, h) = img.dimensions();
ImageTexture {
img,
width: w as f32,
height: h as f32,
width: f32::from(w.min(64000) as u16),
height: f32::from(h.min(64000) as u16),
}
}
}
@ -137,9 +137,12 @@ impl Texture for ImageTexture {
u, v, x, y, self.width, self.height
));
}
let p = self.img.get_pixel(x, y);
let rgb = Vec3::new(p[0] as f32 / 255., p[1] as f32 / 255., p[2] as f32 / 255.);
rgb
let pixel = self.img.get_pixel(x, y);
Vec3::new(
f32::from(pixel[0]) / 255.,
f32::from(pixel[1]) / 255.,
f32::from(pixel[2]) / 255.,
)
}
}