{"record":{"id":"bbc5b94e25aa6c75","repo":"yewstack/yew","slug":"this-if-expression-has-a-condition-but-no-block","errorCode":null,"errorMessage":"this `if` expression has a condition, but no block","messagePattern":"this `if` expression has a condition, but no block","errorType":"validation","errorClass":"syn::Error","httpStatus":null,"severity":"error","filePath":"packages/yew-macro/src/html_tree/html_if.rs","lineNumber":39,"sourceCode":"        (ident == \"if\").then_some(())\n    }\n}\n\nimpl Parse for HtmlIf {\n    fn parse(input: ParseStream) -> syn::Result<Self> {\n        let if_token = input.parse()?;\n        let cond = Box::new(input.call(Expr::parse_without_eager_brace)?);\n        match &*cond {\n            Expr::Block(syn::ExprBlock { block, .. }) if block.stmts.is_empty() => {\n                return Err(syn::Error::new(\n                    cond.span(),\n                    \"missing condition for `if` expression\",\n                ));\n            }\n            _ => {}\n        }\n        if input.is_empty() {\n            return Err(syn::Error::new(\n                cond.span(),\n                \"this `if` expression has a condition, but no block\",\n            ));\n        }\n\n        let then_branch = input.parse()?;\n        let else_branch = input\n            .parse::<Token![else]>()\n            .ok()\n            .map(|else_token| {\n                if input.is_empty() {\n                    return Err(syn::Error::new(\n                        else_token.span(),\n                        \"expected block or `if` after `else`\",\n                    ));\n                }\n\n                input.parse().map(|branch| (else_token, branch))","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/yewstack/yew/blob/0e4a05472fac4e5fce1befe60fa4a1e43a36b6a3/packages/yew-macro/src/html_tree/html_if.rs#L21-L57","documentation":"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.","triggerScenarios":"`html! { if logged_in }` — a condition with nothing after it (end of the macro input).","commonSituations":"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.","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"],"exampleFix":"// before\nhtml! { if logged_in }\n\n// after\nhtml! { if logged_in { <Dashboard /> } else { <Login /> } }","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["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 }`"],"tags":["rust","yew","html-macro","if-expression","syntax","missing-block"],"backgroundTag":"incomplete-if-expression","analyzedSha":"0e4a05472fac4e5fce1befe60fa4a1e43a36b6a3","analyzedAt":"2026-08-22T21:16:31.212Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}