avoid-shadowing

Path
lint/suspicious/avoid-shadowing
Default
Off, opt in
Origin
Falcon
Rule
137 of 512

Flags local bindings and parameters that hide an enclosing type's member.

Renaming the local keeps unqualified member references unambiguous. Checks variables, local functions, closure and executable parameters, loop bindings, catch bindings and declaration patterns inside type members. Declared static members are visible in static bodies; inherited instance members are checked only in instance bodies using resolved declaration identities. Initializing formals, type-signature parameter names and primary constructor declarations are excluded. This rule does not check shadowing of other locals or globals. Unknown inherited types are not guessed from their spelling. SDK members are checked only when the signature inventory proves an instance member; unclassified SDK name-table entries are deliberately not used.

Invalid

example.dartdart
class Parent {
  int inherited = 0;
}

class Example extends Parent {
  int value = 0;
  int get total => 0;
  set status(int next) {}
  static int counter = 0;
  void refresh() {}

  void locals() {
    final value = 1;
    var total = 2;
    final inherited = 3;
    void refresh() {}
    final (status, other) = (1, 2);
    for (final value in <int>[1]) {}
    try {} catch (value) {}
  }

  void parameters(int value) {}

  void closure() {
    final callback = (int total) => total;
  }

  static void staticBody() {
    final counter = 1;
  }
}

extension Helpers on Example {
  void helper() {}
  void run() {
    final helper = 1;
  }
}

extension TargetMembers on Example {
  void inspect() {
    final value = 1;
    final inherited = 2;
  }
}

enum Choice {
  first;

  static void inspect() {
    final first = 1;
  }
}

class LazyInitializer {
  int value = 0;
  late final callback = (int value) => value;
}
This binding shadows an enclosing member. Rename the binding.
12 void locals() {
13 final value = 1;
This binding shadows an enclosing member. Rename the binding.
13 final value = 1;
14 var total = 2;
This binding shadows an enclosing member. Rename the binding.
14 var total = 2;
15 final inherited = 3;
This binding shadows an enclosing member. Rename the binding.
15 final inherited = 3;
16 void refresh() {}
This binding shadows an enclosing member. Rename the binding.
16 void refresh() {}
17 final (status, other) = (1, 2);
This binding shadows an enclosing member. Rename the binding.
17 final (status, other) = (1, 2);
18 for (final value in <int>[1]) {}
This binding shadows an enclosing member. Rename the binding.
18 for (final value in <int>[1]) {}
19 try {} catch (value) {}
This binding shadows an enclosing member. Rename the binding.
21
22 void parameters(int value) {}
This binding shadows an enclosing member. Rename the binding.
24 void closure() {
25 final callback = (int total) => total;
This binding shadows an enclosing member. Rename the binding.
28 static void staticBody() {
29 final counter = 1;
This binding shadows an enclosing member. Rename the binding.
35 void run() {
36 final helper = 1;
This binding shadows an enclosing member. Rename the binding.
41 void inspect() {
42 final value = 1;
This binding shadows an enclosing member. Rename the binding.
42 final value = 1;
43 final inherited = 2;
This binding shadows an enclosing member. Rename the binding.
50 static void inspect() {
51 final first = 1;
This binding shadows an enclosing member. Rename the binding.
56 int value = 0;
57 late final callback = (int value) => value;

Valid

example.dartdart
class Unrelated {
  int unrelated = 0;
}

class Example {
  int value;
  static int count = 0;
  Example(this.value);

  void namesDiffer(int input) {
    final localValue = input;
    final unrelated = 2;
    final callback = (int other) => other;
  }

  static void noInstanceScope(int value) {
    final callback = (int value) => value;
  }

  void signatureOnly(void Function(int value) callback) {}

  void assignmentIsNotDeclaration() {
    (value, count) = (value, count);
  }
}

void topLevel(int value) {
  final count = value;
}

class Another {
  void namesAreLocalToTheOwner() {
    final value = 1;
    final count = 2;
  }
}

extension Separate on Example {
  void unrelated() {}
  static void staticScope() {
    final unrelated = 1;
  }
}

abstract class SignatureOnly {
  int value = 0;
  void abstractMethod(int value);
  external void externalMethod(int value);
  void typedCallback(void callback(int value)) {}
}

class EagerInitializer {
  int value = 0;
  final callback = (int value) => value;
}

class ContextHolder {
  Object get context => Object();
}

class Screen extends ContextHolder {
  Object build(Object context) => context;
}

Configure avoid-shadowing

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

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

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

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