Skip to content

Commit

Permalink
start enabling missing_assert_messagelint
Browse files Browse the repository at this point in the history
  • Loading branch information
bircni committed Oct 2, 2024
1 parent 1406e87 commit f3e9ceb
Show file tree
Hide file tree
Showing 15 changed files with 103 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ zero_sized_map_values = "warn"
# TODO(emilk): enable more of these lints:
iter_over_hash_type = "allow"
let_underscore_untyped = "allow"
missing_assert_message = "allow"
missing_assert_message = "deny"
should_panic_without_expect = "allow"
too_many_lines = "allow"
unwrap_used = "allow" # TODO(emilk): We really wanna warn on this one
Expand Down
10 changes: 8 additions & 2 deletions crates/ecolor/src/color32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,10 @@ impl Color32 {
/// This is perceptually even, and faster that [`Self::linear_multiply`].
#[inline]
pub fn gamma_multiply(self, factor: f32) -> Self {
debug_assert!(0.0 <= factor && factor.is_finite());
debug_assert!(
0.0 <= factor && factor.is_finite(),
"the factor must be finite"
);
let Self([r, g, b, a]) = self;
Self([
(r as f32 * factor + 0.5) as u8,
Expand All @@ -230,7 +233,10 @@ impl Color32 {
/// You likely want to use [`Self::gamma_multiply`] instead.
#[inline]
pub fn linear_multiply(self, factor: f32) -> Self {
debug_assert!(0.0 <= factor && factor.is_finite());
debug_assert!(
0.0 <= factor && factor.is_finite(),
"the factor must be finite"
);
// As an unfortunate side-effect of using premultiplied alpha
// we need a somewhat expensive conversion to linear space and back.
Rgba::from(self).multiply(factor).into()
Expand Down
6 changes: 3 additions & 3 deletions crates/ecolor/src/rgba.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,15 @@ impl Rgba {

#[inline]
pub fn from_luminance_alpha(l: f32, a: f32) -> Self {
debug_assert!(0.0 <= l && l <= 1.0);
debug_assert!(0.0 <= a && a <= 1.0);
debug_assert!(0.0 <= l && l <= 1.0, "l must be in [0.0, 1.0]");
debug_assert!(0.0 <= a && a <= 1.0, "a must be in [0.0, 1.0]");
Self([l * a, l * a, l * a, a])
}

/// Transparent black
#[inline]
pub fn from_black_alpha(a: f32) -> Self {
debug_assert!(0.0 <= a && a <= 1.0);
debug_assert!(0.0 <= a && a <= 1.0, "a must be in [0.0, 1.0]");
Self([0.0, 0.0, 0.0, a])
}

Expand Down
17 changes: 13 additions & 4 deletions crates/emath/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ where
{
let from = from.into();
let to = to.into();
debug_assert!(from.start() != from.end());
debug_assert!(
from.start() != from.end(),
"start and end of range must be different"
);
let t = (x - *from.start()) / (*from.end() - *from.start());
lerp(to, t)
}
Expand All @@ -169,7 +172,10 @@ where
} else if *from.end() <= x {
*to.end()
} else {
debug_assert!(from.start() != from.end());
debug_assert!(
from.start() != from.end(),
"start and end of range must be different"
);
let t = (x - *from.start()) / (*from.end() - *from.start());
// Ensure no numerical inaccuracies sneak in:
if T::ONE <= t {
Expand All @@ -196,8 +202,11 @@ pub fn format_with_minimum_decimals(value: f64, decimals: usize) -> String {
pub fn format_with_decimals_in_range(value: f64, decimal_range: RangeInclusive<usize>) -> String {
let min_decimals = *decimal_range.start();
let max_decimals = *decimal_range.end();
debug_assert!(min_decimals <= max_decimals);
debug_assert!(max_decimals < 100);
debug_assert!(
min_decimals <= max_decimals,
"max_decimals must be >= min_decimals"
);
debug_assert!(max_decimals < 100, "max_decimals is too large");
let max_decimals = max_decimals.min(16);
let min_decimals = min_decimals.min(max_decimals);

Expand Down
2 changes: 1 addition & 1 deletion crates/emath/src/rot2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl Rot2 {
c: self.c / l,
s: self.s / l,
};
debug_assert!(ret.is_finite());
debug_assert!(ret.is_finite(), "ret must be finite");
ret
}
}
Expand Down
10 changes: 8 additions & 2 deletions crates/emath/src/smart_aim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ pub fn best_in_range_f64(min: f64, max: f64) -> f64 {
if !max.is_finite() {
return min;
}
debug_assert!(min.is_finite() && max.is_finite());
debug_assert!(
min.is_finite() && max.is_finite(),
"min and max must be finite"
);

let min_exponent = min.log10();
let max_exponent = max.log10();
Expand Down Expand Up @@ -101,7 +104,10 @@ fn from_decimal_string(s: &[i32]) -> f64 {

/// Find the simplest integer in the range [min, max]
fn simplest_digit_closed_range(min: i32, max: i32) -> i32 {
debug_assert!(1 <= min && min <= max && max <= 9);
debug_assert!(
1 <= min && min <= max && max <= 9,
"the range must be in [1, 9]"
);
if min <= 5 && 5 <= max {
5
} else {
Expand Down
52 changes: 38 additions & 14 deletions crates/epaint/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand All @@ -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]))
Expand All @@ -115,7 +127,11 @@ 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(),
"the size of the image and the length of the gray data must match"
);
let pixels = gray.iter().map(|p| Color32::from_gray(*p)).collect();
Self { size, pixels }
}
Expand All @@ -126,7 +142,11 @@ impl ColorImage {
/// Panics if `size[0] * size[1] != gray_iter.len()`.
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(),
"the size of the image and the count of the pixels must match"
);
Self { size, pixels }
}

Expand All @@ -153,8 +173,8 @@ impl ColorImage {
let max_x = (region.max.x * pixels_per_point) as usize;
let min_y = (region.min.y * pixels_per_point) as usize;
let max_y = (region.max.y * pixels_per_point) as usize;
assert!(min_x <= max_x);
assert!(min_y <= max_y);
assert!(min_x <= max_x, "min must be less than or equal to max");
assert!(min_y <= max_y, "min must be less than or equal to max");
let width = max_x - min_x;
let height = max_y - min_y;
let mut output = Vec::with_capacity(width * height);
Expand All @@ -178,7 +198,11 @@ 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(),
"the size of the image and the length of the rgb data must match"
);
let pixels = rgb
.chunks_exact(3)
.map(|p| Color32::from_rgb(p[0], p[1], p[2]))
Expand Down Expand Up @@ -220,7 +244,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, "index out of bounds");
&self.pixels[y * w + x]
}
}
Expand All @@ -229,7 +253,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, "index out of bounds");
&mut self.pixels[y * w + x]
}
}
Expand Down Expand Up @@ -312,15 +336,15 @@ 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 must be within bounds");
assert!(y + h <= self.height(), "y must be within bounds");

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() must match w * h");
Self {
size: [w, h],
pixels,
Expand All @@ -334,7 +358,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, "index out of bounds");
&self.pixels[y * w + x]
}
}
Expand All @@ -343,7 +367,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, "index out of bounds");
&mut self.pixels[y * w + x]
}
}
Expand Down
18 changes: 12 additions & 6 deletions crates/epaint/src/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl Mesh {
/// Panics when `other` mesh has a different texture.
pub fn append(&mut self, other: Self) {
crate::profile_function!();
debug_assert!(other.is_valid());
debug_assert!(other.is_valid(), "other has a different texture");

if self.is_empty() {
*self = other;
Expand All @@ -126,7 +126,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 has a different texture");

if self.is_empty() {
self.texture_id = other.texture_id;
Expand All @@ -148,7 +148,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(),
"Can't add colored vertex to textured mesh"
);
self.vertices.push(Vertex {
pos,
uv: WHITE_UV,
Expand Down Expand Up @@ -211,7 +214,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(),
"Can't add colored rect to textured mesh"
);
self.add_rect_with_uv(rect, [WHITE_UV, WHITE_UV].into(), color);
}

Expand All @@ -220,7 +226,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(), "Invalid mesh");

const MAX_SIZE: u32 = u16::MAX as u32;

Expand Down Expand Up @@ -273,7 +279,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(), "Invalid mesh");
output.push(mesh);
}
output
Expand Down
8 changes: 6 additions & 2 deletions crates/epaint/src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ impl Shape {

#[inline]
pub fn mesh(mesh: Mesh) -> Self {
debug_assert!(mesh.is_valid());
debug_assert!(mesh.is_valid(), "Invalid mesh");
Self::Mesh(mesh)
}

Expand Down Expand Up @@ -1111,7 +1111,11 @@ 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"
);
let mut position_on_segment = dash_offset;
let mut drawing_dash = false;
let mut step = 0;
Expand Down
5 changes: 4 additions & 1 deletion crates/epaint/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ impl AllocInfo {
}

