fix: replace unwrap calls with proper error handling in fig.rs#454
Conversation
- Handle None case in description_format serializer gracefully - Handle empty strings in uppercase conversion - Replace filter + unwrap patterns with filter_map - Return proper miette errors for parse_from_spec and JSON serialization failures This improves robustness and prevents potential panics during Fig spec generation. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR improves error handling in the Fig spec generator by replacing potentially panicking unwrap() calls with proper error handling mechanisms.
Changes:
- Adds safe pattern matching for
Optiontypes in the description formatter - Replaces
filter + unwrappatterns withfilter_mapfor cleaner functional code - Introduces proper
mietteerror handling for command parsing and JSON serialization failures
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if let Some(first_upper) = v[0].to_uppercase().next() { | ||
| v[0] = first_upper; | ||
| } |
There was a problem hiding this comment.
Potential index out of bounds panic if the string is empty. The empty string check on line 28-30 should occur before collecting chars into the vector to prevent accessing v[0] when the vector is empty.
| .filter_map(|cmd| cmd.generate_spec.as_ref().map(|spec| (cmd, spec))) | ||
| .for_each(|(_, call_template_str)| { |
There was a problem hiding this comment.
The cmd reference is being cloned in the closure and then immediately discarded (using _). Consider using cmd.generate_spec.as_ref() directly in the closure body instead of creating the tuple, or rename the parameter if cmd is actually needed elsewhere in the closure.
| .filter_map(|cmd| cmd.generate_spec.as_ref().map(|spec| (cmd, spec))) | |
| .for_each(|(_, call_template_str)| { | |
| .filter_map(|cmd| cmd.generate_spec.as_ref()) | |
| .for_each(|call_template_str| { |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #454 +/- ##
==========================================
- Coverage 47.66% 47.29% -0.37%
==========================================
Files 47 47
Lines 6928 6981 +53
Branches 6928 6981 +53
==========================================
Hits 3302 3302
- Misses 1780 1833 +53
Partials 1846 1846 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
### 🚀 Features - **(lint)** add more lint checks by [@jdx](https://github.com/jdx) in [#446](#446) - add logo to docs site by [@jdx](https://github.com/jdx) in [#442](#442) - add VT323 retro terminal font and --usage branding by [@jdx](https://github.com/jdx) in [#443](#443) - add missing builder methods by [@jdx](https://github.com/jdx) in [#444](#444) ### 🐛 Bug Fixes - use pithy LLM-generated title for GitHub releases by [@jdx](https://github.com/jdx) in [#441](#441) - replace unwrap calls with proper error handling in fig.rs by [@jdx](https://github.com/jdx) in [#454](#454) - improve error messages with more context by [@jdx](https://github.com/jdx) in [#449](#449) - skip powershell test if pwsh is not installed by [@jdx](https://github.com/jdx) in [#457](#457) - match completion prefix against unescaped names by [@ilyagr](https://github.com/ilyagr) in [#460](#460) ### 🚜 Refactor - simplify Spec::merge with local macros by [@jdx](https://github.com/jdx) in [#451](#451) ### 📚 Documentation - add CLAUDE.md for Claude Code guidance by [@jdx](https://github.com/jdx) in [#452](#452) - escape generic type parameters in macro doc comments by [@jdx](https://github.com/jdx) in [#453](#453) - add rustdoc for public API functions by [@jdx](https://github.com/jdx) in [#450](#450) - add documentation to public API structs by [@jdx](https://github.com/jdx) in [#455](#455) - add conventional commit guidance to CLAUDE.md by [@jdx](https://github.com/jdx) in [#459](#459) ### ⚡ Performance - remove unnecessary clone in set_subcommand_ancestors by [@jdx](https://github.com/jdx) in [#448](#448) ### 🧪 Testing - add test coverage for untested modules by [@jdx](https://github.com/jdx) in [#447](#447) ### 🔍 Other Changes - remove commented-out trait implementations in mount.rs by [@jdx](https://github.com/jdx) in [#445](#445) - make codecov checks informational by [@jdx](https://github.com/jdx) in [#456](#456) ### 📦️ Dependency Updates - lock file maintenance by [@renovate[bot]](https://github.com/renovate[bot]) in [#439](#439) ### New Contributors - @ilyagr made their first contribution in [#460](#460)
This MR contains the following updates: | Package | Update | Change | |---|---|---| | [usage](https://github.com/jdx/usage) | minor | `2.13.1` → `2.15.0` | MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot). **Proposed changes to behavior should be submitted there as MRs.** --- ### Release Notes <details> <summary>jdx/usage (usage)</summary> ### [`v2.15.0`](https://github.com/jdx/usage/blob/HEAD/CHANGELOG.md#2150---2026-01-26) [Compare Source](jdx/usage@v2.14.0...v2.15.0) ##### 🚀 Features - **(parse)** add Parser builder for custom env var handling by [@​jdx](https://github.com/jdx) in [#​464](jdx/usage#464) ##### 🧪 Testing - **(cli)** use fish output format for cleaner assertions by [@​ilyagr](https://github.com/ilyagr) in [#​461](jdx/usage#461) ##### 🔍 Other Changes - add cargo-semver-checks to detect breaking changes by [@​jdx](https://github.com/jdx) in [#​463](jdx/usage#463) ### [`v2.14.0`](https://github.com/jdx/usage/blob/HEAD/CHANGELOG.md#2140---2026-01-26) [Compare Source](jdx/usage@v2.13.1...v2.14.0) ##### 🚀 Features - **(lint)** add more lint checks by [@​jdx](https://github.com/jdx) in [#​446](jdx/usage#446) - add logo to docs site by [@​jdx](https://github.com/jdx) in [#​442](jdx/usage#442) - add VT323 retro terminal font and --usage branding by [@​jdx](https://github.com/jdx) in [#​443](jdx/usage#443) - add missing builder methods by [@​jdx](https://github.com/jdx) in [#​444](jdx/usage#444) ##### 🐛 Bug Fixes - use pithy LLM-generated title for GitHub releases by [@​jdx](https://github.com/jdx) in [#​441](jdx/usage#441) - replace unwrap calls with proper error handling in fig.rs by [@​jdx](https://github.com/jdx) in [#​454](jdx/usage#454) - improve error messages with more context by [@​jdx](https://github.com/jdx) in [#​449](jdx/usage#449) - skip powershell test if pwsh is not installed by [@​jdx](https://github.com/jdx) in [#​457](jdx/usage#457) - match completion prefix against unescaped names by [@​ilyagr](https://github.com/ilyagr) in [#​460](jdx/usage#460) ##### 🚜 Refactor - simplify Spec::merge with local macros by [@​jdx](https://github.com/jdx) in [#​451](jdx/usage#451) ##### 📚 Documentation - add CLAUDE.md for Claude Code guidance by [@​jdx](https://github.com/jdx) in [#​452](jdx/usage#452) - escape generic type parameters in macro doc comments by [@​jdx](https://github.com/jdx) in [#​453](jdx/usage#453) - add rustdoc for public API functions by [@​jdx](https://github.com/jdx) in [#​450](jdx/usage#450) - add documentation to public API structs by [@​jdx](https://github.com/jdx) in [#​455](jdx/usage#455) - add conventional commit guidance to CLAUDE.md by [@​jdx](https://github.com/jdx) in [#​459](jdx/usage#459) ##### ⚡ Performance - remove unnecessary clone in set\_subcommand\_ancestors by [@​jdx](https://github.com/jdx) in [#​448](jdx/usage#448) ##### 🧪 Testing - add test coverage for untested modules by [@​jdx](https://github.com/jdx) in [#​447](jdx/usage#447) ##### 🔍 Other Changes - remove commented-out trait implementations in mount.rs by [@​jdx](https://github.com/jdx) in [#​445](jdx/usage#445) - make codecov checks informational by [@​jdx](https://github.com/jdx) in [#​456](jdx/usage#456) ##### 📦️ Dependency Updates - lock file maintenance by [@​renovate\[bot\]](https://github.com/renovate\[bot]) in [#​439](jdx/usage#439) ##### New Contributors - [@​ilyagr](https://github.com/ilyagr) made their first contribution in [#​460](jdx/usage#460) </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever MR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this MR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box --- This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0Mi45MC4wIiwidXBkYXRlZEluVmVyIjoiNDIuOTIuNCIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiUmVub3ZhdGUgQm90IiwiYXV0b21hdGlvbjpib3QtYXV0aG9yZWQiLCJkZXBlbmRlbmN5LXR5cGU6Om1pbm9yIl19-->
Summary
Replace risky
unwrap()calls in the Fig spec generator with proper error handling to prevent panics.Changes
Noneand empty string cases gracefully instead of panickingfilter + unwrappattern withfilter_mapfor cleaner codemietteerrors for:parse_from_specfailure (command may be hidden)filter + unwrapwithfilter_mapfilter_mapinstead ofmap + filter + unwrapBefore/After
🤖 Generated with Claude Code
Note
Strengthens robustness and cleans up the Fig spec generator.
None/empty descriptions and safely uppercases first charmietteerrors onparse_from_specfailure and JSON serialization failure; builds output unchanged otherwisefilter + unwrapwithfilter_mapinget_generators, mount command handling, andfill_args_completeWritten by Cursor Bugbot for commit b590017. This will update automatically on new commits. Configure here.