Skip to content

Commit d2ec8cf

Browse files
committed
enable most Clippy lints and fix all of the resulting errors; notably, replace impl trait arguments with explicit generics and improve the spectral distribution interface
1 parent 0aee12c commit d2ec8cf

39 files changed

+773
-478
lines changed

Cargo.lock

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,79 @@
11
[workspace]
22
resolver = "2"
33
members = ["crates/*"]
4+
5+
[workspace.lints.clippy]
6+
impl_trait_in_params = "deny"
7+
inefficient_to_string = "deny"
8+
manual_instant_elapsed = "deny"
9+
manual_is_variant_and = "deny"
10+
manual_let_else = "deny"
11+
map_unwrap_or = "deny"
12+
map_with_unused_argument_over_ranges = "deny"
13+
match_bool = "deny"
14+
mismatching_type_param_order = "deny"
15+
missing_assert_message = "deny"
16+
missing_asserts_for_indexing = "deny"
17+
missing_panics_doc = "deny"
18+
multiple_unsafe_ops_per_block = "deny"
19+
# must_use_candidate = "deny" # definitely too aggressive
20+
needless_bitwise_bool = "deny"
21+
needless_collect = "deny"
22+
needless_continue = "deny"
23+
needless_for_each = "deny"
24+
needless_pass_by_ref_mut = "deny"
25+
needless_pass_by_value = "deny"
26+
needless_raw_string_hashes = "deny"
27+
needless_raw_strings = "deny"
28+
non_send_fields_in_send_ty = "deny"
29+
non_zero_suggestions = "deny"
30+
option_if_let_else = "deny"
31+
option_option = "deny"
32+
or_fun_call = "deny"
33+
pattern_type_mismatch = "deny"
34+
# precedence_bits = "deny" # Rust 1.86
35+
print_stderr = "deny"
36+
print_stdout = "deny"
37+
ptr_as_ptr = "deny"
38+
ptr_cast_constness = "deny"
39+
range_minus_one = "deny"
40+
range_plus_one = "deny"
41+
rc_buffer = "deny"
42+
rc_mutex = "deny"
43+
redundant_clone = "deny"
44+
redundant_closure_for_method_calls = "deny"
45+
ref_as_ptr = "deny"
46+
ref_binding_to_reference = "deny"
47+
ref_option = "deny"
48+
ref_option_ref = "deny"
49+
return_self_not_must_use = "deny"
50+
single_match_else = "deny"
51+
std_instead_of_alloc = "deny"
52+
std_instead_of_core = "deny"
53+
str_split_at_newline = "deny"
54+
string_add = "deny"
55+
string_add_assign = "deny"
56+
string_extend_chars = "deny"
57+
string_lit_as_bytes = "deny"
58+
string_lit_chars_any = "deny"
59+
string_to_string = "deny"
60+
suboptimal_flops = "deny"
61+
trait_duplication_in_bounds = "deny"
62+
transmute_ptr_to_ptr = "deny"
63+
transmute_undefined_repr = "deny"
64+
tuple_array_conversions = "deny"
65+
type_repetition_in_bounds = "deny"
66+
unchecked_duration_subtraction = "deny"
67+
undocumented_unsafe_blocks = "deny"
68+
uninhabited_references = "deny"
69+
unnecessary_box_returns = "deny"
70+
unnecessary_join = "deny"
71+
unnecessary_literal_bound = "deny"
72+
unseparated_literal_suffix = "deny"
73+
unwrap_used = "deny"
74+
# use_self = "deny" # seems to be too aggressive
75+
useless_let_if_seq = "deny"
76+
verbose_bit_mask = "deny"
77+
verbose_file_reads = "deny"
78+
while_float = "deny"
79+

crates/prettypretty/CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Changelog
22

