yewstack/yew · error · syn::Error

this `if` expression has a condition, but no block

Error message

this `if` expression has a condition, but no block

What it means

After parsing the `if` condition, `HtmlIf::parse` checks `input.is_empty()` (html_if.rs:33-38); if the macro input ends right after the condition, the mandatory then-block is missing and this error points at the condition span. `html!` if-expressions always require a brace block for the then branch, unlike bare expression children which need no braces.

Source

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

        (ident == "if").then_some(())
    }
}

impl Parse for HtmlIf {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let if_token = input.parse()?;
        let cond = Box::new(input.call(Expr::parse_without_eager_brace)?);
        match &*cond {
            Expr::Block(syn::ExprBlock { block, .. }) if block.stmts.is_empty() => {
                return Err(syn::Error::new(
                    cond.span(),
                    "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))

View on GitHub (pinned to 0e4a05472f)

Solutions

  1. Add the then-block: `if logged_in { <Dashboard /> }`
  2. For a value chosen by condition, compute it in plain Rust first (`let text = if c { "a" } else { "b" };`) and interpolate `{ text }`
  3. Add `else { … }` for the alternative branch

Example fix

// before
html! { if logged_in }

// after
html! { if logged_in { <Dashboard /> } else { <Login /> } }
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: `html! { if logged_in }` — a condition with nothing after it (end of the macro input).

Common situations: Expecting a ternary operator that Rust does not have, truncating the expression while editing, or splitting an `html!` call across lines and forgetting the block on the last line.

Related errors


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