Skip to content

usage of cargo clippy --fix --lib -p usvg with rust 1.83.0 #876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions crates/usvg/src/parser/clippath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Comment on lines 57 to +60
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is better. If you do want to change this, I'd say:

// Bail out if the linked clip path is invalid
let clip_path_attr = convert(...)?;
clip_path = Some(clip_path_attr);

But I think the original code is fine...

}

let mut id = NonEmptyString::new(node.element_id().to_string())?;
Expand Down
15 changes: 5 additions & 10 deletions crates/usvg/src/parser/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,9 @@ 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()));
}
}
}
Expand Down Expand Up @@ -736,18 +735,14 @@ pub(crate) fn convert_group(
let mut clip_path = None;
if let Some(link) = node.attribute::<SvgNode>(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::<SvgNode>(AId::Mask) {
mask = super::mask::convert(link, state, object_bbox, cache);
if mask.is_none() {
return None;
}
mask.as_ref()?;
}
}

Expand Down
4 changes: 1 addition & 3 deletions crates/usvg/src/parser/mask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
8 changes: 4 additions & 4 deletions crates/usvg/src/parser/paint_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ fn convert_stops(grad: SvgNode) -> Vec<Stop> {
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again... I can see the rationale, but I think the previous version was probably better.

let offset2 = stops[i + 1].offset.get();
let offset3 = stops[i + 2].offset.get();

Expand All @@ -358,7 +358,7 @@ fn convert_stops(grad: SvgNode) -> Vec<Stop> {
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) {
Expand All @@ -384,14 +384,14 @@ fn convert_stops(grad: SvgNode) -> Vec<Stop> {
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;
Expand Down
16 changes: 7 additions & 9 deletions crates/usvg/src/parser/svgtree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down
24 changes: 12 additions & 12 deletions crates/usvg/src/parser/svgtree/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,18 +344,18 @@ 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 {
Expand Down
46 changes: 18 additions & 28 deletions crates/usvg/src/parser/svgtree/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down Expand Up @@ -260,27 +258,19 @@ 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();
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions crates/usvg/src/parser/use_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,11 @@ 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;
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/usvg/src/text/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The semantics are the same, but the meaning this suggests to the reader is different

Copy link
Member

@tomcur tomcur Jan 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know the history of this line, but seems like a runtime panic was too risky. Maybe a debug_assert above the push asserting start <= end would be nice here?

It doesn't trigger in usvg's test cases.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So to be clear, I agree. What I mean by "the meaning this suggests to the reader is different" is that ".unwrap_or" makes it really clear what // TODO: can fail? is referring to, whereas saturating_sub reads like that's the chosen semantics - at least to me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly, that's my reading as well.

text: sub_text[start..end].to_string(),
id: GlyphId(info.glyph_id as u16),
dx: pos.x_offset,
Expand Down
8 changes: 3 additions & 5 deletions crates/usvg/src/tree/geom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,10 @@ impl ViewBox {
} else {
sx
}
} else if sx > sy {
sy
} else {
if sx > sy {
sy
} else {
sx
}
sx
};

(s, s)
Expand Down
Loading