From f07d790105b5ee22ff8c2a65b2e2948173999539 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferry=20J=C3=A9r=C3=A9mie?= Date: Sat, 28 Dec 2024 09:46:48 +0100 Subject: [PATCH] usage of `cargo clippy --fix --lib -p usvg` with rust 1.83.0 --- crates/usvg/src/parser/clippath.rs | 4 +-- crates/usvg/src/parser/converter.rs | 14 +++----- crates/usvg/src/parser/mask.rs | 4 +-- crates/usvg/src/parser/paint_server.rs | 8 ++--- crates/usvg/src/parser/svgtree/mod.rs | 16 ++++----- crates/usvg/src/parser/svgtree/parse.rs | 20 +++++------ crates/usvg/src/parser/svgtree/text.rs | 48 +++++++++++-------------- crates/usvg/src/parser/use_node.rs | 6 ++-- crates/usvg/src/text/layout.rs | 2 +- crates/usvg/src/tree/geom.rs | 8 ++--- 10 files changed, 51 insertions(+), 79 deletions(-) diff --git a/crates/usvg/src/parser/clippath.rs b/crates/usvg/src/parser/clippath.rs index 03f6b91f..85d182e3 100644 --- a/crates/usvg/src/parser/clippath.rs +++ b/crates/usvg/src/parser/clippath.rs @@ -57,9 +57,7 @@ pub(crate) fn convert( clip_path = convert(link, state, object_bbox, cache); // Linked `clipPath` must be valid. - if clip_path.is_none() { - return None; - } + clip_path.as_ref()?; } let mut id = NonEmptyString::new(node.element_id().to_string())?; diff --git a/crates/usvg/src/parser/converter.rs b/crates/usvg/src/parser/converter.rs index f4da96b3..c5018d79 100644 --- a/crates/usvg/src/parser/converter.rs +++ b/crates/usvg/src/parser/converter.rs @@ -345,10 +345,8 @@ pub(crate) fn convert_doc(svg_doc: &svgtree::Document, opt: &Options) -> Result< | EId::Pattern | EId::RadialGradient | EId::Image - ) { - if !node.element_id().is_empty() { - cache.all_ids.insert(string_hash(node.element_id())); - } + ) && !node.element_id().is_empty() { + cache.all_ids.insert(string_hash(node.element_id())); } } } @@ -736,18 +734,14 @@ pub(crate) fn convert_group( let mut clip_path = None; if let Some(link) = node.attribute::(AId::ClipPath) { clip_path = super::clippath::convert(link, state, object_bbox, cache); - if clip_path.is_none() { - return None; - } + clip_path.as_ref()?; } let mut mask = None; if state.parent_clip_path.is_none() { if let Some(link) = node.attribute::(AId::Mask) { mask = super::mask::convert(link, state, object_bbox, cache); - if mask.is_none() { - return None; - } + mask.as_ref()?; } } diff --git a/crates/usvg/src/parser/mask.rs b/crates/usvg/src/parser/mask.rs index 14c9f5cc..00dadd38 100644 --- a/crates/usvg/src/parser/mask.rs +++ b/crates/usvg/src/parser/mask.rs @@ -86,9 +86,7 @@ pub(crate) fn convert( mask = convert(link, state, object_bbox, cache); // Linked `mask` must be valid. - if mask.is_none() { - return None; - } + mask.as_ref()?; } let kind = if node.attribute(AId::MaskType) == Some("alpha") { diff --git a/crates/usvg/src/parser/paint_server.rs b/crates/usvg/src/parser/paint_server.rs index 5f23a19b..2f92d073 100644 --- a/crates/usvg/src/parser/paint_server.rs +++ b/crates/usvg/src/parser/paint_server.rs @@ -331,7 +331,7 @@ fn convert_stops(grad: SvgNode) -> Vec { if stops.len() >= 3 { let mut i = 0; while i < stops.len() - 2 { - let offset1 = stops[i + 0].offset.get(); + let offset1 = stops[i].offset.get(); let offset2 = stops[i + 1].offset.get(); let offset3 = stops[i + 2].offset.get(); @@ -358,7 +358,7 @@ fn convert_stops(grad: SvgNode) -> Vec { if stops.len() >= 2 { let mut i = 0; while i < stops.len() - 1 { - let offset1 = stops[i + 0].offset.get(); + let offset1 = stops[i].offset.get(); let offset2 = stops[i + 1].offset.get(); if offset1.approx_eq_ulps(&0.0, 4) && offset2.approx_eq_ulps(&0.0, 4) { @@ -384,14 +384,14 @@ fn convert_stops(grad: SvgNode) -> Vec { let mut i = 1; while i < stops.len() { let offset1 = stops[i - 1].offset.get(); - let offset2 = stops[i - 0].offset.get(); + let offset2 = stops[i].offset.get(); // Next offset must be smaller then previous. if offset1 > offset2 || offset1.approx_eq_ulps(&offset2, 4) { // Make previous offset a bit smaller. let new_offset = offset1 - f32::EPSILON; stops[i - 1].offset = StopOffset::new_clamped(new_offset); - stops[i - 0].offset = StopOffset::new_clamped(offset1); + stops[i].offset = StopOffset::new_clamped(offset1); } i += 1; diff --git a/crates/usvg/src/parser/svgtree/mod.rs b/crates/usvg/src/parser/svgtree/mod.rs index 30308dcb..00504aaf 100644 --- a/crates/usvg/src/parser/svgtree/mod.rs +++ b/crates/usvg/src/parser/svgtree/mod.rs @@ -362,17 +362,15 @@ impl<'a, 'input: 'a> SvgNode<'a, 'input> { } None + } else if self.has_attribute(aid) { + Some(*self) } else { - if self.has_attribute(aid) { - Some(*self) + // Non-inheritable attributes can inherit a value only from a direct parent. + let n = self.parent_element()?; + if n.has_attribute(aid) { + Some(n) } else { - // Non-inheritable attributes can inherit a value only from a direct parent. - let n = self.parent_element()?; - if n.has_attribute(aid) { - Some(n) - } else { - None - } + None } } } diff --git a/crates/usvg/src/parser/svgtree/parse.rs b/crates/usvg/src/parser/svgtree/parse.rs index aa116f28..b3020708 100644 --- a/crates/usvg/src/parser/svgtree/parse.rs +++ b/crates/usvg/src/parser/svgtree/parse.rs @@ -344,18 +344,14 @@ pub(crate) fn parse_svg_element<'input>( insert_attribute(AId::FontVariantPosition, "normal", imp); // Then, we set the properties that have been declared. - shorthand - .font_stretch - .map(|s| insert_attribute(AId::FontStretch, s, imp)); - shorthand - .font_weight - .map(|s| insert_attribute(AId::FontWeight, s, imp)); - shorthand - .font_variant - .map(|s| insert_attribute(AId::FontVariant, s, imp)); - shorthand - .font_style - .map(|s| insert_attribute(AId::FontStyle, s, imp)); + if let Some(s) = shorthand + .font_stretch { insert_attribute(AId::FontStretch, s, imp) } + if let Some(s) = shorthand + .font_weight { insert_attribute(AId::FontWeight, s, imp) } + if let Some(s) = shorthand + .font_variant { insert_attribute(AId::FontVariant, s, imp) } + if let Some(s) = shorthand + .font_style { insert_attribute(AId::FontStyle, s, imp) } insert_attribute(AId::FontSize, shorthand.font_size, imp); insert_attribute(AId::FontFamily, shorthand.font_family, imp); } else { diff --git a/crates/usvg/src/parser/svgtree/text.rs b/crates/usvg/src/parser/svgtree/text.rs index 11877da3..f08d93c2 100644 --- a/crates/usvg/src/parser/svgtree/text.rs +++ b/crates/usvg/src/parser/svgtree/text.rs @@ -19,16 +19,14 @@ pub(crate) fn parse_svg_text_element<'input>( let space = if doc.get(parent_id).has_attribute(AId::Space) { get_xmlspace(doc, parent_id, XmlSpace::Default) + } else if let Some(node) = doc + .get(parent_id) + .ancestors() + .find(|n| n.has_attribute(AId::Space)) + { + get_xmlspace(doc, node.id, XmlSpace::Default) } else { - if let Some(node) = doc - .get(parent_id) - .ancestors() - .find(|n| n.has_attribute(AId::Space)) - { - get_xmlspace(doc, node.id, XmlSpace::Default) - } else { - XmlSpace::Default - } + XmlSpace::Default }; parse_svg_text_element_impl(parent, parent_id, style_sheet, space, doc)?; @@ -260,27 +258,21 @@ fn trim_text_nodes(text_elem_id: NodeId, xmlspace: XmlSpace, doc: &mut Document) // // See text-tspan-02-b.svg for details. if depth1 < depth2 { - if c3 == Some(b' ') { - if xmlspace2 == XmlSpace::Default { - if let NodeKind::Text(ref mut text) = doc.nodes[node2_id.get_usize()].kind { - text.remove_first_space(); - } + if c3 == Some(b' ') && xmlspace2 == XmlSpace::Default { + if let NodeKind::Text(ref mut text) = doc.nodes[node2_id.get_usize()].kind { + text.remove_first_space(); } } - } else { - if c2 == Some(b' ') && c2 == c3 { - if xmlspace1 == XmlSpace::Default && xmlspace2 == XmlSpace::Default { - if let NodeKind::Text(ref mut text) = doc.nodes[node1_id.get_usize()].kind { - text.remove_last_space(); - } - } else { - if xmlspace1 == XmlSpace::Preserve && xmlspace2 == XmlSpace::Default { - if let NodeKind::Text(ref mut text) = - doc.nodes[node2_id.get_usize()].kind - { - text.remove_first_space(); - } - } + } else if c2 == Some(b' ') && c2 == c3 { + if xmlspace1 == XmlSpace::Default && xmlspace2 == XmlSpace::Default { + if let NodeKind::Text(ref mut text) = doc.nodes[node1_id.get_usize()].kind { + text.remove_last_space(); + } + } else if xmlspace1 == XmlSpace::Preserve && xmlspace2 == XmlSpace::Default { + if let NodeKind::Text(ref mut text) = + doc.nodes[node2_id.get_usize()].kind + { + text.remove_first_space(); } } } diff --git a/crates/usvg/src/parser/use_node.rs b/crates/usvg/src/parser/use_node.rs index 74e540f4..38ce92d5 100644 --- a/crates/usvg/src/parser/use_node.rs +++ b/crates/usvg/src/parser/use_node.rs @@ -308,10 +308,8 @@ fn get_clip_rect( // should not be clipped. if use_node.tag_name() == Some(EId::Svg) { // Nested `svg` referenced by `use` still should be clipped, but by `use` bounds. - if state.use_size.0.is_none() && state.use_size.1.is_none() { - if !(use_node.has_attribute(AId::Width) && use_node.has_attribute(AId::Height)) { - return None; - } + if state.use_size.0.is_none() && state.use_size.1.is_none() && !(use_node.has_attribute(AId::Width) && use_node.has_attribute(AId::Height)) { + return None; } } diff --git a/crates/usvg/src/text/layout.rs b/crates/usvg/src/text/layout.rs index 3e9ced0e..f096caca 100644 --- a/crates/usvg/src/text/layout.rs +++ b/crates/usvg/src/text/layout.rs @@ -1418,7 +1418,7 @@ fn shape_text_with_font( glyphs.push(Glyph { byte_idx: ByteIndex::new(idx), - cluster_len: end.checked_sub(start).unwrap_or(0), // TODO: can fail? + cluster_len: end.saturating_sub(start), // TODO: can fail? text: sub_text[start..end].to_string(), id: GlyphId(info.glyph_id as u16), dx: pos.x_offset, diff --git a/crates/usvg/src/tree/geom.rs b/crates/usvg/src/tree/geom.rs index 32ad8e79..93b61491 100644 --- a/crates/usvg/src/tree/geom.rs +++ b/crates/usvg/src/tree/geom.rs @@ -70,12 +70,10 @@ impl ViewBox { } else { sx } + } else if sx > sy { + sy } else { - if sx > sy { - sy - } else { - sx - } + sx }; (s, s)