yewstack/yew · error · syn::Error

expected block or `if` after `else`

Error message

expected block or `if` after `else`

What it means

When an `else` token follows the then-block, `HtmlIf::parse` requires more input after it; if the stream is empty immediately after `else`, this error is raised on the else token's span (html_if.rs:46-52). An `else` in `html!` must be followed by a brace block or another `if` expression, mirroring plain Rust.

Source

Thrown at packages/yew-macro/src/html_tree/html_if.rs:51

                    "missing condition for `if` expression",
                ));
            }
            _ => {}
        }
        if input.is_empty() {
            return Err(syn::Error::new(
                cond.span(),
                "this `if` expression has a condition, but no block",
            ));
        }

        let then_branch = input.parse()?;
        let else_branch = input
            .parse::<Token![else]>()
            .ok()
            .map(|else_token| {
                if input.is_empty() {
                    return Err(syn::Error::new(
                        else_token.span(),
                        "expected block or `if` after `else`",
                    ));
                }

                input.parse().map(|branch| (else_token, branch))
            })
            .transpose()?;

        Ok(HtmlIf {
            if_token,
            cond,
            then_branch,
            else_branch,
        })
    }
}

View on GitHub (pinned to 0e4a05472f)

Solutions

  1. Complete the branch: `else { <Fallback /> }`
  2. Chain conditions with `else if` when the fallback is itself conditional
  3. Delete the dangling `else` if no fallback is needed

Example fix

// before
html! { if cond { <a/> } else }

// after
html! { if cond { <a/> } else { <b/> } }
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: `html! { if cond { <a/> } else }` — a dangling `else` at the end of the macro input.

Common situations: Starting to add a fallback branch and not finishing it, a placeholder `else` left behind by a snippet, or refactoring branches and leaving the tail orphaned.

Related errors


AI-assisted analysis of yewstack/yew@0e4a05472f (2026-08-22). Data as JSON: /api/errors/8f274b34a1da8d24. Report an issue: GitHub.