{"record":{"id":"26fd0dc6bc85386c","repo":"oxc-project/oxc","slug":"function-name-does-not-capture-any-variables-f","errorCode":null,"errorMessage":"Function `{name}` does not capture any variables from its parent scope","messagePattern":"Function `(.+?)` does not capture any variables from its parent scope","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/unicorn/consistent_function_scoping.rs","lineNumber":30,"sourceCode":"    ast_util::{get_function_like_declaration, is_node_call_like_argument, outermost_paren_parent},\n    context::LintContext,\n    rule::{DefaultRuleConfig, Rule},\n    utils::is_react_hook,\n};\n\nfn consistent_function_scoping(\n    fn_span: Span,\n    parent_scope_span: Option<Span>,\n    parent_scope_kind: Option<&'static str>,\n    function_name: Option<&str>,\n) -> OxcDiagnostic {\n    let function_label = if let Some(name) = function_name {\n        format!(\"Function `{name}` does not capture any variables from its parent scope\")\n    } else {\n        \"This function does not use any variables from its parent scope\".into()\n    };\n\n    let d = OxcDiagnostic::warn(function_label).with_help(match function_name {\n        Some(name) => {\n            format!(\"Move `{name}` to the outer scope to avoid recreating it on every call.\")\n        }\n        None => {\n            \"Move this function to the outer scope to avoid recreating it on every call.\".into()\n        }\n    });\n\n    match parent_scope_span {\n        Some(parent) => d.with_labels([\n            parent.label(\"Outer scope where this function is defined\"),\n            fn_span.primary_label(if let Some(parent_scope_kind) = parent_scope_kind {\n                format!(\n                    \"This function does not use any variables from the parent {parent_scope_kind}\"\n                )\n            } else {\n                \"This function does not use any variables from here\".into()\n            }),","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/consistent_function_scoping.rs#L12-L48","documentation":"Diagnostic from oxlint's `unicorn/consistent-function-scoping` rule. It reports functions declared inside another function that capture no variables from the enclosing scope — they can live at module scope instead, avoiding closure re-creation on every call and giving engines better optimization chances. This named variant is used when the function has a name; the diagnostic labels both the outer scope ('Outer scope where this function is defined') and the function itself, and the help text says to move it outward. Arrow functions are checked too unless `checkArrowFunctions: false`; React hook names are exempt.","triggerScenarios":"A named inner function (or const-assigned function) whose body references only its own parameters, locals, or globals — e.g. the `doBar` helper inside `doFoo` in the rule docs. The parent scope can be a function, block, or IIFE; the check is that no reference inside the function resolves to a binding of the outer scope.","commonSituations":"Helper functions defined inside React components, event handlers, or route handlers; per-request server code recreating identical closures; teams enabling unicorn presets on an existing codebase.","solutions":["Hoist the named function to module scope, passing anything it needs as parameters.","If it must stay local, make the dependency explicit by actually referencing an outer variable, then re-run.","Reduce noise from arrows by configuring `checkArrowFunctions: false` in .oxlintrc.json.","Disable the rule inline for deliberate cases (e.g. functions kept local for readability in tests)."],"exampleFix":"// before\nexport function doFoo(foo) {\n  function doBar(bar) {\n    return bar === 'bar';\n  }\n  return doBar;\n}\n// after\nfunction doBar(bar) {\n  return bar === 'bar';\n}\n\nexport function doFoo(foo) {\n  return doBar;\n}","handlingStrategy":"validation","validationCode":"// .oxlintrc.json — tune the rule before enabling on arrow-heavy code\n{\n  \"rules\": {\n    \"unicorn/consistent-function-scoping\": [\"warn\", { \"checkArrowFunctions\": false }]\n  }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Hoist helper functions that use no outer state to module scope as you write them.","Define per-call helpers outside components and handlers; pass dependencies as parameters.","When hoisting is wrong, make the closure's dependence explicit by referencing an outer variable."],"tags":["oxlint","lint","unicorn","scoping","functions","performance"],"backgroundTag":"unnecessary-closures","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"}