feat(validation): guard public Rust examples against unwrap#132
Conversation
- Add repository-owned Semgrep rules for unwrap and expect usage in public doctests, examples, and benchmarks. - Add fixture-based Semgrep rule tests and include them in the lint workflow. - Update examples and benchmarks to model typed fallible flow or operation-labeled benchmark failures. Closes #125
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThis PR adds Semgrep guardrails to prevent ChangesSemgrep Guardrails and Error Handling Compliance
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning Review ran into problems🔥 ProblemsGit: Failed to clone repository. Please run the Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@scripts/check_semgrep_fixtures.py`:
- Line 60: The code assumes data["results"] and result["check_id"] exist and
will raise KeyError; update the aggregation in scripts/check_semgrep_fixtures.py
to guard the shape returned by _semgrep_results(): first verify
data.get("results") is a list (otherwise log to stderr and exit(1)), then build
actual using a safe lookup like collections.Counter(result.get("check_id",
"<missing>") for result in data.get("results", [])) or skip entries without
check_id and log which entries were malformed; ensure the script follows the
existing stderr-plus-exit-1 contract when encountering a non-list results or
missing check_id fields.
- Line 13: The RULE_ANNOTATION regex currently captures both "ruleid" and
"todoruleid" causing todoruleid to be counted in expected; update
RULE_ANNOTATION so it only matches the canonical "ruleid" annotation (or filter
the parsed annotations in expected to exclude any "todoruleid" entries) and
ensure the variable expected only contains real rule ids. Also harden
SEMGREP_JSON parsing: validate that data is a dict with a "results" list before
building actual, iterate results defensively and check each result contains
"check_id" (or skip/collect/report malformed entries), and raise or log a clear
error if the JSON shape is unexpected instead of letting a KeyError propagate;
reference variables/functions: RULE_ANNOTATION, expected, actual, data, results,
result, check_id.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: e074295f-b077-48c2-97f8-4db23d148437
📒 Files selected for processing (11)
benches/exact.rsbenches/vs_linalg.rsexamples/const_det_4x4.rsexamples/exact_sign_3x3.rsexamples/ldlt_solve_3x3.rsjustfilepyproject.tomlscripts/check_semgrep_fixtures.pysemgrep.yamltests/semgrep/doctests/unwrap_expect.txttests/semgrep/src/project_rules/bench_example_usage.rs
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #132 +/- ##
=======================================
Coverage 99.64% 99.64%
=======================================
Files 5 5
Lines 2832 2832
=======================================
Hits 2822 2822
Misses 10 10
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
- Ignore non-canonical todoruleid annotations when counting expected rule hits. - Reject malformed Semgrep JSON results with clear stderr diagnostics instead of propagating KeyError.
Closes #125
Summary by CodeRabbit
Refactor
Tests
Chores