1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-29 04:40:03 -04:00

Add assert messages and print bad argument values in asserts (#5216)

Enabled the `missing_assert_message` lint

* [x] I have followed the instructions in the PR template

---------

Co-authored-by: Lucas Meurer <lucasmeurer96@gmail.com>
This commit is contained in:
Nicolas
2025-03-25 09:20:29 +01:00
committed by GitHub
parent 903bd81313
commit 58b2ac88c0
35 changed files with 331 additions and 108 deletions

View File

@@ -53,7 +53,12 @@ fn tessellate_circles(c: &mut Criterion) {
clipped_shapes.push(ClippedShape { clip_rect, shape });
}
}
assert_eq!(clipped_shapes.len(), 100_000);
assert_eq!(
clipped_shapes.len(),
100_000,
"length of clipped shapes should be 100k, but was {}",
clipped_shapes.len()
);
let pixels_per_point = 2.0;
let options = TessellationOptions::default();

View File

@@ -94,7 +94,13 @@ impl ColorImage {
/// }
/// ```
pub fn from_rgba_unmultiplied(size: [usize; 2], rgba: &[u8]) -> Self {
assert_eq!(size[0] * size[1] * 4, rgba.len());
assert_eq!(
size[0] * size[1] * 4,
rgba.len(),
"size: {:?}, rgba.len(): {}",
size,
rgba.len()
);
let pixels = rgba
.chunks_exact(4)
.map(|p| Color32::from_rgba_unmultiplied(p[0], p[1], p[2], p[3]))
@@ -103,7 +109,13 @@ impl ColorImage {
}
pub fn from_rgba_premultiplied(size: [usize; 2], rgba: &[u8]) -> Self {
assert_eq!(size[0] * size[1] * 4, rgba.len());
assert_eq!(
size[0] * size[1] * 4,
rgba.len(),
"size: {:?}, rgba.len(): {}",
size,
rgba.len()
);
let pixels = rgba
.chunks_exact(4)
.map(|p| Color32::from_rgba_premultiplied(p[0], p[1], p[2], p[3]))
@@ -115,7 +127,13 @@ impl ColorImage {
///
/// Panics if `size[0] * size[1] != gray.len()`.
pub fn from_gray(size: [usize; 2], gray: &[u8]) -> Self {
assert_eq!(size[0] * size[1], gray.len());
assert_eq!(
size[0] * size[1],
gray.len(),
"size: {:?}, gray.len(): {}",
size,
gray.len()
);
let pixels = gray.iter().map(|p| Color32::from_gray(*p)).collect();
Self { size, pixels }
}
@@ -127,7 +145,13 @@ impl ColorImage {
#[doc(alias = "from_grey_iter")]
pub fn from_gray_iter(size: [usize; 2], gray_iter: impl Iterator<Item = u8>) -> Self {
let pixels: Vec<_> = gray_iter.map(Color32::from_gray).collect();
assert_eq!(size[0] * size[1], pixels.len());
assert_eq!(
size[0] * size[1],
pixels.len(),
"size: {:?}, pixels.len(): {}",
size,
pixels.len()
);
Self { size, pixels }
}
@@ -150,7 +174,13 @@ impl ColorImage {
///
/// Panics if `size[0] * size[1] * 3 != rgb.len()`.
pub fn from_rgb(size: [usize; 2], rgb: &[u8]) -> Self {
assert_eq!(size[0] * size[1] * 3, rgb.len());
assert_eq!(
size[0] * size[1] * 3,
rgb.len(),
"size: {:?}, rgb.len(): {}",
size,
rgb.len()
);
let pixels = rgb
.chunks_exact(3)
.map(|p| Color32::from_rgb(p[0], p[1], p[2]))
@@ -225,7 +255,7 @@ impl std::ops::Index<(usize, usize)> for ColorImage {
#[inline]
fn index(&self, (x, y): (usize, usize)) -> &Color32 {
let [w, h] = self.size;
assert!(x < w && y < h);
assert!(x < w && y < h, "x: {x}, y: {y}, w: {w}, h: {h}");
&self.pixels[y * w + x]
}
}
@@ -234,7 +264,7 @@ impl std::ops::IndexMut<(usize, usize)> for ColorImage {
#[inline]
fn index_mut(&mut self, (x, y): (usize, usize)) -> &mut Color32 {
let [w, h] = self.size;
assert!(x < w && y < h);
assert!(x < w && y < h, "x: {x}, y: {y}, w: {w}, h: {h}");
&mut self.pixels[y * w + x]
}
}
@@ -328,15 +358,32 @@ impl FontImage {
/// Clone a sub-region as a new image.
pub fn region(&self, [x, y]: [usize; 2], [w, h]: [usize; 2]) -> Self {
assert!(x + w <= self.width());
assert!(y + h <= self.height());
assert!(
x + w <= self.width(),
"x + w should be <= self.width(), but x: {}, w: {}, width: {}",
x,
w,
self.width()
);
assert!(
y + h <= self.height(),
"y + h should be <= self.height(), but y: {}, h: {}, height: {}",
y,
h,
self.height()
);
let mut pixels = Vec::with_capacity(w * h);
for y in y..y + h {
let offset = y * self.width() + x;
pixels.extend(&self.pixels[offset..(offset + w)]);
}
assert_eq!(pixels.len(), w * h);
assert_eq!(
pixels.len(),
w * h,
"pixels.len should be w * h, but got {}",
pixels.len()
);
Self {
size: [w, h],
pixels,
@@ -350,7 +397,7 @@ impl std::ops::Index<(usize, usize)> for FontImage {
#[inline]
fn index(&self, (x, y): (usize, usize)) -> &f32 {
let [w, h] = self.size;
assert!(x < w && y < h);
assert!(x < w && y < h, "x: {x}, y: {y}, w: {w}, h: {h}");
&self.pixels[y * w + x]
}
}
@@ -359,7 +406,7 @@ impl std::ops::IndexMut<(usize, usize)> for FontImage {
#[inline]
fn index_mut(&mut self, (x, y): (usize, usize)) -> &mut f32 {
let [w, h] = self.size;
assert!(x < w && y < h);
assert!(x < w && y < h, "x: {x}, y: {y}, w: {w}, h: {h}");
&mut self.pixels[y * w + x]
}
}

View File

@@ -119,7 +119,7 @@ impl Mesh {
/// Panics when `other` mesh has a different texture.
pub fn append(&mut self, other: Self) {
profiling::function_scope!();
debug_assert!(other.is_valid());
debug_assert!(other.is_valid(), "Other mesh is invalid");
if self.is_empty() {
*self = other;
@@ -133,7 +133,7 @@ impl Mesh {
///
/// Panics when `other` mesh has a different texture.
pub fn append_ref(&mut self, other: &Self) {
debug_assert!(other.is_valid());
debug_assert!(other.is_valid(), "Other mesh is invalid");
if self.is_empty() {
self.texture_id = other.texture_id;
@@ -155,7 +155,10 @@ impl Mesh {
/// Panics when the mesh has assigned a texture.
#[inline(always)]
pub fn colored_vertex(&mut self, pos: Pos2, color: Color32) {
debug_assert!(self.texture_id == TextureId::default());
debug_assert!(
self.texture_id == TextureId::default(),
"Mesh has an assigned texture"
);
self.vertices.push(Vertex {
pos,
uv: WHITE_UV,
@@ -218,7 +221,10 @@ impl Mesh {
/// Uniformly colored rectangle.
#[inline(always)]
pub fn add_colored_rect(&mut self, rect: Rect, color: Color32) {
debug_assert!(self.texture_id == TextureId::default());
debug_assert!(
self.texture_id == TextureId::default(),
"Mesh has an assigned texture"
);
self.add_rect_with_uv(rect, [WHITE_UV, WHITE_UV].into(), color);
}
@@ -227,7 +233,7 @@ impl Mesh {
/// Splits this mesh into many smaller meshes (if needed)
/// where the smaller meshes have 16-bit indices.
pub fn split_to_u16(self) -> Vec<Mesh16> {
debug_assert!(self.is_valid());
debug_assert!(self.is_valid(), "Mesh is invalid");
const MAX_SIZE: u32 = u16::MAX as u32;
@@ -280,7 +286,7 @@ impl Mesh {
vertices: self.vertices[(min_vindex as usize)..=(max_vindex as usize)].to_vec(),
texture_id: self.texture_id,
};
debug_assert!(mesh.is_valid());
debug_assert!(mesh.is_valid(), "Mesh is invalid");
output.push(mesh);
}
output

View File

@@ -339,7 +339,7 @@ impl Shape {
#[inline]
pub fn mesh(mesh: impl Into<Arc<Mesh>>) -> Self {
let mesh = mesh.into();
debug_assert!(mesh.is_valid());
debug_assert!(mesh.is_valid(), "Invalid mesh: {mesh:#?}");
Self::Mesh(mesh)
}
@@ -525,7 +525,13 @@ fn dashes_from_line(
shapes: &mut Vec<Shape>,
dash_offset: f32,
) {
assert_eq!(dash_lengths.len(), gap_lengths.len());
assert_eq!(
dash_lengths.len(),
gap_lengths.len(),
"Mismatched dash and gap lengths, got dash_lengths: {}, gap_lengths: {}",
dash_lengths.len(),
gap_lengths.len()
);
let mut position_on_segment = dash_offset;
let mut drawing_dash = false;
let mut step = 0;

View File

@@ -111,7 +111,10 @@ impl AllocInfo {
}
pub fn num_elements(&self) -> usize {
assert!(self.element_size != ElementSize::Heterogenous);
assert!(
self.element_size != ElementSize::Heterogenous,
"Heterogenous element size"
);
self.num_elements
}

View File

@@ -382,7 +382,7 @@ impl Path {
pub fn add_open_points(&mut self, points: &[Pos2]) {
let n = points.len();
assert!(n >= 2);
assert!(n >= 2, "A path needs at least two points, but got {n}");
if n == 2 {
// Common case optimization:
@@ -428,7 +428,7 @@ impl Path {
pub fn add_line_loop(&mut self, points: &[Pos2]) {
let n = points.len();
assert!(n >= 2);
assert!(n >= 2, "A path needs at least two points, but got {n}");
self.reserve(n);
let mut n0 = (points[0] - points[n - 1]).normalized().rot90();

View File

@@ -91,8 +91,14 @@ impl FontImpl {
scale_in_pixels: f32,
tweak: FontTweak,
) -> Self {
assert!(scale_in_pixels > 0.0);
assert!(pixels_per_point > 0.0);
assert!(
scale_in_pixels > 0.0,
"scale_in_pixels is smaller than 0, got: {scale_in_pixels:?}"
);
assert!(
pixels_per_point > 0.0,
"pixels_per_point must be greater than 0, got: {pixels_per_point:?}"
);
use ab_glyph::{Font, ScaleFont};
let scaled = ab_glyph_font.as_scaled(scale_in_pixels);
@@ -264,7 +270,7 @@ impl FontImpl {
}
fn allocate_glyph(&self, glyph_id: ab_glyph::GlyphId) -> GlyphInfo {
assert!(glyph_id.0 != 0);
assert!(glyph_id.0 != 0, "Can't allocate glyph for id 0");
use ab_glyph::{Font as _, ScaleFont};
let glyph = glyph_id.with_scale_and_position(

View File

@@ -529,7 +529,7 @@ fn halign_and_justify_row(
(num_leading_spaces, row.glyphs.len() - num_trailing_spaces)
};
let num_glyphs_in_range = glyph_range.1 - glyph_range.0;
assert!(num_glyphs_in_range > 0);
assert!(num_glyphs_in_range > 0, "Should have at least one glyph");
let original_min_x = row.glyphs[glyph_range.0].logical_rect().min.x;
let original_max_x = row.glyphs[glyph_range.1 - 1].logical_rect().max.x;
@@ -898,7 +898,10 @@ fn add_hline(point_scale: PointScale, [start, stop]: [Pos2; 2], stroke: Stroke,
} else {
// Thin lines often lost, so this is a bad idea
assert_eq!(start.y, stop.y);
assert_eq!(
start.y, stop.y,
"Horizontal line must be horizontal, but got: {start:?} -> {stop:?}"
);
let min_y = point_scale.round_to_pixel(start.y - 0.5 * stroke.width);
let max_y = point_scale.round_to_pixel(min_y + stroke.width);

View File

@@ -892,7 +892,7 @@ impl Galley {
}
ccursor_it.index += row.char_count_including_newline();
}
debug_assert!(ccursor_it == self.end());
debug_assert!(ccursor_it == self.end(), "Cursor out of bounds");
if let Some(last_row) = self.rows.last() {
LayoutCursor {

View File

@@ -88,7 +88,11 @@ impl TextureAtlas {
// Make the top left pixel fully white for `WHITE_UV`, i.e. painting something with solid color:
let (pos, image) = atlas.allocate((1, 1));
assert_eq!(pos, (0, 0));
assert_eq!(
pos,
(0, 0),
"Expected the first allocation to be at (0, 0), but was at {pos:?}"
);
image[pos] = 1.0;
// Allocate a series of anti-aliased discs used to render small filled circles: