{"record":{"id":"0f87b44d5a1c750f","repo":"oxc-project/oxc","slug":"unexpected-if-as-the-only-statement-in-an-else","errorCode":null,"errorMessage":"Unexpected `if` as the only statement in an `else` block","messagePattern":"Unexpected `if` as the only statement in an `else` block","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/eslint/no_lonely_if.rs","lineNumber":10,"sourceCode":"use crate::{AstNode, context::LintContext, rule::Rule};\nuse oxc_ast::AstKind;\nuse oxc_ast::ast::{IfStatement, Statement};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::Span;\n\nfn no_lonely_if_diagnostic(lonely_if: &IfStatement) -> OxcDiagnostic {\n    let span = Span::sized(lonely_if.span.start, 2);\n    OxcDiagnostic::warn(\"Unexpected `if` as the only statement in an `else` block\")\n        .with_help(\"Consider using `else if` instead.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct NoLonelyIf;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Disallow `if` statements as the only statement in `else` blocks.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// When an `if` statement is the only statement in an `else` block, it is often clearer to use\n    /// an `else if` instead.\n    ///\n    /// ### Examples","sourceCodeStart":1,"sourceCodeEnd":28,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/no_lonely_if.rs#L1-L28","documentation":"Diagnostic from oxlint's eslint/no-lonely-if rule (crates/oxc_linter/src/rules/eslint/no_lonely_if.rs:10). It reports an if statement that is the only statement inside an else block: 'if (a) {...} else { if (b) {...} }'. The idiomatic form is 'else if', which avoids the extra indentation and brace level. The label is placed on the span of the lonely if keyword.","triggerScenarios":"An else clause whose block contains exactly one statement and that statement is an if (with or without its own else chain); commonly produced when adding a new condition to an existing if/else by hand.","commonSituations":"Incrementally grown conditionals during bug fixes; code formatted with the else block always braced; developers unaware that 'else if' is just else followed by an if statement.","solutions":["Merge into an else-if chain: 'if (a) {...} else if (b) {...}'","If the nested if has its own else, flatten the whole chain with else-if at each level","When the chain grows long, consider a lookup table, switch, or early-return guard clauses instead"],"exampleFix":"// before\nif (a) {\n  doA();\n} else {\n  if (b) {\n    doB();\n  }\n}\n\n// after\nif (a) {\n  doA();\n} else if (b) {\n  doB();\n}","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["When adding a condition to an if/else chain, append 'else if' instead of nesting inside the else block","Prefer guard clauses/early return over growing else chains","Enable no-lonely-if in the shared config so reviewers see it automatically"],"tags":["lint","javascript","if-else","style","readability"],"backgroundTag":"lonely-if-else-block","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}