Skip to content

Commit 6df6351

Browse files
Misc clippy fixes (#872)
* `clippy::bool_assert_comparison` * `clippy::field_reassign_with_default` * `clippy::legacy_numeric_constants` * `clippy::manual_clamp` * `clippy::map_flatten` * `clippy::needless_borrows_for_generic_args` * `clippy::needless_lifetimes` * `clippy::redundant_field_names` * `clippy::to_string_in_format_args` * `clippy::unnecessary_cast` * `clippy::useless_format`
1 parent bf454cc commit 6df6351

File tree

13 files changed

+83
-74
lines changed

13 files changed

+83
-74
lines changed

crates/c-api/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ pub extern "C" fn resvg_options_set_resources_dir(opt: *mut resvg_options, path:
155155
/// Default: 96
156156
#[no_mangle]
157157
pub extern "C" fn resvg_options_set_dpi(opt: *mut resvg_options, dpi: f32) {
158-
cast_opt(opt).dpi = dpi as f32;
158+
cast_opt(opt).dpi = dpi;
159159
}
160160

161161
/// @brief Sets the default font family.

crates/resvg/examples/draw_bboxes.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ fn main() {
1818
1.0
1919
};
2020

21-
let mut opt = usvg::Options::default();
22-
// Get file's absolute directory.
23-
opt.resources_dir = std::fs::canonicalize(&args[1])
24-
.ok()
25-
.and_then(|p| p.parent().map(|p| p.to_path_buf()));
21+
let mut opt = usvg::Options {
22+
// Get file's absolute directory.
23+
resources_dir: std::fs::canonicalize(&args[1])
24+
.ok()
25+
.and_then(|p| p.parent().map(|p| p.to_path_buf())),
26+
..usvg::Options::default()
27+
};
2628

2729
opt.fontdb_mut().load_system_fonts();
2830

@@ -38,8 +40,10 @@ fn main() {
3840
let render_ts = tiny_skia::Transform::from_scale(zoom, zoom);
3941
resvg::render(&tree, render_ts, &mut pixmap.as_mut());
4042

41-
let mut stroke = tiny_skia::Stroke::default();
42-
stroke.width = 1.0 / zoom; // prevent stroke scaling as well
43+
let stroke = tiny_skia::Stroke {
44+
width: 1.0 / zoom, // prevent stroke scaling as well
45+
..tiny_skia::Stroke::default()
46+
};
4347

4448
let mut paint1 = tiny_skia::Paint::default();
4549
paint1.set_color_rgba8(255, 0, 0, 127);

crates/resvg/examples/minimal.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ fn main() {
99
}
1010

1111
let tree = {
12-
let mut opt = usvg::Options::default();
13-
// Get file's absolute directory.
14-
opt.resources_dir = std::fs::canonicalize(&args[1])
15-
.ok()
16-
.and_then(|p| p.parent().map(|p| p.to_path_buf()));
17-
12+
let mut opt = usvg::Options {
13+
// Get file's absolute directory.
14+
resources_dir: std::fs::canonicalize(&args[1])
15+
.ok()
16+
.and_then(|p| p.parent().map(|p| p.to_path_buf())),
17+
..usvg::Options::default()
18+
};
1819
opt.fontdb_mut().load_system_fonts();
1920

2021
let svg_data = std::fs::read(&args[1]).unwrap();

crates/resvg/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ fn parse_args() -> Result<Args, String> {
553553

554554
let style_sheet = match args.style_sheet.as_ref() {
555555
Some(p) => Some(
556-
std::fs::read(&p)
556+
std::fs::read(p)
557557
.ok()
558558
.and_then(|s| std::str::from_utf8(&s).ok().map(|s| s.to_string()))
559559
.ok_or("failed to read stylesheet".to_string())?,

crates/resvg/tests/integration/main.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,16 @@ pub fn render(name: &str) -> usize {
3333
let svg_path = format!("tests/{}.svg", name);
3434
let png_path = format!("tests/{}.png", name);
3535

36-
let mut opt = usvg::Options::default();
37-
opt.resources_dir = Some(
38-
std::path::PathBuf::from(&svg_path)
39-
.parent()
40-
.unwrap()
41-
.to_owned(),
42-
);
43-
opt.fontdb = GLOBAL_FONTDB.clone();
36+
let opt = usvg::Options {
37+
resources_dir: Some(
38+
std::path::PathBuf::from(&svg_path)
39+
.parent()
40+
.unwrap()
41+
.to_owned(),
42+
),
43+
fontdb: GLOBAL_FONTDB.clone(),
44+
..usvg::Options::default()
45+
};
4446

4547
let tree = {
4648
let svg_data = std::fs::read(&svg_path).unwrap();
@@ -98,8 +100,10 @@ pub fn render_extra_with_scale(name: &str, scale: f32) -> usize {
98100
let svg_path = format!("tests/{}.svg", name);
99101
let png_path = format!("tests/{}.png", name);
100102

101-
let mut opt = usvg::Options::default();
102-
opt.fontdb = GLOBAL_FONTDB.clone();
103+
let opt = usvg::Options {
104+
fontdb: GLOBAL_FONTDB.clone(),
105+
..usvg::Options::default()
106+
};
103107

104108
let tree = {
105109
let svg_data = std::fs::read(&svg_path).unwrap();
@@ -148,8 +152,10 @@ pub fn render_node(name: &str, id: &str) -> usize {
148152
let svg_path = format!("tests/{}.svg", name);
149153
let png_path = format!("tests/{}.png", name);
150154

151-
let mut opt = usvg::Options::default();
152-
opt.fontdb = GLOBAL_FONTDB.clone();
155+
let opt = usvg::Options {
156+
fontdb: GLOBAL_FONTDB.clone(),
157+
..usvg::Options::default()
158+
};
153159

154160
let tree = {
155161
let svg_data = std::fs::read(&svg_path).unwrap();

crates/usvg/src/main.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ fn main() {
313313
}
314314

315315
if let Err(e) = process(args) {
316-
eprintln!("Error: {}.", e.to_string());
316+
eprintln!("Error: {}.", e);
317317
process::exit(1);
318318
}
319319
}
@@ -326,7 +326,7 @@ fn process(args: Args) -> Result<(), String> {
326326
let svg_from = if in_svg == "-" {
327327
InputFrom::Stdin
328328
} else if in_svg == "-c" {
329-
return Err(format!("-c should be set after input"));
329+
return Err("-c should be set after input".to_string());
330330
} else {
331331
InputFrom::File(in_svg)
332332
};
@@ -456,13 +456,13 @@ fn process(args: Args) -> Result<(), String> {
456456
OutputTo::Stdout => {
457457
io::stdout()
458458
.write_all(s.as_bytes())
459-
.map_err(|_| format!("failed to write to the stdout"))?;
459+
.map_err(|_| "failed to write to the stdout".to_string())?;
460460
}
461461
OutputTo::File(path) => {
462462
let mut f =
463-
File::create(path).map_err(|_| format!("failed to create the output file"))?;
463+
File::create(path).map_err(|_| "failed to create the output file".to_string())?;
464464
f.write_all(s.as_bytes())
465-
.map_err(|_| format!("failed to write to the output file"))?;
465+
.map_err(|_| "failed to write to the output file".to_string())?;
466466
}
467467
}
468468

@@ -476,7 +476,7 @@ fn load_stdin() -> Result<Vec<u8>, String> {
476476

477477
handle
478478
.read_to_end(&mut buf)
479-
.map_err(|_| format!("failed to read from stdin"))?;
479+
.map_err(|_| "failed to read from stdin".to_string())?;
480480

481481
Ok(buf)
482482
}

crates/usvg/src/parser/filter.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub(crate) fn convert(
7575
id: cache.gen_filter_id(),
7676
rect,
7777
primitives: vec![Primitive {
78-
rect: rect,
78+
rect,
7979
// Unlike `filter` elements, filter functions use sRGB colors by default.
8080
color_interpolation: ColorInterpolation::SRGB,
8181
result: "result".to_string(),
@@ -800,8 +800,8 @@ fn convert_std_dev_attr(fe: SvgNode, scale: Size, default: &str) -> (PositiveF32
800800
let std_dev_x = (std_dev_x as f32) * scale.width();
801801
let std_dev_y = (std_dev_y as f32) * scale.height();
802802

803-
let std_dev_x = PositiveF32::new(std_dev_x as f32).unwrap_or(PositiveF32::ZERO);
804-
let std_dev_y = PositiveF32::new(std_dev_y as f32).unwrap_or(PositiveF32::ZERO);
803+
let std_dev_x = PositiveF32::new(std_dev_x).unwrap_or(PositiveF32::ZERO);
804+
let std_dev_y = PositiveF32::new(std_dev_y).unwrap_or(PositiveF32::ZERO);
805805

806806
(std_dev_x, std_dev_y)
807807
}

crates/usvg/src/parser/image.rs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -328,37 +328,35 @@ fn get_image_data_format(data: &[u8]) -> Option<ImageFormat> {
328328
/// Unlike `Tree::from_*` methods, this one will also remove all `image` elements
329329
/// from the loaded SVG, as required by the spec.
330330
pub(crate) fn load_sub_svg(data: &[u8], opt: &Options) -> Option<ImageKind> {
331-
let mut sub_opt = Options::default();
332-
sub_opt.resources_dir = None;
333-
sub_opt.dpi = opt.dpi;
334-
sub_opt.font_size = opt.font_size;
335-
sub_opt.languages = opt.languages.clone();
336-
sub_opt.shape_rendering = opt.shape_rendering;
337-
sub_opt.text_rendering = opt.text_rendering;
338-
sub_opt.image_rendering = opt.image_rendering;
339-
sub_opt.default_size = opt.default_size;
340-
341-
// The referenced SVG image cannot have any 'image' elements by itself.
342-
// Not only recursive. Any. Don't know why.
343-
sub_opt.image_href_resolver = ImageHrefResolver {
344-
resolve_data: Box::new(|_, _, _| None),
345-
resolve_string: Box::new(|_, _| None),
346-
};
347-
348-
#[cfg(feature = "text")]
349-
{
331+
let sub_opt = Options {
332+
resources_dir: None,
333+
dpi: opt.dpi,
334+
font_size: opt.font_size,
335+
languages: opt.languages.clone(),
336+
shape_rendering: opt.shape_rendering,
337+
text_rendering: opt.text_rendering,
338+
image_rendering: opt.image_rendering,
339+
default_size: opt.default_size,
340+
// The referenced SVG image cannot have any 'image' elements by itself.
341+
// Not only recursive. Any. Don't know why.
342+
image_href_resolver: ImageHrefResolver {
343+
resolve_data: Box::new(|_, _, _| None),
344+
resolve_string: Box::new(|_, _| None),
345+
},
350346
// In the referenced SVG, we start with the unmodified user-provided
351347
// fontdb, not the one from the cache.
352-
sub_opt.fontdb = opt.fontdb.clone();
353-
348+
#[cfg(feature = "text")]
349+
fontdb: opt.fontdb.clone(),
354350
// Can't clone the resolver, so we create a new one that forwards to it.
355-
sub_opt.font_resolver = crate::FontResolver {
351+
#[cfg(feature = "text")]
352+
font_resolver: crate::FontResolver {
356353
select_font: Box::new(|font, db| (opt.font_resolver.select_font)(font, db)),
357354
select_fallback: Box::new(|c, used_fonts, db| {
358355
(opt.font_resolver.select_fallback)(c, used_fonts, db)
359356
}),
360-
};
361-
}
357+
},
358+
..Options::default()
359+
};
362360

363361
let tree = Tree::from_data(data, &sub_opt);
364362
let tree = match tree {

crates/usvg/src/parser/style.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,12 @@ fn convert_paint(
160160
svgtypes::Paint::ContextFill => state
161161
.context_element
162162
.clone()
163-
.map(|(f, _)| f)
164-
.flatten()
163+
.and_then(|(f, _)| f)
165164
.map(|f| (f.paint, f.context_element)),
166165
svgtypes::Paint::ContextStroke => state
167166
.context_element
168167
.clone()
169-
.map(|(_, s)| s)
170-
.flatten()
168+
.and_then(|(_, s)| s)
171169
.map(|s| (s.paint, s.context_element)),
172170
svgtypes::Paint::CurrentColor => {
173171
let svg_color: svgtypes::Color = node

crates/usvg/src/parser/svgtree/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ pub(crate) struct NodeId(NonZeroU32);
155155
impl NodeId {
156156
#[inline]
157157
fn new(id: u32) -> Self {
158-
debug_assert!(id < core::u32::MAX);
158+
debug_assert!(id < u32::MAX);
159159

160160
// We are using `NonZeroU32` to reduce overhead of `Option<NodeId>`.
161161
NodeId(NonZeroU32::new(id + 1).unwrap())
@@ -176,7 +176,7 @@ impl From<usize> for NodeId {
176176
#[inline]
177177
fn from(id: usize) -> Self {
178178
// We already checked that `id` is limited by u32::MAX.
179-
debug_assert!(id <= core::u32::MAX as usize);
179+
debug_assert!(id <= u32::MAX as usize);
180180
NodeId::new(id as u32)
181181
}
182182
}

0 commit comments

Comments
 (0)