avoid-referencing-subclasses

Path
lint/suspicious/avoid-referencing-subclasses
Default
Off, opt in
Origin
Falcon
Rule
127 of 512

Keeps base classes independent from their transitive subclasses. ignored-annotations replaces the default ["freezed", "Freezed", "unfreezed"]. Annotation names match the final name segment exactly, including case.

Invalid

example.dartdart
class Base {
  Child? field;
  Child make() => throw 0;
  void accept(Child value) {}
  List<Grandchild> children = [];
  Object create() => Child();
  int staticValue() => Child.value;
  bool test(Object value) => value is Child;
}
class Child extends Base { static int value = 1; }
class Grandchild extends Child {}

const FREEZED = 0;
@FREEZED
class ExactCaseBase {
  ExactCaseChild? child;
}
class ExactCaseChild extends ExactCaseBase {}
mixin class MixinBase {
  MixedSubclass? child;
}
class MixedSubclass with MixinBase {}
Avoid referencing a subclass from its base class.
1class Base {
2 Child? field;
Avoid referencing a subclass from its base class.
2 Child? field;
3 Child make() => throw 0;
Avoid referencing a subclass from its base class.
3 Child make() => throw 0;
4 void accept(Child value) {}
Avoid referencing a subclass from its base class.
4 void accept(Child value) {}
5 List<Grandchild> children = [];
Avoid referencing a subclass from its base class.
5 List<Grandchild> children = [];
6 Object create() => Child();
Avoid referencing a subclass from its base class.
6 Object create() => Child();
7 int staticValue() => Child.value;
Avoid referencing a subclass from its base class.
7 int staticValue() => Child.value;
8 bool test(Object value) => value is Child;
Avoid referencing a subclass from its base class.
15class ExactCaseBase {
16 ExactCaseChild? child;
Avoid referencing a subclass from its base class.
19mixin class MixinBase {
20 MixedSubclass? child;

Valid

example.dartdart
class Base {
  Base? parent;
  Unrelated? value;
  Object factory(Object Child) => Child;
  T generic<T>(T value) => value;
  void shadow() {
    final Child = () => Object();
    Child();
  }
  Child genericShadow<Child>(Child value) => value;
}
class Child extends Base {}
class Unrelated {}
const freezed = 0;
@freezed
class GeneratedBase { GeneratedChild? child; }
class GeneratedChild extends GeneratedBase {}
class GenericBase<GenericChild> { GenericChild? value; }
class GenericChild extends GenericBase<int> {}

abstract class Repo { factory Repo() = RepoImpl; }
class RepoImpl implements Repo {}
class ConstraintBase { ConstrainedMixin? value; }
mixin ConstrainedMixin on ConstraintBase {}
class InterfaceBase { ViaInterface? value; }
class InterfaceOnly implements InterfaceBase {}
class ViaInterface extends InterfaceOnly {}

class Freezed { const Freezed({String? unionKey}); }
const unfreezed = 0;
@Freezed(unionKey: 'type')
class ConfiguredUnion { ConfiguredCase? value; }
class ConfiguredCase extends ConfiguredUnion {}
@unfreezed
class MutableUnion { MutableCase? value; }
class MutableCase extends MutableUnion {}

Configure avoid-referencing-subclasses

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "suspicious": {
        "avoid-referencing-subclasses": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/suspicious/avoid-referencing-subclasses: <reason>