Skip to content

rust: avoid using unwrap()#117

Merged
sourya-deepsource merged 1 commit intoDeepSourceCorp:masterfrom
hrideshmg:rust_avoid_unwrap
Mar 6, 2025
Merged

rust: avoid using unwrap()#117
sourya-deepsource merged 1 commit intoDeepSourceCorp:masterfrom
hrideshmg:rust_avoid_unwrap

Conversation

@hrideshmg
Copy link
Contributor

@hrideshmg hrideshmg commented Feb 24, 2025

Description

This PR adds a new rust checker to detect the use of .unwrap() in Rust code. While .unwrap() is convenient, it can lead to unexpected panics and is only meant for quick prototyping (see rust error handling). This checker is flagged as an antipattern since it does not impact security much.

Detection Logic

The checker flags the following cases:

  • Calling .unwrap() on an Option or Result

Recommended Alternatives

Instead of using .unwrap(), consider:

  • Handling errors explicitly using match or if let
  • Using .unwrap_or_else() to provide a fallback value or handle errors gracefully
  • Using .expect() with a meaningful message if a panic is intentional

Exclusions

To reduce noise, the checker does not flag occurrences in test files, vendor dependencies, or test directories.

@vercel
Copy link

vercel bot commented Feb 24, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
globstar ⬜️ Ignored (Inspect) Visit Preview Feb 24, 2025 7:35pm

@sourya-deepsource sourya-deepsource merged commit 9de6d96 into DeepSourceCorp:master Mar 6, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants