U0014 ui-unwrap-external-data¶
Rule: UI code returns an error for a bad file, parse or connection, never unwraps or expects it.
| Code | U0014 (stable, never reused) |
| Id | ui-unwrap-external-data |
| Category | ui |
| 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 | warn |
panic-in-library skips binary crates; a UI app's bad file, parse or connection still deserves an error, not a crash; an estimate, since the chain may be known good or already checked |
| egui | warn |
panic-in-library skips binary crates; a UI app's bad file, parse or connection still deserves an error, not a crash; an estimate, since the chain may be known good or already checked |
| units-strict | off | |
| units-paranoid | off |
Parameters¶
| Name | Type | Default | Meaning |
|---|---|---|---|
calls |
list of strings | ["std::fs::read", "std::fs::read_to_string", "std::fs::write", "std::fs::File::open", "std::fs::File::create", "std::fs::metadata", "std::fs::read_dir", "serde_json::from_str", "serde_json::from_slice", "serde_json::from_reader", "toml::from_str", "toml_edit::de::from_str", "ron::from_str", "ron::de::from_str", "bincode::deserialize", "std::net::TcpStream::connect", "reqwest::blocking::get", "reqwest::get", "ureq::get", "ureq::post"] |
constructors and calls whose result a file, a parse or a connection decides, resolved through the file's use items |
panic-methods |
list of strings | ["unwrap", "expect"] |
methods that panic on an Err or a None |
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¶
panic-in-library counts bare unwrap in library code, but a UI app's binary crate holds no library for it to check. The same file, parse or connection failures reach it directly: a missing config file, a stale cache that no longer deserialises, a server gone. Unwrapping there turns a bad environment into a crash, where a library would have returned a typed error.
The lint reports, in UI crates, a panic-methods call (unwrap, expect) at the end of a chain rooted in one of calls: a file read or write, a serde-style parse (serde_json::from_str, toml::from_str, ...), or a network connection (reqwest::get, TcpStream::connect, ...), resolved through the file's use items. A ?, a reference or a method in between (.map_err(..)) does not clear the chain. Findings are estimates: the value may be known good by construction (a file baked into the binary) or already checked a few lines above.
How to fix¶
Return the error with ? from a function whose signature allows it, or match it and show the person what happened and what to do, the way ui-debug-text and the UX guide's error wording expect. Where the value truly cannot fail (a fixture, a constant parsed once at compile time), waive the finding with that reason.
Configure¶
[lints.ui-unwrap-external-data]
level = "deny"
calls = ["std::fs::read", "std::fs::read_to_string", "std::fs::write", "std::fs::File::open", "std::fs::File::create", "std::fs::metadata", "std::fs::read_dir", "serde_json::from_str", "serde_json::from_slice", "serde_json::from_reader", "toml::from_str", "toml_edit::de::from_str", "ron::from_str", "ron::de::from_str", "bincode::deserialize", "std::net::TcpStream::connect", "reqwest::blocking::get", "reqwest::get", "ureq::get", "ureq::post"]
panic-methods = ["unwrap", "expect"]
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(ui-unwrap-external-data) <reason> (waivers); the code works there too, as in [lints.U0014].