Skip to content

refactor: replace all unwrap() calls in crates/ with expect() - #225

Merged
nh13 merged 2 commits into
mainfrom
nh/unwrap-audit-crates
Apr 4, 2026
Merged

nh13 merged 2 commits into
mainfrom
nh/unwrap-audit-crates

Conversation

@nh13

@nh13 nh13 commented Apr 4, 2026

Copy link
Copy Markdown
Member

Summary

  • Audit and replace all 494 unwrap() calls across 34 files in crates/
  • Production code (~61 calls): converted to expect() with invariant-documenting messages
  • Test code (~433 calls): converted to expect() with context-specific failure messages
  • Only 9 unwrap() calls remain, all in doc comments (standard Rust convention)

Test plan

  • cargo ci-test — all 2,213 tests pass
  • cargo ci-fmt — formatting clean
  • cargo ci-lint — no warnings

Audit and replace all 494 unwrap() calls across 36 files in crates/.
Production code (~61 calls) converted to expect() with invariant
messages. Test code (~433 calls) converted to expect() with
context-specific failure messages.

Only 9 unwrap() calls remain, all in doc comments where they are
standard Rust convention.
@nh13
nh13 temporarily deployed to github-actions April 4, 2026 06:17 — with GitHub Actions Inactive
@codecov

codecov Bot commented Apr 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.18%. Comparing base (ea6e3fc) to head (c457462).
⚠️ Report is 7 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #225   +/-   ##
=======================================
  Coverage   88.18%   88.18%           
=======================================
  Files         113      113           
  Lines       53388    53388           
=======================================
+ Hits        47080    47081    +1     
+ Misses       6308     6307    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@coderabbitai

coderabbitai Bot commented Apr 4, 2026 •

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: abf5bbe5-15cd-4875-93b1-34979f5ecb06

📥 Commits

Reviewing files that changed from the base of the PR and between 8c62548 and c457462.

📒 Files selected for processing (5)
  • crates/fgumi-consensus/src/duplex_caller.rs
  • crates/fgumi-consensus/src/vanilla_caller.rs
  • crates/fgumi-consensus/src/vendored/bam_codec/encoder/mod.rs
  • crates/fgumi-consensus/src/vendored/bam_codec/encoder/reference_sequence_id.rs
  • crates/fgumi-sam/src/builder.rs
✅ Files skipped from review due to trivial changes (1)
  • crates/fgumi-consensus/src/vendored/bam_codec/encoder/mod.rs
🚧 Files skipped from review as they are similar to previous changes (3)
  • crates/fgumi-consensus/src/vendored/bam_codec/encoder/reference_sequence_id.rs
  • crates/fgumi-consensus/src/duplex_caller.rs
  • crates/fgumi-sam/src/builder.rs

📝 Walkthrough

Walkthrough

The PR replaces numerous unwrap()/unwrap_err() calls with expect()/expect_err(...).to_string().contains(...) and explicit expect messages across many crates (fgumi-bgzf, fgumi-consensus, fgumi-dna, fgumi-metrics, fgumi-raw-bam, fgumi-sam, fgumi-simd-fastq, fgumi-umi, and others). Changes occur mainly in tests and a few infallible-write or conversion sites in production code. No public API signatures, core algorithms, or runtime control flow were modified.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main refactoring effort: systematically replacing unwrap() calls with expect() across the crates/ directory.
Description check ✅ Passed The description directly relates to the changeset, explaining the audit scope, production vs test code changes, remaining unwrap() calls in docs, and validation through passing tests.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch nh/unwrap-audit-crates

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
crates/fgumi-metrics/src/consensus.rs (1)

492-534: LGTM on the unwrap→expect changes.

Note: The assert!(x.is_some()) checks before .expect() are redundant—both panic on None. Consider removing them in a future cleanup.

