unreachable_extension_member
Reports an extension member that the extended type already has, so a normal call never reaches it. Give it another name.
- Tier
- advisory
- Scope
- Dart
- Lowest preset
- recommended
- Fix
- none
What it reports
Section titled “What it reports”An instance member of an extension that the extended type already has: extension on String { int get length => 0; }. A call such as text.length always reaches the type’s own member, so the extension’s is never called that way. The violation covers the member’s name.
A member counts where the type has a member of the same kind and name:
- a getter or method, where the type has a getter or method of that name;
- a setter, where it has a setter;
- an operator, where it has that operator;
- a private name, only where the type’s member is in the same library.
It does not report:
- a static member;
- an extension on a nullable type (
String?has nolengthto call, so its members are reachable), on a type parameter, a function type or a record type; - a member of an unnamed or private extension that nothing in its file refers to: that is the analyzer’s
unused_element; - a member named like a member of
Object, which is an analyzer error.
A member reached through an explicit extension application, Counting(list).length, is reported all the same: that is the only way to reach it.
Nothing else warns you, and the extension looks installed. A test that calls it through an explicit extension application passes, while every real call goes to the type.
Example
Section titled “Example”extension Counting on List<int> { int get length => where((value) => value > 0).length;}extension Counting on List<int> { int get positiveCount => where((value) => value > 0).length;}Rule parameters
Section titled “Rule parameters”Set under the assay: key of the project’s root analysis_options.yaml.
| Rule parameter | Default | Effect |
|---|---|---|
enabled |
true |
false switches the rule off for the whole project. |
exclude |
[] |
Path globs where the rule is silent. |
Suppressing it
Section titled “Suppressing it”Write why on the line directly above the suppression:
// Called only through the extension's name, on purpose.// ignore: assay/unreachable_extension_memberint get length => 0;// ignore_for_file: assay/unreachable_extension_member silences it in a whole file, and // ignore: unreachable_extension_member without the prefix works too. See Suppressions.