pub fn num_elements(&self) -> usize {
assert!(self.element_size != ElementSize::Heterogenous);
assert!(
self.element_size != ElementSize::Heterogenous,
"hetereogenous size"
);
self.num_elements
}

Expand Down
4 changes: 2 additions & 2 deletions crates/epaint/src/tessellator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,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");

if n == 2 {
// Common case optimization:
Expand Down Expand Up @@ -430,7 +430,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");
self.reserve(n);

let mut n0 = (points[0] - points[n - 1]).normalized().rot90();
Expand Down
6 changes: 3 additions & 3 deletions crates/epaint/src/text/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ 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 must be positive");
assert!(pixels_per_point > 0.0, "Pixels per point must be positive");

use ab_glyph::{Font, ScaleFont};
let scaled = ab_glyph_font.as_scaled(scale_in_pixels);
Expand Down Expand Up @@ -266,7 +266,7 @@ impl FontImpl {
}

fn allocate_glyph(&self, glyph_id: ab_glyph::GlyphId) -> GlyphInfo {
assert!(glyph_id.0 != 0);
assert!(glyph_id.0 != 0, "Invalid glyph_id");
use ab_glyph::{Font as _, ScaleFont};

let glyph = glyph_id.with_scale_and_position(
Expand Down
Loading

0 comments on commit f3e9ceb

Please sign in to comment.