3+
## Prettypretty v0.12.0 (????-??-??)
4+
5+
The interface to spectral distributions has been improved: The new `get()`
6+
method now returns an optional value, whereas the updated `at()` returns the
7+
unwrapped value, panicking upon out-of-distribution wavelengths.
8+
9+
Many small code improvements, including the use of more accurate floating point
10+
operations throughout. All impl trait arguments have been replaced by explicit
11+
generic types bound by those same traits.
12+
13+
314
## Prettypretty v0.11.1 (2025-01-31)
415

516
Move `pretty.css` styles back into prettypretty and fix all plumbing so that it

crates/prettypretty/Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ version = "0.11.1"
88
repository = "https://github.com/apparebit/prettypretty"
99
documentation = "https://docs.rs/prettypretty/latest/prettypretty/"
1010
homepage = "https://crates.io/crates/prettypretty"
11-
rust-version = "1.79"
11+
rust-version = "1.82"
1212
edition = "2021"
1313
license = "Apache-2.0"
1414

1515

1616
[dependencies]
17-
pyo3 = { version = "0.23.3", features = ["extension-module", "abi3", "abi3-py311"], optional = true }
17+
pyo3 = { version = "0.23.4", features = ["extension-module", "abi3", "abi3-py311"], optional = true }
1818
prettytty = { version = "0.3.0", path = "../prettytty", optional = true }
1919

2020
[dev-dependencies]
@@ -34,6 +34,10 @@ name = "prettypretty"
3434
crate-type = ["lib", "cdylib"]
3535

3636

37+
[lints]
38+
workspace = true
39+
40+
3741
[package.metadata.docs.rs]
3842
features = ["f64", "gamut", "tty"]
3943
rustdoc-args = ["-e", "./docs/pretty.css"]

