mirror of
https://github.com/emilk/egui.git
synced 2026-09-01 14:20:04 -04:00
Enable or_fun_call lint to avoid unnecessary allocations (#7754)
### What From the [lint description](https://rust-lang.github.io/rust-clippy/master/index.html?search=or_fu#or_fun_call): > The function will always be called. This is only bad if it allocates or does some non-trivial amount of work. But also: > If the function has side-effects, not calling it will change the semantic of the program, but you shouldn’t rely on that. > > The lint also cannot figure out whether the function you call is actually expensive to call or not. Still worth it to keep our happy paths clean, imo.
This commit is contained in:
@@ -82,7 +82,7 @@ impl<'a> AtomKind<'a> {
|
||||
) -> (Vec2, SizedAtomKind<'a>) {
|
||||
match self {
|
||||
AtomKind::Text(text) => {
|
||||
let wrap_mode = wrap_mode.unwrap_or(ui.wrap_mode());
|
||||
let wrap_mode = wrap_mode.unwrap_or_else(|| ui.wrap_mode());
|
||||
let galley = text.into_galley(ui, Some(wrap_mode), available_size.x, fallback_font);
|
||||
(galley.intrinsic_size(), SizedAtomKind::Text(galley))
|
||||
}
|
||||
|
||||
@@ -168,7 +168,7 @@ impl<'a> AtomLayout<'a> {
|
||||
|
||||
let fallback_font = fallback_font.unwrap_or_default();
|
||||
|
||||
let wrap_mode = wrap_mode.unwrap_or(ui.wrap_mode());
|
||||
let wrap_mode = wrap_mode.unwrap_or_else(|| ui.wrap_mode());
|
||||
|
||||
// If the TextWrapMode is not Extend, ensure there is some item marked as `shrink`.
|
||||
// If none is found, mark the first text item as `shrink`.
|
||||
@@ -188,7 +188,7 @@ impl<'a> AtomLayout<'a> {
|
||||
|
||||
let fallback_text_color =
|
||||
fallback_text_color.unwrap_or_else(|| ui.style().visuals.text_color());
|
||||
let gap = gap.unwrap_or(ui.spacing().icon_spacing);
|
||||
let gap = gap.unwrap_or_else(|| ui.spacing().icon_spacing);
|
||||
|
||||
// The size available for the content
|
||||
let available_inner_size = ui.available_size() - frame.total_margin().sum();
|
||||
|
||||
@@ -941,7 +941,7 @@ impl Panel {
|
||||
PanelState::load(ctx, panel.id)
|
||||
.map(get_rect_state_size)
|
||||
.or(panel.default_size)
|
||||
.unwrap_or(get_spacing_size())
|
||||
.unwrap_or_else(get_spacing_size)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -465,7 +465,7 @@ impl<'a> Popup<'a> {
|
||||
pub fn get_best_align(&self) -> RectAlign {
|
||||
let expected_popup_size = self
|
||||
.get_expected_size()
|
||||
.unwrap_or(vec2(self.width.unwrap_or(0.0), 0.0));
|
||||
.unwrap_or_else(|| vec2(self.width.unwrap_or(0.0), 0.0));
|
||||
|
||||
let Some(anchor_rect) = self.anchor.rect(self.id, &self.ctx) else {
|
||||
return self.rect_align;
|
||||
@@ -473,6 +473,7 @@ impl<'a> Popup<'a> {
|
||||
|
||||
RectAlign::find_best_align(
|
||||
#[expect(clippy::iter_on_empty_collections)]
|
||||
#[expect(clippy::or_fun_call)]
|
||||
once(self.rect_align).chain(
|
||||
self.alternative_aligns
|
||||
// Need the empty slice so the iters have the same type so we can unwrap_or
|
||||
|
||||
@@ -611,7 +611,7 @@ impl ContextImpl {
|
||||
}
|
||||
|
||||
let parent_id = find_accesskit_parent(&state.parent_map, builders, id)
|
||||
.unwrap_or(crate::accesskit_root_id());
|
||||
.unwrap_or_else(crate::accesskit_root_id);
|
||||
|
||||
let parent_builder = builders.get_mut(&parent_id).unwrap();
|
||||
parent_builder.push_child(id.accesskit_id());
|
||||
|
||||
@@ -433,7 +433,7 @@ impl Response {
|
||||
pub fn drag_motion(&self) -> Vec2 {
|
||||
if self.dragged() {
|
||||
self.ctx
|
||||
.input(|i| i.pointer.motion().unwrap_or(i.pointer.delta()))
|
||||
.input(|i| i.pointer.motion().unwrap_or_else(|| i.pointer.delta()))
|
||||
} else {
|
||||
Vec2::ZERO
|
||||
}
|
||||
|
||||
@@ -136,7 +136,7 @@ impl Ui {
|
||||
accessibility_parent,
|
||||
} = ui_builder;
|
||||
|
||||
let layer_id = layer_id.unwrap_or(LayerId::background());
|
||||
let layer_id = layer_id.unwrap_or_else(LayerId::background);
|
||||
|
||||
debug_assert!(
|
||||
id_salt.is_none(),
|
||||
@@ -148,7 +148,7 @@ impl Ui {
|
||||
let layout = layout.unwrap_or_default();
|
||||
let disabled = disabled || invisible;
|
||||
let style = style.unwrap_or_else(|| ctx.style());
|
||||
let sense = sense.unwrap_or(Sense::hover());
|
||||
let sense = sense.unwrap_or_else(Sense::hover);
|
||||
|
||||
let placer = Placer::new(max_rect, layout);
|
||||
let ui_stack = UiStack {
|
||||
@@ -277,7 +277,7 @@ impl Ui {
|
||||
|
||||
let id_salt = id_salt.unwrap_or_else(|| Id::from("child"));
|
||||
let max_rect = max_rect.unwrap_or_else(|| self.available_rect_before_wrap());
|
||||
let mut layout = layout.unwrap_or(*self.layout());
|
||||
let mut layout = layout.unwrap_or_else(|| *self.layout());
|
||||
let enabled = self.enabled && !disabled && !invisible;
|
||||
if let Some(layer_id) = layer_id {
|
||||
painter.set_layer_id(layer_id);
|
||||
@@ -287,7 +287,7 @@ impl Ui {
|
||||
}
|
||||
let sizing_pass = self.sizing_pass || sizing_pass;
|
||||
let style = style.unwrap_or_else(|| self.style.clone());
|
||||
let sense = sense.unwrap_or(Sense::hover());
|
||||
let sense = sense.unwrap_or_else(Sense::hover);
|
||||
|
||||
if sizing_pass {
|
||||
// During the sizing pass we want widgets to use up as little space as possible,
|
||||
|
||||
@@ -681,7 +681,7 @@ pub fn paint_texture_load_result(
|
||||
}
|
||||
Ok(TexturePoll::Pending { .. }) => {
|
||||
let show_loading_spinner =
|
||||
show_loading_spinner.unwrap_or(ui.visuals().image_loading_spinners);
|
||||
show_loading_spinner.unwrap_or_else(|| ui.visuals().image_loading_spinners);
|
||||
if show_loading_spinner {
|
||||
Spinner::new().paint_at(ui, rect);
|
||||
}
|
||||
|
||||
@@ -248,7 +248,9 @@ impl Label {
|
||||
layout_job.halign = Align::LEFT;
|
||||
layout_job.justify = false;
|
||||
} else {
|
||||
layout_job.halign = self.halign.unwrap_or(ui.layout().horizontal_placement());
|
||||
layout_job.halign = self
|
||||
.halign
|
||||
.unwrap_or_else(|| ui.layout().horizontal_placement());
|
||||
layout_job.justify = ui.layout().horizontal_justify();
|
||||
}
|
||||
|
||||
|
||||
@@ -118,7 +118,7 @@ impl Widget for ProgressBar {
|
||||
|
||||
let desired_width =
|
||||
desired_width.unwrap_or_else(|| ui.available_size_before_wrap().x.at_least(96.0));
|
||||
let height = desired_height.unwrap_or(ui.spacing().interact_size.y);
|
||||
let height = desired_height.unwrap_or_else(|| ui.spacing().interact_size.y);
|
||||
let (outer_rect, response) =
|
||||
ui.allocate_exact_size(vec2(desired_width, height), Sense::hover());
|
||||
|
||||
|
||||
@@ -496,7 +496,7 @@ impl TextEdit<'_> {
|
||||
} = self;
|
||||
|
||||
let text_color = text_color
|
||||
.or(ui.visuals().override_text_color)
|
||||
.or_else(|| ui.visuals().override_text_color)
|
||||
// .unwrap_or_else(|| ui.style().interact(&response).text_color()); // too bright
|
||||
.unwrap_or_else(|| ui.visuals().widgets.inactive.text_color());
|
||||
|
||||
@@ -691,7 +691,7 @@ impl TextEdit<'_> {
|
||||
if ui.is_rect_visible(rect) {
|
||||
if text.as_str().is_empty() && !hint_text.is_empty() {
|
||||
let hint_text_color = ui.visuals().weak_text_color();
|
||||
let hint_text_font_id = hint_text_font.unwrap_or(font_id.into());
|
||||
let hint_text_font_id = hint_text_font.unwrap_or_else(|| font_id.into());
|
||||
let galley = if multiline {
|
||||
hint_text.into_galley(
|
||||
ui,
|
||||
|
||||
Reference in New Issue
Block a user