bevy_math: don't allow dead code (#20039)

# Objective

- `bevy_math` allows the `dead_code` lint on some private structs when
`alloc` is not enabled
- allowing lints is not allowed, we should use expect

## Solution

- Don't even compile the code if its expected to be dead instead of
allowing or expecting the lint
This commit is contained in:
François Mockers 2025-07-08 19:46:01 +02:00 committed by GitHub
parent 9b114a4936
commit 9bb41a8309
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,17 +2,13 @@
use { use {
super::{Measured2d, Triangle2d}, super::{Measured2d, Triangle2d},
alloc::{collections::BTreeMap, vec::Vec}, alloc::{collections::BTreeMap, vec::Vec},
core::cmp::Ordering,
}; };
use core::cmp::Ordering;
use crate::Vec2; use crate::Vec2;
#[cfg_attr(
not(feature = "alloc"),
expect(dead_code, reason = "this type is only used with the alloc feature")
)]
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
#[cfg(feature = "alloc")]
enum Endpoint { enum Endpoint {
Left, Left,
Right, Right,
@ -24,22 +20,16 @@ enum Endpoint {
/// If `e1.position().x == e2.position().x` the events are ordered from bottom to top. /// If `e1.position().x == e2.position().x` the events are ordered from bottom to top.
/// ///
/// This is the order expected by the [`SweepLine`]. /// This is the order expected by the [`SweepLine`].
#[cfg(feature = "alloc")]
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
#[cfg_attr(
not(feature = "alloc"),
allow(dead_code, reason = "this type is only used with the alloc feature")
)]
struct SweepLineEvent { struct SweepLineEvent {
segment: Segment, segment: Segment,
/// Type of the vertex (left or right) /// Type of the vertex (left or right)
endpoint: Endpoint, endpoint: Endpoint,
} }
#[cfg(feature = "alloc")]
impl SweepLineEvent { impl SweepLineEvent {
#[cfg_attr(
not(feature = "alloc"),
allow(dead_code, reason = "this type is only used with the alloc feature")
)]
fn position(&self) -> Vec2 { fn position(&self) -> Vec2 {
match self.endpoint { match self.endpoint {
Endpoint::Left => self.segment.left, Endpoint::Left => self.segment.left,
@ -48,20 +38,24 @@ impl SweepLineEvent {
} }
} }
#[cfg(feature = "alloc")]
impl PartialEq for SweepLineEvent { impl PartialEq for SweepLineEvent {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.position() == other.position() self.position() == other.position()
} }
} }
#[cfg(feature = "alloc")]
impl Eq for SweepLineEvent {} impl Eq for SweepLineEvent {}
#[cfg(feature = "alloc")]
impl PartialOrd for SweepLineEvent { impl PartialOrd for SweepLineEvent {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other)) Some(self.cmp(other))
} }
} }
#[cfg(feature = "alloc")]
impl Ord for SweepLineEvent { impl Ord for SweepLineEvent {
fn cmp(&self, other: &Self) -> Ordering { fn cmp(&self, other: &Self) -> Ordering {
xy_order(self.position(), other.position()) xy_order(self.position(), other.position())
@ -69,10 +63,7 @@ impl Ord for SweepLineEvent {
} }
/// Orders 2D points according to the order expected by the sweep line and event queue from -X to +X and then -Y to Y. /// Orders 2D points according to the order expected by the sweep line and event queue from -X to +X and then -Y to Y.
#[cfg_attr( #[cfg(feature = "alloc")]
not(feature = "alloc"),
allow(dead_code, reason = "this type is only used with the alloc feature")
)]
fn xy_order(a: Vec2, b: Vec2) -> Ordering { fn xy_order(a: Vec2, b: Vec2) -> Ordering {
a.x.total_cmp(&b.x).then_with(|| a.y.total_cmp(&b.y)) a.x.total_cmp(&b.x).then_with(|| a.y.total_cmp(&b.y))
} }
@ -129,26 +120,31 @@ impl EventQueue {
/// Segments are ordered from bottom to top based on their left vertices if possible. /// Segments are ordered from bottom to top based on their left vertices if possible.
/// If their y values are identical, the segments are ordered based on the y values of their right vertices. /// If their y values are identical, the segments are ordered based on the y values of their right vertices.
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
#[cfg(feature = "alloc")]
struct Segment { struct Segment {
edge_index: usize, edge_index: usize,
left: Vec2, left: Vec2,
right: Vec2, right: Vec2,
} }
#[cfg(feature = "alloc")]
impl PartialEq for Segment { impl PartialEq for Segment {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.edge_index == other.edge_index self.edge_index == other.edge_index
} }
} }
#[cfg(feature = "alloc")]
impl Eq for Segment {} impl Eq for Segment {}
#[cfg(feature = "alloc")]
impl PartialOrd for Segment { impl PartialOrd for Segment {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other)) Some(self.cmp(other))
} }
} }
#[cfg(feature = "alloc")]
impl Ord for Segment { impl Ord for Segment {
fn cmp(&self, other: &Self) -> Ordering { fn cmp(&self, other: &Self) -> Ordering {
self.left self.left
@ -159,10 +155,7 @@ impl Ord for Segment {
} }
/// Holds information about which segment is above and which is below a given [`Segment`] /// Holds information about which segment is above and which is below a given [`Segment`]
#[cfg_attr( #[cfg(feature = "alloc")]
not(feature = "alloc"),
expect(dead_code, reason = "this type is only used with the alloc feature")
)]
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
struct SegmentOrder { struct SegmentOrder {
above: Option<usize>, above: Option<usize>,
@ -173,8 +166,8 @@ struct SegmentOrder {
/// ///
/// It can be thought of as a vertical line sweeping from -X to +X across the polygon that keeps track of the order of the segments /// It can be thought of as a vertical line sweeping from -X to +X across the polygon that keeps track of the order of the segments
/// the sweep line is intersecting at any given moment. /// the sweep line is intersecting at any given moment.
#[cfg(feature = "alloc")]
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
#[cfg(feature = "alloc")]
struct SweepLine<'a> { struct SweepLine<'a> {
vertices: &'a [Vec2], vertices: &'a [Vec2],
tree: BTreeMap<Segment, SegmentOrder>, tree: BTreeMap<Segment, SegmentOrder>,