{"record":{"id":"9e4f55652387cd14","repo":"oxc-project/oxc","slug":"block-is-unnecessary","errorCode":null,"errorMessage":"Block is unnecessary.","messagePattern":"Block is unnecessary\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/eslint/no_lone_blocks.rs","lineNumber":10,"sourceCode":"use oxc_ast::AstKind;\nuse oxc_ast::ast::{Declaration, VariableDeclarationKind};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\n\nuse crate::{AstNode, context::LintContext, rule::Rule};\n\nfn no_lone_blocks_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Block is unnecessary.\")\n        .with_help(\"Remove the unnecessary block statement. If you need to limit variable scope, consider using a function or module instead.\")\n        .with_label(span)\n}\n\nfn no_nested_lone_blocks_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Nested block is redundant.\")\n        .with_help(\"Remove the redundant nested block statement.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct NoLoneBlocks;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Disallows unnecessary standalone block statements.\n    ///","sourceCodeStart":1,"sourceCodeEnd":28,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/no_lone_blocks.rs#L1-L28","documentation":"Diagnostic from oxlint's eslint/no-lone-blocks rule (crates/oxc_linter/src/rules/eslint/no_lone_blocks.rs:10). It reports a standalone block statement '{ ... }' that serves no purpose: it does not create a scope that matters unless it contains let/const/class/function declarations (per modern ESLint semantics, blocks containing such lexical declarations are 'necessary' and not reported). Leftover braces from deleted if/try statements are the classic source.","triggerScenarios":"A block statement not attached to if/for/while/try/function that contains only statements using var or expressions: '{ doWork(); }' at top level or inside another block; braces left after removing an 'if (debug)' wrapper.","commonSituations":"Deleting an if/for/try line but leaving its braces; old scoping hacks from pre-ES2015 code that used blocks + var for 'namespacing'; merging code where a block was used to avoid variable name clashes that no longer exist.","solutions":["Remove the braces and keep the inner statements, dedenting them","If the block was used for scope, replace it with an IIFE or move the code into a function (or use let/const for real block scoping)","Check for accidentally deleted control-flow keywords above the block (if/else/for/while) and restore them if the braces were meant to belong to one"],"exampleFix":"// before\n{\n  var x = 1;\n  console.log(x);\n}\n\n// after\nvar x = 1;\nconsole.log(x);","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["When deleting an if/for/try, delete both its keyword and its braces in one edit","Use let/const (real block scoping) instead of bare blocks plus var","Run a formatter/linter on save so leftover braces are flagged immediately"],"tags":["lint","javascript","blocks","dead-code","style"],"backgroundTag":"lone-block-statements","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}