Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable missing_assert_message lint #5216

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ match_wildcard_for_single_variants = "warn"
mem_forget = "warn"
mismatched_target_os = "warn"
mismatching_type_param_order = "warn"
missing_assert_message = "warn"
missing_enforced_import_renames = "warn"
missing_errors_doc = "warn"
missing_safety_doc = "warn"
Expand Down Expand Up @@ -256,7 +257,6 @@ 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"
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
4 changes: 2 additions & 2 deletions crates/eframe/src/stopwatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl Stopwatch {
}

pub fn start(&mut self) {
assert!(self.start.is_none());
assert!(self.start.is_none(), "Stopwatch already running");
self.start = Some(Instant::now());
}

Expand All @@ -29,7 +29,7 @@ impl Stopwatch {
}

pub fn resume(&mut self) {
assert!(self.start.is_none());
assert!(self.start.is_none(), "Stopwatch already running");
self.start = Some(Instant::now());
}

Expand Down
18 changes: 14 additions & 4 deletions crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ impl Default for WrappedTextureManager {
epaint::FontImage::new([0, 0]).into(),
Default::default(),
);
assert_eq!(font_id, TextureId::default());
assert_eq!(
font_id,
TextureId::default(),
"the font_id must be the same as the default TextureId"
);

Self(Arc::new(RwLock::new(tex_mngr)))
}
Expand Down Expand Up @@ -794,7 +798,10 @@ impl Context {
let max_passes = self.write(|ctx| ctx.memory.options.max_passes.get());

let mut output = FullOutput::default();
debug_assert_eq!(output.platform_output.num_completed_passes, 0);
debug_assert_eq!(
output.platform_output.num_completed_passes, 0,
"Should be 0 for the first pass"
);

loop {
crate::profile_scope!(
Expand All @@ -814,7 +821,10 @@ impl Context {
self.begin_pass(new_input.take());
run_ui(self);
output.append(self.end_pass());
debug_assert!(0 < output.platform_output.num_completed_passes);
debug_assert!(
0 < output.platform_output.num_completed_passes,
"Should be at least 1"
);

if !output.platform_output.requested_discard() {
break; // no need for another pass
Expand Down Expand Up @@ -3186,7 +3196,7 @@ impl Context {
#[cfg(feature = "accesskit")]
self.pass_state_mut(|fs| {
if let Some(state) = fs.accesskit_state.as_mut() {
assert_eq!(state.parent_stack.pop(), Some(_id));
assert_eq!(state.parent_stack.pop(), Some(_id), "Mismatched push/pop");
}
});

Expand Down
4 changes: 2 additions & 2 deletions crates/egui/src/hit_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ pub fn hit_test(
let hits = hit_test_on_close(&close, pos_in_layer);

if let Some(drag) = hits.drag {
debug_assert!(drag.sense.drag);
debug_assert!(drag.sense.drag, "Drag widget should sense drag");
}
if let Some(click) = hits.click {
debug_assert!(click.sense.click);
debug_assert!(click.sense.click, "Click widget should sense click");
}

hits
Expand Down
45 changes: 27 additions & 18 deletions crates/egui/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ impl Region {
}

pub fn sanity_check(&self) {
debug_assert!(!self.min_rect.any_nan());
debug_assert!(!self.max_rect.any_nan());
debug_assert!(!self.cursor.any_nan());
debug_assert!(!self.min_rect.any_nan(), "should not be NaN");
debug_assert!(!self.max_rect.any_nan(), "should not be NaN");
debug_assert!(!self.cursor.any_nan(), "should not be NaN");
}
}

Expand Down Expand Up @@ -392,8 +392,8 @@ impl Layout {
/// ## Doing layout
impl Layout {
pub fn align_size_within_rect(&self, size: Vec2, outer: Rect) -> Rect {
debug_assert!(size.x >= 0.0 && size.y >= 0.0);
debug_assert!(!outer.is_negative());
debug_assert!(size.x >= 0.0 && size.y >= 0.0, "size should be positive");
debug_assert!(!outer.is_negative(), "outer should be positive");
self.align2().align_size_within_rect(size, outer)
}

Expand All @@ -419,7 +419,7 @@ impl Layout {
}

pub(crate) fn region_from_max_rect(&self, max_rect: Rect) -> Region {
debug_assert!(!max_rect.any_nan());
debug_assert!(!max_rect.any_nan(), "should not be NaN");
let mut region = Region {
min_rect: Rect::NOTHING, // temporary
max_rect,
Expand Down Expand Up @@ -452,8 +452,8 @@ impl Layout {
/// Given the cursor in the region, how much space is available
/// for the next widget?
fn available_from_cursor_max_rect(&self, cursor: Rect, max_rect: Rect) -> Rect {
debug_assert!(!cursor.any_nan());
debug_assert!(!max_rect.any_nan());
debug_assert!(!cursor.any_nan(), "should not be NaN");
debug_assert!(!max_rect.any_nan(), "should not be NaN");

// NOTE: in normal top-down layout the cursor has moved below the current max_rect,
// but the available shouldn't be negative.
Expand Down Expand Up @@ -507,7 +507,7 @@ impl Layout {
avail.max.y = y;
}

debug_assert!(!avail.any_nan());
debug_assert!(!avail.any_nan(), "should not be NaN");

avail
}
Expand All @@ -518,7 +518,10 @@ impl Layout {
/// Use `justify_and_align` to get the inner `widget_rect`.
pub(crate) fn next_frame(&self, region: &Region, child_size: Vec2, spacing: Vec2) -> Rect {
region.sanity_check();
debug_assert!(child_size.x >= 0.0 && child_size.y >= 0.0);
debug_assert!(
child_size.x >= 0.0 && child_size.y >= 0.0,
"child_size should be positive"
);

if self.main_wrap {
let available_size = self.available_rect_before_wrap(region).size();
Expand Down Expand Up @@ -598,7 +601,10 @@ impl Layout {

fn next_frame_ignore_wrap(&self, region: &Region, child_size: Vec2) -> Rect {
region.sanity_check();
debug_assert!(child_size.x >= 0.0 && child_size.y >= 0.0);
debug_assert!(
child_size.x >= 0.0 && child_size.y >= 0.0,
"child_size should be positive"
);

let available_rect = self.available_rect_before_wrap(region);

Expand Down Expand Up @@ -631,16 +637,19 @@ impl Layout {
frame_rect = frame_rect.translate(Vec2::Y * (region.cursor.top() - frame_rect.top()));
}

debug_assert!(!frame_rect.any_nan());
debug_assert!(!frame_rect.is_negative());
debug_assert!(!frame_rect.any_nan(), "frame should not be NaN");
debug_assert!(!frame_rect.is_negative(), "frame should be positive");

frame_rect
}

/// Apply justify (fill width/height) and/or alignment after calling `next_space`.
pub(crate) fn justify_and_align(&self, frame: Rect, mut child_size: Vec2) -> Rect {
debug_assert!(child_size.x >= 0.0 && child_size.y >= 0.0);
debug_assert!(!frame.is_negative());
debug_assert!(
child_size.x >= 0.0 && child_size.y >= 0.0,
"child_size should be positive"
);
debug_assert!(!frame.is_negative(), "frame should be positive");

if self.horizontal_justify() {
child_size.x = child_size.x.at_least(frame.width()); // fill full width
Expand All @@ -658,8 +667,8 @@ impl Layout {
) -> Rect {
let frame = self.next_frame_ignore_wrap(region, size);
let rect = self.align_size_within_rect(size, frame);
debug_assert!(!rect.any_nan());
debug_assert!(!rect.is_negative());
debug_assert!(!rect.any_nan(), "rect should not be NaN");
debug_assert!(!rect.is_negative(), "rect should be positive");
rect
}

Expand Down Expand Up @@ -702,7 +711,7 @@ impl Layout {
widget_rect: Rect,
item_spacing: Vec2,
) {
debug_assert!(!cursor.any_nan());
debug_assert!(!cursor.any_nan(), "cursor should not be NaN");
if self.main_wrap {
if cursor.intersects(frame_rect.shrink(1.0)) {
// make row/column larger if necessary
Expand Down
8 changes: 4 additions & 4 deletions crates/egui/src/placer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ impl Placer {

/// Apply justify or alignment after calling `next_space`.
pub(crate) fn justify_and_align(&self, rect: Rect, child_size: Vec2) -> Rect {
debug_assert!(!rect.any_nan());
debug_assert!(!child_size.any_nan());
debug_assert!(!rect.any_nan(), "rect should not be NaN");
debug_assert!(!child_size.any_nan(), "child_size should not be NaN");

if let Some(grid) = &self.grid {
grid.justify_and_align(rect, child_size)
Expand Down Expand Up @@ -164,8 +164,8 @@ impl Placer {
widget_rect: Rect,
item_spacing: Vec2,
) {
debug_assert!(!frame_rect.any_nan());
debug_assert!(!widget_rect.any_nan());
debug_assert!(!frame_rect.any_nan(), "frame_rect should not be NaN");
debug_assert!(!widget_rect.any_nan(), "widget_rect should not be NaN");
self.region.sanity_check();

if let Some(grid) = &mut self.grid {
Expand Down
5 changes: 4 additions & 1 deletion crates/egui/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,10 @@ impl Response {
///
/// You may not call [`Self::interact`] on the resulting `Response`.
pub fn union(&self, other: Self) -> Self {
assert!(self.ctx == other.ctx);
assert!(
self.ctx == other.ctx,
"Responses must be from the same `Ui`"
);
debug_assert!(
self.layer_id == other.layer_id,
"It makes no sense to combine Responses from two different layers"
Expand Down
5 changes: 4 additions & 1 deletion crates/egui/src/text_selection/text_cursor_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,10 @@ pub fn byte_index_from_char_index(s: &str, char_index: usize) -> usize {
}

pub fn slice_char_range(s: &str, char_range: std::ops::Range<usize>) -> &str {
assert!(char_range.start <= char_range.end);
assert!(
char_range.start <= char_range.end,
"start must be less than or equal to end"
);
let start_byte = byte_index_from_char_index(s, char_range.start);
let end_byte = byte_index_from_char_index(s, char_range.end);
&s[start_byte..end_byte]
Expand Down
6 changes: 5 additions & 1 deletion crates/egui/src/text_selection/visuals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ pub fn paint_text_selection(
// Start by appending the selection rectangle to end of the mesh, as two triangles (= 6 indices):
let num_indices_before = mesh.indices.len();
mesh.add_colored_rect(rect, color);
assert_eq!(num_indices_before + 6, mesh.indices.len());
assert_eq!(
num_indices_before + 6,
mesh.indices.len(),
"We expect exactly 6 new indices"
);

// Copy out the new triangles:
let selection_triangles = [
Expand Down
15 changes: 9 additions & 6 deletions crates/egui/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ impl Ui {
}
}

debug_assert!(!max_rect.any_nan());
debug_assert!(!max_rect.any_nan(), "max_rect should not be NaN");
let stable_id = self.id.with(id_salt);
let unique_id = stable_id.with(self.next_auto_id_salt);
let next_auto_id_salt = unique_id.value().wrapping_add(1);
Expand Down Expand Up @@ -901,14 +901,14 @@ impl Ui {
/// Set the minimum width of the ui.
/// This can't shrink the ui, only make it larger.
pub fn set_min_width(&mut self, width: f32) {
debug_assert!(0.0 <= width);
debug_assert!(0.0 <= width, "width must be larger than or equal to 0.0");
self.placer.set_min_width(width);
}

/// Set the minimum height of the ui.
/// This can't shrink the ui, only make it larger.
pub fn set_min_height(&mut self, height: f32) {
debug_assert!(0.0 <= height);
debug_assert!(0.0 <= height, "height must be larger than or equal to 0.0");
self.placer.set_min_height(height);
}

Expand Down Expand Up @@ -1275,7 +1275,7 @@ impl Ui {
fn allocate_space_impl(&mut self, desired_size: Vec2) -> Rect {
let item_spacing = self.spacing().item_spacing;
let frame_rect = self.placer.next_space(desired_size, item_spacing);
debug_assert!(!frame_rect.any_nan());
debug_assert!(!frame_rect.any_nan(), "frame_rect should not be NaN");
let widget_rect = self.placer.justify_and_align(frame_rect, desired_size);

self.placer
Expand All @@ -1297,7 +1297,7 @@ impl Ui {

/// Allocate a rect without interacting with it.
pub fn advance_cursor_after_rect(&mut self, rect: Rect) -> Id {
debug_assert!(!rect.any_nan());
debug_assert!(!rect.any_nan(), "rect should not be NaN");
let item_spacing = self.spacing().item_spacing;
self.placer.advance_after_rects(rect, rect, item_spacing);
register_rect(self, rect);
Expand Down Expand Up @@ -1367,7 +1367,10 @@ impl Ui {
layout: Layout,
add_contents: Box<dyn FnOnce(&mut Self) -> R + 'c>,
) -> InnerResponse<R> {
debug_assert!(desired_size.x >= 0.0 && desired_size.y >= 0.0);
debug_assert!(
desired_size.x >= 0.0 && desired_size.y >= 0.0,
"desired_size should be positive"
);
let item_spacing = self.spacing().item_spacing;
let frame_rect = self.placer.next_space(desired_size, item_spacing);
let child_rect = self.placer.justify_and_align(frame_rect, desired_size);
Expand Down
5 changes: 4 additions & 1 deletion crates/egui/src/widgets/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,10 @@ impl Label {

let cursor = ui.cursor();
let first_row_indentation = available_width - ui.available_size_before_wrap().x;
debug_assert!(first_row_indentation.is_finite());
debug_assert!(
first_row_indentation.is_finite(),
"first_row_indentation should be finite"
);

layout_job.wrap.max_width = available_width;
layout_job.first_row_min_height = cursor.height();
Expand Down
Loading
Loading