oxc-project/oxc · warning · OxcDiagnostic

Empty constructors are unnecessary

Error message

Empty constructors are unnecessary

What it means

Diagnostic from `no-useless-constructor` (empty-constructor variant). A class declares `constructor() {}` with no parameters and no body statements. Such a constructor is identical to the default constructor the spec supplies, so it is dead code. Oxc reports the constructor span with help 'Remove the constructor or add code to it.'

Source

Thrown at crates/oxc_linter/src/rules/eslint/no_useless_constructor.rs:18

use oxc_ast::{
    AstKind,
    ast::{
        Argument, BindingPattern, CallExpression, Expression, FormalParameterRest,
        FormalParameters, FunctionBody, MethodDefinition, Statement, TSAccessibility,
    },
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};

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

/// ```js
/// class A { constructor(){} }
/// ```
fn no_empty_constructor(constructor_span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Empty constructors are unnecessary")
        .with_label(constructor_span)
        .with_help("Remove the constructor or add code to it.")
}

/// ```js
/// class A { }
/// class B extends A {
///     constructor() {
///         super();
///     }
/// }
/// ```
fn no_redundant_super_call(constructor_span: Span, super_span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Redundant super call in constructor")
        .with_labels([
            constructor_span.primary_label("This constructor is unnecessary,"),
            super_span.label("because it only passes arguments through to the superclass"),
        ])

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Delete the empty constructor entirely; the class works the same via its implicit default constructor.
  2. If you intended real initialization, add the missing statements to the body.
  3. Apply the fix automatically with `oxlint --fix`.
  4. Keep it only if a decorator or mixin framework requires an explicit constructor, and disable the rule for that line.

Example fix

// before
class A {
  constructor() {}
}

// after
class A {}
Defensive patterns

Strategy: validation

Validate before calling

class A {
  constructor() {}
}
// check: does the constructor body have zero statements? then delete it

Prevention

When it happens

Trigger: `class A { constructor() {} }` or `constructor() { /* only comments */ }`. Fires on ClassElement constructors whose body is empty; derived classes whose body is only `super(...)` are handled by the separate redundant-super diagnostic.

Common situations: Scaffolding from class boilerplate templates; refactors that removed initialization code but left the shell; TypeScript code where `public x: number` parameter properties were converted to fields.

Related errors


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