oxc-project/oxc · error · OxcDiagnostic

Expected to always return a value in getter.

Error message

Expected to always return a value in getter.

What it means

Oxlint's port of the ESLint rule getter-return (crates/oxc_linter/src/rules/eslint/getter_return.rs:28): a getter must return a value on every code path. The rule checks named getters plus getters passed to a watchlist of four defineProperty-style APIs (METHODS_TO_WATCH_FOR in this file). The config carries `allowImplicit: bool` — when true, a bare `return;` counts as returning undefined; by default it does not, and a getter that can fall through without an expression is reported.

Source

Thrown at crates/oxc_linter/src/rules/eslint/getter_return.rs:29

        Direction,
        visit::{Control, DfsEvent, set_depth_first_search},
    },
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use schemars::JsonSchema;
use serde::Deserialize;
use serde_json::Value;

use crate::{
    AstNode,
    context::{ContextHost, LintContext},
    rule::{DefaultRuleConfig, Rule},
};

fn getter_return_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Expected to always return a value in getter.")
        .with_help("Return a value from all code paths in getter.")
        .with_label(span)
}

#[derive(Debug, Default, Clone, JsonSchema, Deserialize)]
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
pub struct GetterReturn {
    /// When set to `true`, allows getters to implicitly return `undefined` with a `return` statement containing no expression.
    pub allow_implicit: bool,
}

const METHODS_TO_WATCH_FOR: [(&str, &str); 4] = [
    ("Object", "defineProperty"),
    ("Reflect", "defineProperty"),
    ("Object", "create"),
    ("Object", "defineProperties"),
];

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Return a value on every path: add a final `return fallback;` after the branches.
  2. If undefined is intended, return it explicitly or set "allowImplicit": true in .oxlintrc.json.
  3. If the member performs work rather than exposing state, convert the getter to a method.

Example fix

// before
get label() {
  if (this.raw) return this.raw.trim();
}

// after
get label() {
  if (this.raw) return this.raw.trim();
  return '';
}
Defensive patterns

Strategy: validation

Validate before calling

// .oxlintrc.json — permit bare `return;` if undefined is intentional
{
  "rules": {
    "getter-return": ["error", { "allowImplicit": false }]
  }
}

Prevention

When it happens

Trigger: A getter with a branch that falls through (`get x() { if (c) return v; }`), or a getter containing only `return;` while allowImplicit is false. Also fires for `Object.defineProperty(obj, 'x', { get() { ... } })` shapes.

Common situations: Adding an early guard clause to a getter and forgetting the fallthrough return; ESLint configs where allowImplicit was enabled but not carried to oxlint; generated getters.

Related errors


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