,

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/fgumi-metrics/src/consensus.rs` around lines 492 - 534, The test
redundantly calls assert!(...is_some()) before using expect(...) on the same
Option values (kv_metrics.iter().find(...)); remove the duplicate assert!(...).
Keep the expect(...) calls which already provide a clear panic message and
assertions on .value/.description for keys like "raw_reads_considered",
"raw_reads_rejected", "raw_reads_used", "frac_raw_reads_used",
"consensus_reads_emitted", "raw_reads_rejected_for_insufficient_support", and
"raw_reads_rejected_for_minority_alignment" so the test remains concise and
still fails with the same diagnostics if a metric is missing.
crates/fgumi-consensus/src/duplex_caller.rs (1)

375-375: Tighten a few expect messages to reflect the actual invariant.

A few messages are still generic (“expected item at index”, “expected another item from iterator”). At Line 2312/Line 2316 these are keyed lookups, and at Line 5080 it’s really “exactly one record expected”.

Suggested message-only cleanup
-        let last = *min_reads.last().expect("expected a last item");
+        let last = *min_reads
+            .last()
+            .expect("min_reads must be non-empty (validated above)");

-        let (a_reads, b_reads) = groups.get("UMI1").expect("expected item at index");
+        let (a_reads, b_reads) = groups
+            .get("UMI1")
+            .expect("expected group for key UMI1");

-        let (a_reads, b_reads) = groups.get("UMI2").expect("expected item at index");
+        let (a_reads, b_reads) = groups
+            .get("UMI2")
+            .expect("expected group for key UMI2");

-            records.into_iter().next().expect("expected another item from iterator")
+            records
+                .into_iter()
+                .next()
+                .expect("expected exactly one parsed record")

Also applies to: 2312-2317, 5077-5080

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@crates/fgumi-consensus/src/duplex_caller.rs` at line 375, Replace the generic
expect messages with precise invariants: for the shown line change the panic
text on min_reads.last().expect(...) to something like "min_reads must contain
at least one element" (referencing the min_reads variable in duplex_caller.rs);
for the keyed lookups mentioned (the HashMap/lookup sites around the other
occurrences) update their expect messages to include the missing key or context
(e.g., "expected entry for key {key} in <variable_name>"); and for the case
noted as "exactly one record expected" replace that generic expect with "exactly
one record expected in <collection_name>, found {len}" so the panic communicates
the exact invariant and context. Ensure each expect uses the actual variable/key
names from duplex_caller.rs to make the messages actionable.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@crates/fgumi-consensus/src/duplex_caller.rs`:
- Line 375: Replace the generic expect messages with precise invariants: for the
shown line change the panic text on min_reads.last().expect(...) to something
like "min_reads must contain at least one element" (referencing the min_reads
variable in duplex_caller.rs); for the keyed lookups mentioned (the
HashMap/lookup sites around the other occurrences) update their expect messages
to include the missing key or context (e.g., "expected entry for key {key} in
<variable_name>"); and for the case noted as "exactly one record expected"
replace that generic expect with "exactly one record expected in
<collection_name>, found {len}" so the panic communicates the exact invariant
and context. Ensure each expect uses the actual variable/key names from
duplex_caller.rs to make the messages actionable.

In `@crates/fgumi-metrics/src/consensus.rs`:
- Around line 492-534: The test redundantly calls assert!(...is_some()) before
using expect(...) on the same Option values (kv_metrics.iter().find(...));
remove the duplicate assert!(...). Keep the expect(...) calls which already
provide a clear panic message and assertions on .value/.description for keys
like "raw_reads_considered", "raw_reads_rejected", "raw_reads_used",
"frac_raw_reads_used", "consensus_reads_emitted",
"raw_reads_rejected_for_insufficient_support", and
"raw_reads_rejected_for_minority_alignment" so the test remains concise and
still fails with the same diagnostics if a metric is missing.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: b1480bba-6a96-424a-9d93-fe17453038e5

📥 Commits

Reviewing files that changed from the base of the PR and between ea6e3fc and 8c62548.

📒 Files selected for processing (34)
  • crates/fgumi-bgzf/src/reader.rs
  • crates/fgumi-bgzf/src/writer.rs
  • crates/fgumi-consensus/src/base_builder.rs
  • crates/fgumi-consensus/src/caller.rs
  • crates/fgumi-consensus/src/codec_caller.rs
  • crates/fgumi-consensus/src/duplex_caller.rs
  • crates/fgumi-consensus/src/filter.rs
  • crates/fgumi-consensus/src/overlapping.rs
  • crates/fgumi-consensus/src/vanilla_caller.rs
  • crates/fgumi-consensus/src/vendored/bam_codec/encoder/mod.rs
  • crates/fgumi-consensus/src/vendored/bam_codec/encoder/quality_scores.rs
  • crates/fgumi-consensus/src/vendored/bam_codec/encoder/reference_sequence_id.rs
  • crates/fgumi-consensus/src/vendored/bam_codec/encoder/sequence.rs
  • crates/fgumi-dna/src/bitenc.rs
  • crates/fgumi-metrics/src/consensus.rs
  • crates/fgumi-metrics/src/duplex.rs
  • crates/fgumi-metrics/src/shared.rs
  • crates/fgumi-metrics/src/simplex.rs
  • crates/fgumi-raw-bam/src/builder.rs
  • crates/fgumi-raw-bam/src/fields.rs
  • crates/fgumi-raw-bam/src/noodles_compat.rs
  • crates/fgumi-raw-bam/src/raw_bam_record.rs
  • crates/fgumi-raw-bam/src/sequence.rs
  • crates/fgumi-raw-bam/src/tags.rs
  • crates/fgumi-raw-bam/src/testutil.rs
  • crates/fgumi-sam/src/alignment_tags.rs
  • crates/fgumi-sam/src/builder.rs
  • crates/fgumi-sam/src/clipper.rs
  • crates/fgumi-sam/src/lib.rs
  • crates/fgumi-simd-fastq/benches/fastq_parsing.rs
  • crates/fgumi-simd-fastq/src/parser.rs
  • crates/fgumi-simd-fastq/src/reader.rs
  • crates/fgumi-umi/src/assigner.rs
  • crates/fgumi-umi/src/lib.rs

@nh13 nh13 added the hygiene label Apr 4, 2026
- Fix terse/inconsistent expect messages in RecordPairBuilder::build()
  to match the descriptive style used elsewhere in builder.rs
- Replace generic "NonZero::new should succeed" messages with specific
  value-based messages like "8 is non-zero" for consistency
- Fix misleading "expected item at index" on HashMap::get() calls to
  say "key should be present in groups"
- Replace cryptic "failed to get sr" messages with "source read should
  be Some" across vanilla_caller.rs test code
- Fix ambiguous "source should be Some/Ok" to "source should be Some"
@nh13
nh13 temporarily deployed to github-actions April 4, 2026 07:05 — with GitHub Actions Inactive
@nh13

nh13 commented Apr 4, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Apr 4, 2026

Copy link
Copy Markdown
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@nh13

nh13 commented Apr 4, 2026

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Apr 4, 2026

Copy link
Copy Markdown
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@nh13
nh13 merged commit 2292e93 into main Apr 4, 2026
7 checks passed
@nh13
nh13 deleted the nh/unwrap-audit-crates branch April 4, 2026 20:47
@nh13 nh13 mentioned this pull request Apr 4, 2026

This branch was previously deployed

1 inactive deployment
github-actions — c457462f Deployed Apr 4, 2026 by nh13 via coverage #857
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant