Skip to content

Commit e0d3270

Browse files
authored
bevy_mesh: Apply #![deny(clippy::allow_attributes, clippy::allow_attributes_without_reason)] (#17218)
# Objective - #17111 ## Solution Set the `clippy::allow_attributes` and `clippy::allow_attributes_without_reason` lints to `deny`, and bring `bevy_mesh` in line with the new restrictions. ## Testing `cargo clippy --tests` and `cargo test --package bevy_mesh` were run, and no errors were encountered.
1 parent 2403487 commit e0d3270

File tree

3 files changed

+55
-40
lines changed

3 files changed

+55
-40
lines changed

crates/bevy_mesh/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
#![expect(missing_docs, reason = "Not all docs are written yet, see #3492.")]
2+
#![deny(
3+
clippy::allow_attributes,
4+
clippy::allow_attributes_without_reason,
5+
reason = "See #17111; To be removed once all crates are in-line with these attributes"
6+
)]
27

38
extern crate alloc;
49
extern crate core;

crates/bevy_mesh/src/mesh.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,6 @@ impl Mesh {
491491
///
492492
/// This can dramatically increase the vertex count, so make sure this is what you want.
493493
/// Does nothing if no [Indices] are set.
494-
#[allow(clippy::match_same_arms)]
495494
pub fn duplicate_vertices(&mut self) {
496495
fn duplicate<T: Copy>(values: &[T], indices: impl Iterator<Item = usize>) -> Vec<T> {
497496
indices.map(|i| values[i]).collect()
@@ -503,6 +502,10 @@ impl Mesh {
503502

504503
for attributes in self.attributes.values_mut() {
505504
let indices = indices.iter();
505+
#[expect(
506+
clippy::match_same_arms,
507+
reason = "Although the `vec` binding on some match arms may have different types, each variant has different semantics; thus it's not guaranteed that they will use the same type forever."
508+
)]
506509
match &mut attributes.values {
507510
VertexAttributeValues::Float32(vec) => *vec = duplicate(vec, indices),
508511
VertexAttributeValues::Sint32(vec) => *vec = duplicate(vec, indices),
@@ -789,7 +792,6 @@ impl Mesh {
789792
///
790793
/// Panics if the vertex attribute values of `other` are incompatible with `self`.
791794
/// For example, [`VertexAttributeValues::Float32`] is incompatible with [`VertexAttributeValues::Float32x3`].
792-
#[allow(clippy::match_same_arms)]
793795
pub fn merge(&mut self, other: &Mesh) {
794796
use VertexAttributeValues::*;
795797

@@ -800,6 +802,10 @@ impl Mesh {
800802
for (attribute, values) in self.attributes_mut() {
801803
let enum_variant_name = values.enum_variant_name();
802804
if let Some(other_values) = other.attribute(attribute.id) {
805+
#[expect(
806+
clippy::match_same_arms,
807+
reason = "Although the bindings on some match arms may have different types, each variant has different semantics; thus it's not guaranteed that they will use the same type forever."
808+
)]
803809
match (values, other_values) {
804810
(Float32(vec1), Float32(vec2)) => vec1.extend(vec2),
805811
(Sint32(vec1), Sint32(vec2)) => vec1.extend(vec2),

crates/bevy_mesh/src/vertex.rs

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -170,45 +170,43 @@ pub trait VertexFormatSize {
170170
}
171171

172172
impl VertexFormatSize for VertexFormat {
173-
#[allow(clippy::match_same_arms)]
174173
fn get_size(self) -> u64 {
175-
match self {
176-
VertexFormat::Uint8x2 => 2,
177-
VertexFormat::Uint8x4 => 4,
178-
VertexFormat::Sint8x2 => 2,
179-
VertexFormat::Sint8x4 => 4,
180-
VertexFormat::Unorm8x2 => 2,
181-
VertexFormat::Unorm8x4 => 4,
182-
VertexFormat::Snorm8x2 => 2,
183-
VertexFormat::Snorm8x4 => 4,
184-
VertexFormat::Unorm10_10_10_2 => 4,
185-
VertexFormat::Uint16x2 => 2 * 2,
186-
VertexFormat::Uint16x4 => 2 * 4,
187-
VertexFormat::Sint16x2 => 2 * 2,
188-
VertexFormat::Sint16x4 => 2 * 4,
189-
VertexFormat::Unorm16x2 => 2 * 2,
190-
VertexFormat::Unorm16x4 => 2 * 4,
191-
VertexFormat::Snorm16x2 => 2 * 2,
192-
VertexFormat::Snorm16x4 => 2 * 4,
174+
use core::mem::size_of;
175+
let size = match self {
176+
VertexFormat::Uint8x2 | VertexFormat::Unorm8x2 => size_of::<u8>() * 2,
177+
VertexFormat::Uint8x4 | VertexFormat::Unorm8x4 => size_of::<u8>() * 4,
178+
VertexFormat::Sint8x2 | VertexFormat::Snorm8x2 => size_of::<i8>() * 2,
179+
VertexFormat::Sint8x4 | VertexFormat::Snorm8x4 => size_of::<i8>() * 4,
180+
VertexFormat::Unorm10_10_10_2 => 10 + 10 + 10 + 2,
181+
VertexFormat::Uint16x2 | VertexFormat::Unorm16x2 => size_of::<u16>() * 2,
182+
VertexFormat::Uint16x4 | VertexFormat::Unorm16x4 => size_of::<u16>() * 4,
183+
VertexFormat::Sint16x2 | VertexFormat::Snorm16x2 => size_of::<i16>() * 2,
184+
VertexFormat::Sint16x4 | VertexFormat::Snorm16x4 => size_of::<i16>() * 4,
185+
// NOTE: As of the time of writing this code, `f16` is not a stabilized primitive, so we
186+
// can't use `size_of::<f16>()` here.
193187
VertexFormat::Float16x2 => 2 * 2,
194188
VertexFormat::Float16x4 => 2 * 4,
195-
VertexFormat::Float32 => 4,
196-
VertexFormat::Float32x2 => 4 * 2,
197-
VertexFormat::Float32x3 => 4 * 3,
198-
VertexFormat::Float32x4 => 4 * 4,
199-
VertexFormat::Uint32 => 4,
200-
VertexFormat::Uint32x2 => 4 * 2,
201-
VertexFormat::Uint32x3 => 4 * 3,
202-
VertexFormat::Uint32x4 => 4 * 4,
203-
VertexFormat::Sint32 => 4,
204-
VertexFormat::Sint32x2 => 4 * 2,
205-
VertexFormat::Sint32x3 => 4 * 3,
206-
VertexFormat::Sint32x4 => 4 * 4,
207-
VertexFormat::Float64 => 8,
208-
VertexFormat::Float64x2 => 8 * 2,
209-
VertexFormat::Float64x3 => 8 * 3,
210-
VertexFormat::Float64x4 => 8 * 4,
211-
}
189+
VertexFormat::Float32 => size_of::<f32>(),
190+
VertexFormat::Float32x2 => size_of::<f32>() * 2,
191+
VertexFormat::Float32x3 => size_of::<f32>() * 3,
192+
VertexFormat::Float32x4 => size_of::<f32>() * 4,
193+
VertexFormat::Uint32 => size_of::<u32>(),
194+
VertexFormat::Uint32x2 => size_of::<u32>() * 2,
195+
VertexFormat::Uint32x3 => size_of::<u32>() * 3,
196+
VertexFormat::Uint32x4 => size_of::<u32>() * 4,
197+
VertexFormat::Sint32 => size_of::<i32>(),
198+
VertexFormat::Sint32x2 => size_of::<i32>() * 2,
199+
VertexFormat::Sint32x3 => size_of::<i32>() * 3,
200+
VertexFormat::Sint32x4 => size_of::<i32>() * 4,
201+
VertexFormat::Float64 => size_of::<f64>(),
202+
VertexFormat::Float64x2 => size_of::<f64>() * 2,
203+
VertexFormat::Float64x3 => size_of::<f64>() * 3,
204+
VertexFormat::Float64x4 => size_of::<f64>() * 4,
205+
};
206+
207+
// We can safely cast `size` (a `usize`) into a `u64`, as we don't even reach the limits of
208+
// of a `u8`.
209+
size.try_into().unwrap()
212210
}
213211
}
214212

@@ -249,7 +247,10 @@ pub enum VertexAttributeValues {
249247
impl VertexAttributeValues {
250248
/// Returns the number of vertices in this [`VertexAttributeValues`]. For a single
251249
/// mesh, all of the [`VertexAttributeValues`] must have the same length.
252-
#[allow(clippy::match_same_arms)]
250+
#[expect(
251+
clippy::match_same_arms,
252+
reason = "Although the `values` binding on some match arms may have matching types, each variant has different semantics; thus it's not guaranteed that they will use the same type forever."
253+
)]
253254
pub fn len(&self) -> usize {
254255
match self {
255256
VertexAttributeValues::Float32(values) => values.len(),
@@ -299,7 +300,10 @@ impl VertexAttributeValues {
299300
// TODO: add vertex format as parameter here and perform type conversions
300301
/// Flattens the [`VertexAttributeValues`] into a sequence of bytes. This is
301302
/// useful for serialization and sending to the GPU.
302-
#[allow(clippy::match_same_arms)]
303+
#[expect(
304+
clippy::match_same_arms,
305+
reason = "Although the `values` binding on some match arms may have matching types, each variant has different semantics; thus it's not guaranteed that they will use the same type forever."
306+
)]
303307
pub fn get_bytes(&self) -> &[u8] {
304308
match self {
305309
VertexAttributeValues::Float32(values) => cast_slice(values),

0 commit comments

Comments
 (0)