oxc-project/oxc · warning · OxcDiagnostic

This function does not use any variables from its parent sco

Error message

This function does not use any variables from its parent scope

What it means

Diagnostic from oxlint's `unicorn/consistent-function-scoping` rule, anonymous variant: the flagged function has no name, so the message reads 'This function does not use any variables from its parent scope'. The function is defined in an inner scope yet captures nothing from it, so it can be hoisted to module scope to avoid being recreated on every call. Help text and dual labels (outer scope + function) are the same as the named variant.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/consistent_function_scoping.rs:30

    ast_util::{get_function_like_declaration, is_node_call_like_argument, outermost_paren_parent},
    context::LintContext,
    rule::{DefaultRuleConfig, Rule},
    utils::is_react_hook,
};

fn consistent_function_scoping(
    fn_span: Span,
    parent_scope_span: Option<Span>,
    parent_scope_kind: Option<&'static str>,
    function_name: Option<&str>,
) -> OxcDiagnostic {
    let function_label = if let Some(name) = function_name {
        format!("Function `{name}` does not capture any variables from its parent scope")
    } else {
        "This function does not use any variables from its parent scope".into()
    };

    let d = OxcDiagnostic::warn(function_label).with_help(match function_name {
        Some(name) => {
            format!("Move `{name}` to the outer scope to avoid recreating it on every call.")
        }
        None => {
            "Move this function to the outer scope to avoid recreating it on every call.".into()
        }
    });

    match parent_scope_span {
        Some(parent) => d.with_labels([
            parent.label("Outer scope where this function is defined"),
            fn_span.primary_label(if let Some(parent_scope_kind) = parent_scope_kind {
                format!(
                    "This function does not use any variables from the parent {parent_scope_kind}"
                )
            } else {
                "This function does not use any variables from here".into()
            }),

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Hoist the anonymous function to module scope and give it a name.
  2. If it genuinely belongs locally, reference an outer variable or pass state via parameters so the intent is explicit.
  3. Set `checkArrowFunctions: false` if arrow checking is too noisy for your style.
  4. Suppress intentional cases with an inline disable comment.

Example fix

// before
function makeGreeter() {
  return name => `Hello, ${name}`;
}
// after
const greet = name => `Hello, ${name}`;

function makeGreeter() {
  return greet;
}
Defensive patterns

Strategy: validation

Validate before calling

// .oxlintrc.json — silence arrows if only named functions should be checked
{
  "rules": {
    "unicorn/consistent-function-scoping": ["warn", { "checkArrowFunctions": false }]
  }
}

Prevention

When it happens

Trigger: Anonymous function expressions and arrow functions assigned or passed inline whose bodies reference only their own parameters and globals, e.g. `const doBar = bar => bar === 'bar';` inside `doFoo`, or `arr.map(x => x * 2)` inside a frequently-called function (subject to call-argument exemptions in the rule).

Common situations: Arrow helpers inside components and reducers; callback factories inside loops; enabling unicorn presets on callback-heavy code and getting many findings at once.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/012aacd53950fd9b. Report an issue: GitHub.