{"record":{"id":"53194304a9a15bcc","repo":"oxc-project/oxc","slug":"unexpected-return-statement-in-constructor","errorCode":null,"errorMessage":"Unexpected return statement in constructor.","messagePattern":"Unexpected return statement in constructor\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/eslint/no_constructor_return.rs","lineNumber":13,"sourceCode":"use oxc_ast::{\n    AstKind,\n    ast::{MethodDefinition, MethodDefinitionKind},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_semantic::NodeId;\nuse oxc_span::Span;\n\nuse crate::{AstNode, context::LintContext, rule::Rule};\n\nfn no_constructor_return_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Unexpected return statement in constructor.\")\n        .with_help(\"Remove the return statement from the constructor. If you need early exit, use a bare `return;` with no value.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct NoConstructorReturn;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Disallow returning a value from a constructor.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// In JavaScript, returning a value in the constructor of a class may be a mistake.\n    /// Forbidding this pattern prevents mistakes resulting from unfamiliarity with the language or a copy-paste error.\n    ///\n    /// ### Examples","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/no_constructor_return.rs#L1-L31","documentation":"Diagnostic from the oxlint rule `no-constructor-return` (crates/oxc_linter/src/rules/eslint/no_constructor_return.rs). It fires on a `return` statement carrying a value inside a class MethodDefinition whose kind is `constructor`. Constructor return values are discarded when invoked with `new` (unless returning an object, which replaces `this` — a niche source of bugs), so returning a value signals confusion about constructor semantics.","triggerScenarios":"A ReturnStatement with a non-null `argument` lexically inside a MethodDefinition with `kind == Constructor` — e.g. `class A { constructor() { return this.value; } }` or `constructor(x) { if (!x) return null; }`.","commonSituations":"Factory-ish classes converted from functions that returned values; early-exit validation written as `return null` in constructors; code moved between `function Foo()` and `class Foo` during modernization where the old return became meaningless or silently changes behavior when it returns an object.","solutions":["Remove the return statement so construction always yields `this`.","For early exit, use a bare `return;` (no value) as the help text suggests, or throw on invalid input.","If callers need a value or null on failure, convert to a factory function or static method (`Foo.create()` returning `new Foo()` or null).","Never rely on returning a different object from a constructor unless that substitution is the documented design."],"exampleFix":"// before\nclass Connection {\n  constructor(cfg) {\n    if (!cfg.url) return null;\n  }\n}\n\n// after\nclass Connection {\n  constructor(cfg) {\n    if (!cfg.url) throw new Error('url required');\n  }\n}","handlingStrategy":"validation","validationCode":"// Gate: `return <value>;` inside a constructor (heuristic on class bodies)\nconst ctorReturn = /constructor\\s*\\([^)]*\\)\\s*\\{[^}]*\\breturn\\s+[^;\\s]/.test(src);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use bare `return;` for early exits; throw for invalid construction input.","Move value-returning construction to factory/static create() methods.","Remember constructor return values are discarded with new (unless replacing this)."],"tags":["lint","constructor","class","return-value","oxlint"],"backgroundTag":"constructor-return-value","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"}