Skip to content

Commit 9429cb9

Browse files
authored
Merge pull request #2270 from image-rs/clippy
A bunch of Clippy fixes
2 parents 4589a4a + 0aa70f9 commit 9429cb9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+432
-356
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "image"
3-
version = "0.25.1"
3+
version = "0.25.2"
44
edition = "2021"
55
resolver = "2"
66

benches/copy_from.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub fn bench_copy_from(c: &mut Criterion) {
66
let mut dst = ImageBuffer::from_pixel(2048, 2048, Rgba([0u8, 0, 0, 255]));
77

88
c.bench_function("copy_from", |b| {
9-
b.iter(|| dst.copy_from(black_box(&src), 0, 0))
9+
b.iter(|| dst.copy_from(black_box(&src), 0, 0));
1010
});
1111
}
1212

benches/decode.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ fn bench_load(c: &mut Criterion, def: &BenchDef) {
9696
group.bench_function(file_name.to_owned(), |b| {
9797
b.iter(|| {
9898
image::load_from_memory_with_format(&buf, def.format).unwrap();
99-
})
99+
});
100100
});
101101
}
102102
}

benches/encode.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn encode_all(c: &mut Criterion) {
3838
];
3939

4040
for definition in BENCH_DEFS {
41-
encode_definition(c, definition)
41+
encode_definition(c, definition);
4242
}
4343
}
4444

