Skip to content

U0012 egui-id-in-loop

Rule: A widget made in a loop gets an id that differs per iteration.

Code U0012 (stable, never reused)
Id egui-id-in-loop
Category egui
Source spirit_level
Fix by hand

Level per tier

Tier Level Reason
small off
small-workspace off
medium off
big off
ai off
embedded off
ui off
egui warn widgets that share an id share their state, and egui reports the clash on screen
units-strict off
units-paranoid off

Parameters

Name Type Default Meaning
id-calls list of strings ["Grid::new", "ComboBox::from_id_salt", "ComboBox::from_label", "CollapsingHeader::new", "Window::new", "id_salt", "id_source", "push_id"] calls whose first argument becomes the widget's id
unsalted list of strings ["ScrollArea::vertical", "ScrollArea::horizontal", "ScrollArea::both", "ScrollArea::new"] constructors whose widget takes a fixed id unless a salt method follows
salt-methods list of strings ["id_salt", "id_source", "id"] methods that give a widget its id
loop-methods list of strings ["for_each", "try_for_each"] iterator methods whose closure is a loop body
toolkits list of strings ["egui", "eframe", "iced", "slint", "gtk4", "relm4", "dioxus", "leptos", "yew", "floem", "xilem", "makepad-widgets"] UI toolkits: a crate that depends on one directly is a UI crate, unless the crate map says ui = false

Why

egui keeps a widget's state (scroll offset, open or closed, column widths) under its id, made from the parent's id and a salt. A widget made in a loop with a constant salt gets the same id on every iteration: the widgets share their state, and egui paints an id clash warning over them.

The lint reports, inside a for, while or loop body, or the closure of a loop-methods call (for_each):

  • a call of id-calls whose first argument is a literal: Grid::new("grid"), .id_salt("list"), ComboBox::from_label("Mode");
  • an unsalted constructor (ScrollArea::vertical()) with no salt-methods call in its chain.

A salt-methods call later in the chain (Window::new("").id(id)) gives the widget its id and clears both.

A widget inside a closure of another call in the loop (ui.horizontal(|ui| ..)) sits in a child Ui with an id of its own, and is not reported. Findings are estimates: a loop that runs once is fine.

How to fix

Salt the id with the item: Grid::new(("grid", i)), .id_salt(("list", row.id)), or wrap the body in ui.push_id(i, |ui| ..).

Configure

[lints.egui-id-in-loop]
level = "deny"
id-calls = ["Grid::new", "ComboBox::from_id_salt", "ComboBox::from_label", "CollapsingHeader::new", "Window::new", "id_salt", "id_source", "push_id"]
unsalted = ["ScrollArea::vertical", "ScrollArea::horizontal", "ScrollArea::both", "ScrollArea::new"]
salt-methods = ["id_salt", "id_source", "id"]
loop-methods = ["for_each", "try_for_each"]
toolkits = ["egui", "eframe", "iced", "slint", "gtk4", "relm4", "dioxus", "leptos", "yew", "floem", "xilem", "makepad-widgets"]
reason = "why this project wants it at this level"

Allow one finding in the code with // spirit-level: allow(egui-id-in-loop) <reason> (waivers); the code works there too, as in [lints.U0012].