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
- Write a real condition before the then-block: `if is_ready { … }`
- If you only wanted to group children, use a fragment `<></>` instead of an empty `if`
- 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
- Always type a boolean expression after `if` in `html!`; if grouping was the goal, use `<></>`
- Replace IDE snippet placeholders before compiling
- Compute complex conditions as named `bool`s in plain Rust so the template line stays short and obviously conditioned
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
- this `if` expression has a condition, but no block
- expected block or `if` after `else`
- missing expression for `match`
- this `match` expression has a scrutinee, but no body
- missing condition for `while` expression
AI-assisted analysis of yewstack/yew@0e4a05472f (2026-08-22).
Data as JSON: /api/errors/e585fe93c4c77af9.
Report an issue: GitHub.