Skip to content

fix(foundation): collapse nested if guards in parse_gemini_sse (clippy::collapsible_if #1295) - #1650

Open
SH20RAJ wants to merge 1 commit into
mofa-org:mainfrom
SH20RAJ:fix/1295-gemini-sse-collapsible-if
Open

SH20RAJ wants to merge 1 commit into
mofa-org:mainfrom
SH20RAJ:fix/1295-gemini-sse-collapsible-if

Conversation

@SH20RAJ

@SH20RAJ SH20RAJ commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #1295.

Running `cargo clippy --workspace --all-features -- -D warnings` on
`main` produces three `clippy::collapsible_if` warnings in
`crates/mofa-foundation/src/llm/google.rs` inside the end-of-stream
branch of `parse_gemini_sse`:

warning: this if statement can be collapsed
  --> crates/mofa-foundation/src/llm/google.rs:450:25

Root Cause

The end-of-stream arm of the SSE parser had three levels of nested
`if` / `if let` / `if` guards:

if !buf.trim().is_empty() {
    if let Some(json_str) = buf.trim().strip_prefix("data: ") {
        if json_str.trim() != "[DONE]" {
            if let Ok(chunk) = serde_json::from_str::<...>(json_str) { ... }
        }
    }
}

Fix

Collapse the outer three guards into a single flat chain using
`strip_prefix` + `.filter()` so there is only one level of nesting
left (the inner `if let Ok(chunk)`):

if let Some(json_str) = buf
    .trim()
    .strip_prefix("data: ")
    .filter(|s| !s.trim().is_empty() && s.trim() != "[DONE]")
{
    if let Ok(chunk) = serde_json::from_str::<GeminiStreamChunk>(json_str) { ... }
}

Semantics are identical: the filter checks both the non-empty guard and
the [DONE] sentinel in one step.

Verification

cargo clippy -p mofa-foundation -- -D warnings
cargo test   -p mofa-foundation

Both pass clean.

Checklist

…apsible_if)

Fixes mofa-org#1295

Three nested if / if-let / if guards inside the end-of-stream branch of
parse_gemini_sse triggered clippy::collapsible_if.  Collapse them into a
single flat chain using strip_prefix + .filter() combined with one outer
if-let, eliminating the nesting without changing any behaviour.

Running cargo clippy -p mofa-foundation -- -D warnings now passes clean.
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.

chore: Fix clippy::collapsible_if warnings in Gemini SSE parser

1 participant