yewstack/yew · error · syn::Error

missing condition for `if` expression

Error message

missing condition for `if` expression

What it means

`HtmlIf::parse` reads the `if` token and then parses the condition with `Expr::parse_without_eager_brace` (html_if.rs:29). If the parsed condition is an empty block `{}`, there was never a real condition before the then-block, so the macro errors on the condition's span. Because `html!` children are brace-delimited, `if {} {…}` looks plausible but leaves the if with nothing to test.

Source

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

    cond: Box<Expr>,
    then_branch: HtmlRootBraced,
    else_branch: Option<(Token![else], Box<HtmlRootBracedOrIf>)>,
}

impl PeekValue<()> for HtmlIf {
    fn peek(cursor: Cursor) -> Option<()> {
        let (ident, _) = cursor.ident()?;
        (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| {

View on GitHub (pinned to 0e4a05472f)

Solutions

  1. Write a real condition before the then-block: `if is_ready { … }`
  2. If you only wanted to group children, use a fragment `<></>` instead of an empty `if`
  3. Bind complex conditions to a named `bool` above the macro and reference that identifier

Example fix

// before
html! { if {} { <p>{ "never compiles" }</p> } }

// after
html! { if is_ready { <p>{ "ready" }</p> } }
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: `html! { if {} { <p/> } }` — the token immediately after `if` is an empty block instead of a boolean expression.

Common situations: Leaving an IDE snippet placeholder `{}` after `if`, intending the block as a grouping wrapper (like a React fragment), or deleting a condition while editing a template.

Related errors


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