crates/prettypretty/src/cie/mod.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,17 @@ mod test {
2121
for illuminant in [&CIE_ILLUMINANT_D50, &CIE_ILLUMINANT_D65] {
2222
let mut sum = Sum::new();
2323
for wavelength in illuminant.range() {
24-
sum += illuminant.at(wavelength).unwrap();
24+
assert_eq!(*illuminant.at(wavelength), illuminant[wavelength]);
25+
sum += illuminant[wavelength];
2526
}
2627
assert_eq!(sum.value(), illuminant.checksum());
2728
}
2829

2930
for observer in [&CIE_OBSERVER_2DEG_1931, &CIE_OBSERVER_10DEG_1964] {
3031
let mut sum = ThreeSum::new();
3132
for wavelength in observer.range() {
32-
sum += observer.at(wavelength).unwrap();
33+
assert_eq!(*observer.at(wavelength), observer[wavelength]);
34+
sum += observer[wavelength];
3335
}
3436
assert_eq!(sum.value(), observer.checksum());
3537
}
@@ -60,11 +62,12 @@ mod test {
6062
// Check results of range overlap computation
6163
assert_eq!(table.start(), 360);
6264
assert_eq!(table.end(), 831);
65+
assert_eq!(table.range(), table.start()..table.end());
6366

6467
// Compute sum for tristimulus
6568
let mut sum = ThreeSum::new();
66-
for index in table.start()..table.end() {
67-
sum += table.at(index).unwrap();
69+
for index in table.range() {
70+
sum += table[index];
6871
}
6972

7073
// Scale to tristimulus and compare to expected value

crates/prettypretty/src/core/conversion.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub(crate) fn to_24bit(space: ColorSpace, coordinates: &[Float; 3]) -> [u8; 3] {
2828
/// new 3-element vector.
2929
#[inline]
3030
fn multiply(matrix: &[[Float; 3]; 3], vector: &[Float; 3]) -> [Float; 3] {
31-
let [row1, row2, row3] = matrix;
31+
let [ref row1, ref row2, ref row3] = *matrix;
3232

3333
[
3434
row1[0].mul_add(vector[0], row1[1].mul_add(vector[1], row1[2] * vector[2])),
@@ -175,7 +175,7 @@ mod rec2020 {
175175
if value < BETA {
176176
value * 4.5
177177
} else {
178-
ALPHA * value.powf(0.45) - (ALPHA - 1.0)
178+
ALPHA.mul_add(value.powf(0.45), -(ALPHA - 1.0))
179179
}
180180
}
181181

crates/prettypretty/src/core/difference.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use crate::Float;
77
/// Compute Delta-E for Oklab or Oklrab.
88
#[allow(non_snake_case)]
99
pub(crate) fn delta_e_ok(coordinates1: &[Float; 3], coordinates2: &[Float; 3]) -> Float {
10-
let [L1, a1, b1] = coordinates1;
11-
let [L2, a2, b2] = coordinates2;
10+
let [ref L1, ref a1, ref b1] = *coordinates1;
11+
let [ref L2, ref a2, ref b2] = *coordinates2;
1212

1313
let ΔL = L1 - L2;
1414
let Δa = a1 - a2;
@@ -229,8 +229,8 @@ pub(crate) fn interpolate(
229229
coordinates2: &[Float; 3],
230230
) -> [Float; 3] {
231231
[
232-
coordinates1[0] + fraction * (coordinates2[0] - coordinates1[0]),
233-
coordinates1[1] + fraction * (coordinates2[1] - coordinates1[1]),
234-
coordinates1[2] + fraction * (coordinates2[2] - coordinates1[2]),
232+
fraction.mul_add(coordinates2[0] - coordinates1[0], coordinates1[0]),
233+
fraction.mul_add(coordinates2[1] - coordinates1[1], coordinates1[1]),
234+
fraction.mul_add(coordinates2[2] - coordinates1[2], coordinates1[2]),
235235
]
236236
}

crates/prettypretty/src/core/gamut.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub(crate) fn in_gamut(space: ColorSpace, coordinates: &[Float; 3]) -> bool {
1919
/// Clip the coordinates to the gamut of their color space.
2020
pub(crate) fn clip(space: ColorSpace, coordinates: &[Float; 3]) -> [Float; 3] {
2121
if space.is_rgb() {
22-
let [r, g, b] = coordinates;
22+
let [ref r, ref g, ref b] = *coordinates;
2323
[r.clamp(0.0, 1.0), g.clamp(0.0, 1.0), b.clamp(0.0, 1.0)]
2424
} else {
2525
*coordinates
@@ -81,6 +81,7 @@ pub(crate) fn to_gamut(space: ColorSpace, coordinates: &[Float; 3]) -> [Float; 3
8181
let mut max = origin_as_oklch[1];
8282
let mut min_in_gamut = true;
8383

84+
#[allow(clippy::while_float)]
8485
while EPSILON < max - min {
8586
let chroma = (min + max) / 2.0;
8687
current_as_oklch = [current_as_oklch[0], chroma, current_as_oklch[2]];
@@ -142,17 +143,17 @@ pub enum GamutTraversalStep {
142143
impl GamutTraversalStep {
143144
/// Get this step's color.
144145
pub fn color(&self) -> Color {
145-
match self {
146-
Self::MoveTo(color) => color.clone(),
147-
Self::LineTo(color) => color.clone(),
148-
Self::CloseWith(color) => color.clone(),
146+
match *self {
147+
Self::MoveTo(ref color) => color.clone(),
148+
Self::LineTo(ref color) => color.clone(),
149+
Self::CloseWith(ref color) => color.clone(),
149150
}
150151
}
151152

152153
/// Get a debug representation. <i class=python-only>Python only!</i>
153154
#[cfg(feature = "pyffi")]
154155
pub fn __repr__(&self) -> String {
155-
match self {
156+
match *self {
156157
Self::MoveTo(ref color) => format!("GamutTraversalStep.MoveTo({:?}", color),
157158
Self::LineTo(ref color) => format!("GamutTraversalStep.LineTo({:?}", color),
158159
Self::CloseWith(ref color) => format!("GamutTraversalStep.CloseWith({:?}", color),
@@ -448,14 +449,14 @@ impl Iterator for GamutTraversal {
448449
}
449450

450451
#[cfg(feature = "gamut")]
451-
impl std::iter::ExactSizeIterator for GamutTraversal {
452+
impl ExactSizeIterator for GamutTraversal {
452453
fn len(&self) -> usize {
453454
self.remaining
454455
}
455456
}
456457

457458
#[cfg(feature = "gamut")]
458-
impl std::iter::FusedIterator for GamutTraversal {}
459+
impl core::iter::FusedIterator for GamutTraversal {}
459460

460461
#[cfg(all(feature = "gamut", feature = "pyffi"))]
461462
#[pymethods]

crates/prettypretty/src/core/math.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub(crate) mod sum {
5050
}
5151
}
5252

53-
impl std::ops::Add<Float> for Sum {
53+
impl core::ops::Add<Float> for Sum {
5454
type Output = Sum;
5555

5656
/// Add a number to this sum.
@@ -64,7 +64,7 @@ pub(crate) mod sum {
6464
}
6565
}
6666

67-
impl std::ops::AddAssign<Float> for Sum {
67+
impl core::ops::AddAssign<Float> for Sum {
6868
fn add_assign(&mut self, rhs: Float) {
6969
let t = self.sum + rhs;
7070
if rhs.abs() < self.sum.abs() {
@@ -76,7 +76,7 @@ pub(crate) mod sum {
7676
}
7777
}
7878

79-
impl std::iter::Sum<Float> for Sum {
79+
impl core::iter::Sum<Float> for Sum {
8080
fn sum<I: Iterator<Item = Float>>(iter: I) -> Self {
8181
let mut sum = Sum::new();
8282
for num in iter {
@@ -113,7 +113,7 @@ pub(crate) mod sum {
113113
}
114114
}
115115

116-
impl std::ops::Add<[Float; 3]> for ThreeSum {
116+
impl core::ops::Add<[Float; 3]> for ThreeSum {
117117
type Output = ThreeSum;
118118

119119
fn add(self, rhs: [Float; 3]) -> Self::Output {
@@ -123,7 +123,7 @@ pub(crate) mod sum {
123123
}
124124
}
125125

126-
impl std::ops::AddAssign<[Float; 3]> for ThreeSum {
126+
impl core::ops::AddAssign<[Float; 3]> for ThreeSum {
127127
fn add_assign(&mut self, rhs: [Float; 3]) {
128128
let [a, b, c] = rhs;
129129
self.a += a;
@@ -132,7 +132,7 @@ pub(crate) mod sum {
132132
}
133133
}
134134

135-
impl std::iter::Sum<[Float; 3]> for ThreeSum {
135+
impl core::iter::Sum<[Float; 3]> for ThreeSum {
136136
fn sum<I: Iterator<Item = [Float; 3]>>(iter: I) -> Self {
137137
let mut sum = ThreeSum::new();
138138
for triple in iter {

crates/prettypretty/src/core/space.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl ColorSpace {
127127

128128
/// Determine whether this color space is XYZ.
129129
pub const fn is_xyz(&self) -> bool {
130-
matches!(self, Self::Xyz)
130+
matches!(self, &Self::Xyz)
131131
}
132132

133133
/// Determine whether this color space is RGB.
@@ -187,11 +187,11 @@ impl ColorSpace {
187187
}
188188
}
189189

190-
impl std::fmt::Display for ColorSpace {
191-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
190+
impl core::fmt::Display for ColorSpace {
191+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
192192
use ColorSpace::*;
193193

194-
let s = match self {
194+
let s = match *self {
195195
Srgb => "sRGB",
196196
LinearSrgb => "linear sRGB",
197197
DisplayP3 => "Display P3",

0 commit comments

Comments
 (0)