deprecated-consistency

Same as deprecated_consistency in Dart lints

Group
Style
Path
lint/style/deprecated-consistency
Default
On (recommended)
Origin
Dart lints
Rule
216 of 512
recommended

Flags deprecations that are not carried through to related declarations.

Deprecating a class implies deprecating its constructors, and a deprecated field implies that the initializing formal (this.field) that sets it is deprecated too — and vice versa. Leaving one side unmarked means callers get no warning on the path they actually use.

@deprecated
class A {
  A();                        // flagged: constructor not deprecated
}

class B {
  B({this.field});            // flagged: parameter not deprecated
  @Deprecated('Use x')
  Object? field;
}

Private fields are not checked. Deprecation metadata is resolved to dart:core, including prefixed references and named Deprecated constructors; user-defined lookalikes do not count. Primary constructors are checked separately from their enclosing type; only their own body metadata deprecates them. Header initializing formals participate in the same field/parameter consistency checks.

Invalid

example.dartdart
@deprecated
class A {
  A();
}

@Deprecated('Use B2 instead')
class B {
  B.named();

  @Deprecated('ok')
  B();
}

class C {
  C({this.field});

  @Deprecated('Use x instead')
  Object? field;
}

class D {
  D({@Deprecated('Use y instead') this.field});

  Object? field;
}

class E {
  E(this.value, {@deprecated this.legacy});

  final int value;
  final int? legacy;
}

@deprecated
enum Mode {
  on(1),
  off(0);

  const Mode(this.code);
  final int code;
}
@Deprecated.extend()
class Extendable {
  Extendable();
}

@Deprecated.implement()
class Implementable {
  Implementable();
}
@deprecated
extension type Representation(int value) {
  Representation.named(this.value);
}

@deprecated
extension type PrivateRepresentation._(int value) {}

typedef Dep = Deprecated;

@Dep('old')
class Aliased {
  Aliased();
}

@Dep.extend()
class AliasedNamed {
  AliasedNamed();
}

@deprecated
extension type const ConstRepresentation(int value) {}

@deprecated
extension type const ConstNamedRepresentation._(int value) {}
Constructors in a deprecated class should be deprecated.
2class A {
3 A();
Constructors in a deprecated class should be deprecated.
7class B {
8 B.named();
Parameters that initialize a deprecated field should be deprecated.
14class C {
15 C({this.field});
Fields that are initialized by a deprecated parameter should be deprecated.
23
24 Object? field;
Fields that are initialized by a deprecated parameter should be deprecated.
30 final int value;
31 final int? legacy;
Constructors in a deprecated class should be deprecated.
38
39 const Mode(this.code);
Constructors in a deprecated class should be deprecated.
43class Extendable {
44 Extendable();
Constructors in a deprecated class should be deprecated.
48class Implementable {
49 Implementable();
Constructors in a deprecated class should be deprecated.
51@deprecated
52extension type Representation(int value) {
Constructors in a deprecated class should be deprecated.
52extension type Representation(int value) {
53 Representation.named(this.value);
Constructors in a deprecated class should be deprecated.
56@deprecated
57extension type PrivateRepresentation._(int value) {}
Constructors in a deprecated class should be deprecated.
62class Aliased {
63 Aliased();
Constructors in a deprecated class should be deprecated.
67class AliasedNamed {
68 AliasedNamed();
Constructors in a deprecated class should be deprecated.
71@deprecated
72extension type const ConstRepresentation(int value) {}
Constructors in a deprecated class should be deprecated.
74@deprecated
75extension type const ConstNamedRepresentation._(int value) {}

Valid

example.dartdart
@deprecated
class A {
  @deprecated
  A();
}

@Deprecated('Use B2 instead')
class B {
  @Deprecated('Use B2.named instead')
  B.named();
}

class C {
  C({@Deprecated('Use x instead') this.field});

  @Deprecated('Use x instead')
  Object? field;
}

// Neither side deprecated.
class D {
  D(this.field);

  final Object? field;
}

// Private fields are not part of the public API.
class E {
  E(this._secret);

  @deprecated
  final int _secret;

  int get secret => _secret;
}

// A deprecated constructor in a non-deprecated class is fine.
class F {
  @Deprecated('Use F.create')
  F();

  F.create();
}

// Static fields are not initialized by formals.
class G {
  @deprecated
  static int counter = 0;

  G(int counter);
}

@Deprecated.extend()
class ConsistentNamed {
  @deprecated
  ConsistentNamed();
}

class ConsistentField {
  ConsistentField(@deprecated this.value);
  @Deprecated('old')
  final int value;
}

@Deprecated.new('old')
class ExplicitUnnamed {
  @Deprecated.new('old')
  ExplicitUnnamed();
}

// Representation fields have no reportable field-fragment name offset.
extension type DeprecatedRepresentationParameter(int value) {
  DeprecatedRepresentationParameter.named(@deprecated this.value);
}

typedef Dep = Deprecated;

@Dep('old')
class ConsistentAlias {
  @Dep('old')
  ConsistentAlias();
}

Configure deprecated-consistency

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "deprecated-consistency": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/style/deprecated-consistency: <reason>
  • directives-orderingdirectives_ordering

    Flags import and export directives that break the Effective Dart ordering.

  • do-not-use-environmentdo_not_use_environment

    Disallow reading compilation environment declarations, including type aliases.

  • double-literal-formatdouble-literal-format

    Flags double literals written with redundant or missing digits.

  • empty-constructor-bodiesempty_constructor_bodiesrecommended

    Flags constructors with an empty block body {}.

  • enum-constants-ordering

    Flags enum constants whose declared names violate the configured order.

  • eol-at-end-of-fileeol_at_end_of_filerecommended

    Flags a non-empty file that does not end with exactly one line break.

  • extend-equatableequatable

    Require Equatable semantics for configured class/file name patterns.

  • file-namesfile_namesrecommended

    Enforces lowercase_with_underscores for source file names.