Skip to content

🧹 Replace unwrap() with safe fallback in TtlLruCache - #8

Open
LeandroPG19 wants to merge 1 commit into
mainfrom
fix-cache-unwrap-rust-10090660793338763548
Open

🧹 Replace unwrap() with safe fallback in TtlLruCache#8
LeandroPG19 wants to merge 1 commit into
mainfrom
fix-cache-unwrap-rust-10090660793338763548

Conversation

@LeandroPG19

Copy link
Copy Markdown
Owner

This PR addresses a code health issue in the Rust search cache by removing unsafe unwrap() calls when creating a NonZeroUsize for cache capacity. These have been replaced with unwrap_or(NonZeroUsize::MIN), which ensures the cache always has a minimum size of 1 even if the configuration is invalid, preventing potential panics.

Changes:

  • In TtlLruCache::new(), replaced .unwrap() with .unwrap_or(NonZeroUsize::MIN).
  • In TtlLruCache::with_config(), replaced the .max(1).unwrap() pattern with .unwrap_or(NonZeroUsize::MIN).

Verification:

The changes were verified using a standalone Rust script that confirmed identical behavior between the old and new logic for both zero and non-zero inputs, while providing greater safety. Existing tests in the codebase cover the cache functionality and were reviewed for compatibility.


PR created automatically by Jules for task 10090660793338763548 started by @LeandroPG19

Replaced `unwrap()` calls when initializing `NonZeroUsize` for the
`LruCache` capacity. Used `unwrap_or(NonZeroUsize::MIN)` to provide
a safe fallback of 1 instead of panicking, improving the
robustness and maintainability of the search cache.

- Modified `TtlLruCache::new()`
- Modified `TtlLruCache::with_config()`

Verified with a standalone Rust script since `cargo test` was
unavailable due to network constraints.

Co-authored-by: LeandroPG19 <151863062+LeandroPG19@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

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.

1 participant