Skip to content

Commit 9bb41a8

Browse files
authored
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
1 parent 9b114a4 commit 9bb41a8

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed

crates/bevy_math/src/primitives/polygon.rs

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,13 @@
22
use {
33
super::{Measured2d, Triangle2d},
44
alloc::{collections::BTreeMap, vec::Vec},
5+
core::cmp::Ordering,
56
};
67

7-
use core::cmp::Ordering;
8-
98
use crate::Vec2;
109

11-
#[cfg_attr(
12-
not(feature = "alloc"),
13-
expect(dead_code, reason = "this type is only used with the alloc feature")
14-
)]
1510
#[derive(Debug, Clone, Copy)]
11+
#[cfg(feature = "alloc")]
1612
enum Endpoint {
1713
Left,
1814
Right,
@@ -24,22 +20,16 @@ enum Endpoint {
2420
/// If `e1.position().x == e2.position().x` the events are ordered from bottom to top.
2521
///
2622
/// This is the order expected by the [`SweepLine`].
23+
#[cfg(feature = "alloc")]
2724
#[derive(Debug, Clone, Copy)]
28-
#[cfg_attr(
29-
not(feature = "alloc"),
30-
allow(dead_code, reason = "this type is only used with the alloc feature")
31-
)]
3225
struct SweepLineEvent {
3326
segment: Segment,
3427
/// Type of the vertex (left or right)
3528
endpoint: Endpoint,
3629
}
3730

31+
#[cfg(feature = "alloc")]
3832
impl SweepLineEvent {
39-
#[cfg_attr(
40-
not(feature = "alloc"),
41-
allow(dead_code, reason = "this type is only used with the alloc feature")
42-
)]
4333
fn position(&self) -> Vec2 {
4434
match self.endpoint {
4535
Endpoint::Left => self.segment.left,
@@ -48,31 +38,32 @@ impl SweepLineEvent {
4838
}
4939
}
5040

41+
#[cfg(feature = "alloc")]
5142
impl PartialEq for SweepLineEvent {
5243
fn eq(&self, other: &Self) -> bool {
5344
self.position() == other.position()
5445
}
5546
}
5647

48+
#[cfg(feature = "alloc")]
5749
impl Eq for SweepLineEvent {}
5850

51+
#[cfg(feature = "alloc")]
5952
impl PartialOrd for SweepLineEvent {
6053
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
6154
Some(self.cmp(other))
6255
}
6356
}
6457

58+
#[cfg(feature = "alloc")]
6559
impl Ord for SweepLineEvent {
6660
fn cmp(&self, other: &Self) -> Ordering {
6761
xy_order(self.position(), other.position())
6862
}
6963
}
7064

7165
/// Orders 2D points according to the order expected by the sweep line and event queue from -X to +X and then -Y to Y.
72-
#[cfg_attr(
73-
not(feature = "alloc"),
74-
allow(dead_code, reason = "this type is only used with the alloc feature")
75-
)]
66+
#[cfg(feature = "alloc")]
7667
fn xy_order(a: Vec2, b: Vec2) -> Ordering {
7768
a.x.total_cmp(&b.x).then_with(|| a.y.total_cmp(&b.y))
7869
}
@@ -129,26 +120,31 @@ impl EventQueue {
129120
/// Segments are ordered from bottom to top based on their left vertices if possible.
130121
/// If their y values are identical, the segments are ordered based on the y values of their right vertices.
131122
#[derive(Debug, Clone, Copy)]
123+
#[cfg(feature = "alloc")]
132124
struct Segment {
133125
edge_index: usize,
134126
left: Vec2,
135127
right: Vec2,
136128
}
137129

130+
#[cfg(feature = "alloc")]
138131
impl PartialEq for Segment {
139132
fn eq(&self, other: &Self) -> bool {
140133
self.edge_index == other.edge_index
141134
}
142135
}
143136

137+
#[cfg(feature = "alloc")]
144138
impl Eq for Segment {}
145139

140+
#[cfg(feature = "alloc")]
146141
impl PartialOrd for Segment {
147142
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
148143
Some(self.cmp(other))
149144
}
150145
}
151146

147+
#[cfg(feature = "alloc")]
152148
impl Ord for Segment {
153149
fn cmp(&self, other: &Self) -> Ordering {
154150
self.left
@@ -159,10 +155,7 @@ impl Ord for Segment {
159155
}
160156

161157
/// Holds information about which segment is above and which is below a given [`Segment`]
162-
#[cfg_attr(
163-
not(feature = "alloc"),
164-
expect(dead_code, reason = "this type is only used with the alloc feature")
165-
)]
158+
#[cfg(feature = "alloc")]
166159
#[derive(Debug, Clone, Copy)]
167160
struct SegmentOrder {
168161
above: Option<usize>,
@@ -173,8 +166,8 @@ struct SegmentOrder {
173166
///
174167
/// 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
175168
/// the sweep line is intersecting at any given moment.
176-
#[cfg(feature = "alloc")]
177169
#[derive(Debug, Clone)]
170+
#[cfg(feature = "alloc")]
178171
struct SweepLine<'a> {
179172
vertices: &'a [Vec2],
180173
tree: BTreeMap<Segment, SegmentOrder>,

0 commit comments

Comments
 (0)