@@ -55,7 +55,7 @@ fn encode_zeroed(group: &mut BenchGroup, with: &dyn Encoder, size: u32, color: E
5555
let im = vec![0; (color.bits_per_pixel() as usize * size as usize + 7) / 8 * size as usize];
5656

5757
group.bench_with_input(
58-
BenchmarkId::new(format!("zero-{:?}-rawvec", color), size),
58+
BenchmarkId::new(format!("zero-{color:?}-rawvec"), size),
5959
&im,
6060
|b, image| {
6161
let mut v = vec![];
@@ -64,7 +64,7 @@ fn encode_zeroed(group: &mut BenchGroup, with: &dyn Encoder, size: u32, color: E
6464
},
6565
);
6666
group.bench_with_input(
67-
BenchmarkId::new(format!("zero-{:?}-bufvec", color), size),
67+
BenchmarkId::new(format!("zero-{color:?}-bufvec"), size),
6868
&im,
6969
|b, image| {
7070
let mut v = vec![];
@@ -73,7 +73,7 @@ fn encode_zeroed(group: &mut BenchGroup, with: &dyn Encoder, size: u32, color: E
7373
},
7474
);
7575
group.bench_with_input(
76-
BenchmarkId::new(format!("zero-{:?}-file", color), size),
76+
BenchmarkId::new(format!("zero-{color:?}-file"), size),
7777
&im,
7878
|b, image| {
7979
let file = File::create("temp.bmp").unwrap();

examples/concat/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use image::{GenericImage, GenericImageView, ImageBuffer, Pixel, Primitive};
22

33
/// Example showcasing a generic implementation of image concatenation.
44
///
5-
/// The example images are coming from https://placeholder.com/
5+
/// The example images are coming from <https://placeholder.com>/
66
///
77
/// Run from the root of the repository with:
88
/// cargo run --release --example concat

examples/opening.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn main() {
2424
// The color method returns the image's ColorType
2525
println!("{:?}", im.color());
2626

27-
let fout = &mut File::create(Path::new(&format!("{}.png", file))).unwrap();
27+
let fout = &mut File::create(Path::new(&format!("{file}.png"))).unwrap();
2828

2929
// Write the contents of this image to the Writer in PNG format.
3030
im.write_to(fout, ImageFormat::Png).unwrap();

examples/scaledown/main.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,36 @@ impl Elapsed {
1515
impl fmt::Display for Elapsed {
1616
fn fmt(&self, out: &mut fmt::Formatter) -> Result<(), fmt::Error> {
1717
match (self.0.as_secs(), self.0.subsec_nanos()) {
18-
(0, n) if n < 1000 => write!(out, "{} ns", n),
18+
(0, n) if n < 1000 => write!(out, "{n} ns"),
1919
(0, n) if n < 1_000_000 => write!(out, "{} µs", n / 1000),
2020
(0, n) => write!(out, "{} ms", n / 1_000_000),
2121
(s, n) if s < 10 => write!(out, "{}.{:02} s", s, n / 10_000_000),
22-
(s, _) => write!(out, "{} s", s),
22+
(s, _) => write!(out, "{s} s"),
2323
}
2424
}
2525
}
2626

2727
fn main() {
2828
let img = image::open("examples/scaledown/test.jpg").unwrap();
29-
for &(name, filter) in [
29+
for &(name, filter) in &[
3030
("near", FilterType::Nearest),
3131
("tri", FilterType::Triangle),
3232
("cmr", FilterType::CatmullRom),
3333
("gauss", FilterType::Gaussian),
3434
("lcz2", FilterType::Lanczos3),
35-
]
36-
.iter()
37-
{
35+
] {
3836
let timer = Instant::now();
3937
let scaled = img.resize(400, 400, filter);
4038
println!("Scaled by {} in {}", name, Elapsed::from(&timer));
41-
let mut output = File::create(&format!("test-{}.png", name)).unwrap();
39+
let mut output = File::create(&format!("test-{name}.png")).unwrap();
4240
scaled.write_to(&mut output, ImageFormat::Png).unwrap();
4341
}
4442

4543
for size in &[20_u32, 40, 100, 200, 400] {
4644
let timer = Instant::now();
4745
let scaled = img.thumbnail(*size, *size);
4846
println!("Thumbnailed to {} in {}", size, Elapsed::from(&timer));
49-
let mut output = File::create(format!("test-thumb{}.png", size)).unwrap();
47+
let mut output = File::create(format!("test-thumb{size}.png")).unwrap();
5048
scaled.write_to(&mut output, ImageFormat::Png).unwrap();
5149
}
5250
}

examples/scaleup/main.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,34 @@ impl Elapsed {
1515
impl fmt::Display for Elapsed {
1616
fn fmt(&self, out: &mut fmt::Formatter) -> Result<(), fmt::Error> {
1717
match (self.0.as_secs(), self.0.subsec_nanos()) {
18-
(0, n) if n < 1000 => write!(out, "{} ns", n),
18+
(0, n) if n < 1000 => write!(out, "{n} ns"),
1919
(0, n) if n < 1_000_000 => write!(out, "{} µs", n / 1000),
2020
(0, n) => write!(out, "{} ms", n / 1_000_000),
2121
(s, n) if s < 10 => write!(out, "{}.{:02} s", s, n / 10_000_000),
22-
(s, _) => write!(out, "{} s", s),
22+
(s, _) => write!(out, "{s} s"),
2323
}
2424
}
2525
}
2626

2727
fn main() {
2828
let tiny = image::open("examples/scaleup/tinycross.png").unwrap();
29-
for &(name, filter) in [
29+
for &(name, filter) in &[
3030
("near", FilterType::Nearest),
3131
("tri", FilterType::Triangle),
3232
("xcmr", FilterType::CatmullRom),
3333
("ygauss", FilterType::Gaussian),
3434
("zlcz2", FilterType::Lanczos3),
35-
]
36-
.iter()
37-
{
35+
] {
3836
let timer = Instant::now();
3937
let scaled = tiny.resize(32, 32, filter);
4038
println!("Scaled by {} in {}", name, Elapsed::from(&timer));
41-
let mut output = File::create(&format!("up2-{}.png", name)).unwrap();
39+
let mut output = File::create(&format!("up2-{name}.png")).unwrap();
4240
scaled.write_to(&mut output, ImageFormat::Png).unwrap();
4341

4442
let timer = Instant::now();
4543
let scaled = tiny.resize(48, 48, filter);
4644
println!("Scaled by {} in {}", name, Elapsed::from(&timer));
47-
let mut output = File::create(&format!("up3-{}.png", name)).unwrap();
45+
let mut output = File::create(&format!("up3-{name}.png")).unwrap();
4846
scaled.write_to(&mut output, ImageFormat::Png).unwrap();
4947
}
5048
}

src/animation.rs

+11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub struct Frames<'a> {
1111

1212
impl<'a> Frames<'a> {
1313
/// Creates a new `Frames` from an implementation specific iterator.
14+
#[must_use]
1415
pub fn new(iterator: Box<dyn Iterator<Item = ImageResult<Frame>> + 'a>) -> Self {
1516
Frames { iterator }
1617
}
@@ -69,6 +70,7 @@ pub struct Delay {
6970

7071
impl Frame {
7172
/// Constructs a new frame without any delay.
73+
#[must_use]
7274
pub fn new(buffer: RgbaImage) -> Frame {
7375
Frame {
7476
delay: Delay::from_ratio(Ratio { numer: 0, denom: 1 }),
@@ -79,6 +81,7 @@ impl Frame {
7981
}
8082

8183
/// Constructs a new frame
84+
#[must_use]
8285
pub fn from_parts(buffer: RgbaImage, left: u32, top: u32, delay: Delay) -> Frame {
8386
Frame {
8487
delay,
@@ -89,11 +92,13 @@ impl Frame {
8992
}
9093

9194
/// Delay of this frame
95+
#[must_use]
9296
pub fn delay(&self) -> Delay {
9397
self.delay
9498
}
9599

96100
/// Returns the image buffer
101+
#[must_use]
97102
pub fn buffer(&self) -> &RgbaImage {
98103
&self.buffer
99104
}
@@ -104,16 +109,19 @@ impl Frame {
104109
}
105110

106111
/// Returns the image buffer
112+
#[must_use]
107113
pub fn into_buffer(self) -> RgbaImage {
108114
self.buffer
109115
}
110116

111117
/// Returns the x offset
118+
#[must_use]
112119
pub fn left(&self) -> u32 {
113120
self.left
114121
}
115122

116123
/// Returns the y offset
124+
#[must_use]
117125
pub fn top(&self) -> u32 {
118126
self.top
119127
}
@@ -128,6 +136,7 @@ impl Delay {
128136
/// use image::Delay;
129137
/// let delay_10ms = Delay::from_numer_denom_ms(10, 1);
130138
/// ```
139+
#[must_use]
131140
pub fn from_numer_denom_ms(numerator: u32, denominator: u32) -> Self {
132141
Delay {
133142
ratio: Ratio::new(numerator, denominator),
@@ -148,6 +157,7 @@ impl Delay {
148157
/// let duration = Duration::from_millis(20);
149158
/// let delay = Delay::from_saturating_duration(duration);
150159
/// ```
160+
#[must_use]
151161
pub fn from_saturating_duration(duration: Duration) -> Self {
152162
// A few notes: The largest number we can represent as a ratio is u32::MAX but we can
153163
// sometimes represent much smaller numbers.
@@ -177,6 +187,7 @@ impl Delay {
177187
///
178188
/// This is guaranteed to be an exact conversion if the `Delay` was previously created with the
179189
/// `from_numer_denom_ms` constructor.
190+
#[must_use]
180191
pub fn numer_denom_ms(self) -> (u32, u32) {
181192
(self.ratio.numer, self.ratio.denom)
182193
}

src/buffer.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ where
644644
/// image::imageops::overlay(&mut img, &on_top, 128, 128);
645645
/// ```
646646
///
647-
/// Convert an RgbaImage to a GrayImage.
647+
/// Convert an `RgbaImage` to a `GrayImage`.
648648
///
649649
/// ```no_run
650650
/// use image::{open, DynamicImage};
@@ -801,7 +801,7 @@ where
801801
/// the bounds will not overflow.
802802
fn check_image_fits(width: u32, height: u32, len: usize) -> bool {
803803
let checked_len = Self::image_buffer_len(width, height);
804-
checked_len.map(|min_len| min_len <= len).unwrap_or(false)
804+
checked_len.map_or(false, |min_len| min_len <= len)
805805
}
806806

807807
fn image_buffer_len(width: u32, height: u32) -> Option<usize> {
@@ -980,7 +980,7 @@ where
980980
#[inline]
981981
#[track_caller]
982982
pub fn put_pixel(&mut self, x: u32, y: u32, pixel: P) {
983-
*self.get_pixel_mut(x, y) = pixel
983+
*self.get_pixel_mut(x, y) = pixel;
984984
}
985985
}
986986

@@ -1198,22 +1198,22 @@ where
11981198
}
11991199

12001200
fn put_pixel(&mut self, x: u32, y: u32, pixel: P) {
1201-
*self.get_pixel_mut(x, y) = pixel
1201+
*self.get_pixel_mut(x, y) = pixel;
12021202
}
12031203

12041204
/// Puts a pixel at location (x, y), ignoring bounds checking.
12051205
#[inline(always)]
12061206
unsafe fn unsafe_put_pixel(&mut self, x: u32, y: u32, pixel: P) {
12071207
let indices = self.pixel_indices_unchecked(x, y);
12081208
let p = <P as Pixel>::from_slice_mut(self.data.get_unchecked_mut(indices));
1209-
*p = pixel
1209+
*p = pixel;
12101210
}
12111211

12121212
/// Put a pixel at location (x, y), taking into account alpha channels
12131213
///
12141214
/// DEPRECATED: This method will be removed. Blend the pixel directly instead.
12151215
fn blend_pixel(&mut self, x: u32, y: u32, p: P) {
1216-
self.get_pixel_mut(x, y).blend(&p)
1216+
self.get_pixel_mut(x, y).blend(&p);
12171217
}
12181218

12191219
fn copy_within(&mut self, source: Rect, x: u32, y: u32) -> bool {
@@ -1268,6 +1268,7 @@ impl<P: Pixel> ImageBuffer<P, Vec<P::Subpixel>> {
12681268
/// # Panics
12691269
///
12701270
/// Panics when the resulting image is larger than the maximum size of a vector.
1271+
#[must_use]
12711272
pub fn new(width: u32, height: u32) -> ImageBuffer<P, Vec<P::Subpixel>> {
12721273
let size = Self::image_buffer_len(width, height)
12731274
.expect("Buffer length in `ImageBuffer::new` overflows usize");
@@ -1279,20 +1280,20 @@ impl<P: Pixel> ImageBuffer<P, Vec<P::Subpixel>> {
12791280
}
12801281
}
12811282

1282-
/// Constructs a new ImageBuffer by copying a pixel
1283+
/// Constructs a new `ImageBuffer` by copying a pixel
12831284
///
12841285
/// # Panics
12851286
///
12861287
/// Panics when the resulting image is larger the the maximum size of a vector.
12871288
pub fn from_pixel(width: u32, height: u32, pixel: P) -> ImageBuffer<P, Vec<P::Subpixel>> {
12881289
let mut buf = ImageBuffer::new(width, height);
12891290
for p in buf.pixels_mut() {
1290-
*p = pixel
1291+
*p = pixel;
12911292
}
12921293
buf
12931294
}
12941295

1295-
/// Constructs a new ImageBuffer by repeated application of the supplied function.
1296+
/// Constructs a new `ImageBuffer` by repeated application of the supplied function.
12961297
///
12971298
/// The arguments to the function are the pixel's x and y coordinates.
12981299
///
@@ -1305,13 +1306,14 @@ impl<P: Pixel> ImageBuffer<P, Vec<P::Subpixel>> {
13051306
{
13061307
let mut buf = ImageBuffer::new(width, height);
13071308
for (x, y, p) in buf.enumerate_pixels_mut() {
1308-
*p = f(x, y)
1309+
*p = f(x, y);
13091310
}
13101311
buf
13111312
}
13121313

13131314
/// Creates an image buffer out of an existing buffer.
13141315
/// Returns None if the buffer is not big enough.
1316+
#[must_use]
13151317
pub fn from_vec(
13161318
width: u32,
13171319
height: u32,
@@ -1322,6 +1324,7 @@ impl<P: Pixel> ImageBuffer<P, Vec<P::Subpixel>> {
13221324

13231325
/// Consumes the image buffer and returns the underlying data
13241326
/// as an owned buffer
1327+
#[must_use]
13251328
pub fn into_vec(self) -> Vec<P::Subpixel> {
13261329
self.into_raw()
13271330
}
@@ -1341,6 +1344,7 @@ impl GrayImage {
13411344
/// Expands a color palette by re-using the existing buffer.
13421345
/// Assumes 8 bit per pixel. Uses an optionally transparent index to
13431346
/// adjust it's alpha value accordingly.
1347+
#[must_use]
13441348
pub fn expand_palette(
13451349
self,
13461350
palette: &[(u8, u8, u8)],
@@ -1397,7 +1401,7 @@ where
13971401
let mut buffer: ImageBuffer<ToType, Vec<ToType::Subpixel>> =
13981402
ImageBuffer::new(self.width, self.height);
13991403
for (to, from) in buffer.pixels_mut().zip(self.pixels()) {
1400-
to.from_color(from)
1404+
to.from_color(from);
14011405
}
14021406
buffer
14031407
}

src/buffer_par.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ where
377377
P: Pixel + Send + Sync,
378378
P::Subpixel: Send + Sync,
379379
{
380-
/// Constructs a new ImageBuffer by repeated application of the supplied function,
380+
/// Constructs a new `ImageBuffer` by repeated application of the supplied function,
381381
/// utilizing multi-threading via `rayon`.
382382
///
383383
/// The arguments to the function are the pixel's x and y coordinates.

0 commit comments

Comments
 (0)