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
- Add the then-block: `if logged_in { <Dashboard /> }`
- For a value chosen by condition, compute it in plain Rust first (`let text = if c { "a" } else { "b" };`) and interpolate `{ text }`
- 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
- Remember Rust has no ternary — every `if` in `html!` needs `{ … }` immediately after the condition
- Close each `html!` block on the same line as its last branch while drafting, then format
- For conditional text, compute the value in plain Rust and interpolate it as `{ value }`
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
- missing condition for `if` expression
- expected block or `if` after `else`
- this `match` expression has a scrutinee, but no body
- this `while` expression has a condition, but no block
- missing expression for `match`
AI-assisted analysis of yewstack/yew@0e4a05472f (2026-08-22).
Data as JSON: /api/errors/bbc5b94e25aa6c75.
Report an issue: GitHub.