Skip to content

avoid_shadowed_extension_methods

v1.0.0 Warning Code Quality

This rule flags an extension member whose name already exists on the type being extended. Instance members always win over extension members, so the extension one can never be called.

Extension members are resolved statically and lose to instance members every time. An extension declaring toUpperCase() on String compiles cleanly and is simply never invoked — every call site silently reaches String.toUpperCase instead.

The result is code that reads as though the extension applies and behaves as though it does not. Since nothing errors, the discrepancy is usually found by debugging the wrong thing.

See also: Dart: extension methods

extension on String {
String toUpperCase() => '!'; // never called
}

Give the extension member a name the type does not already use:

extension on String {
String shout() => '\$this!';
}

The check walks the extended type and its supertypes. Members inherited from Object are excluded — every type has them, so reporting toString or hashCode would flag ordinary, useful extensions.

Static extension members are skipped, since they are accessed through the extension name and cannot be shadowed.

This rule is in the core preset, so it is on with preset: core, preset: recommended or preset: opinionated.

To turn it off:

many_lints.yaml
rules:
avoid_shadowed_extension_methods: false

To keep the rule on but skip certain paths, use per-rule exclude.