avoid-shadowing
added in: 1.2.0
warning
Warns when a declaration name shadows another declaration with the same name in the current scope.
Shadowing might lead to unexpected errors (especially if the types of both declarations match).
Use ignore-static
configuration (default is false
), if you want to exclude static fields from the name scope.
Use ignore-fields
configuration (default is false
), if you want to exclude fields from the name scope.
Use ignore-parameters
configuration (default is false
), if you want to exclude parameters from the name scope.
Use ignored-names
configuration (default is none), if you want to ignore specific names.
⚙️ Config example
dart_code_metrics:
...
rules:
...
- avoid-shadowing:
ignore-static: true
ignore-fields: true
ignore-parameters: true
ignored-names:
- some
- name
Example
❌ Bad:
class SomeClass {
final String content;
...
void method() {
final content = ...; // LINT
}
void anotherMethod(String content) { // LINT
...
}
void records() {
final declarations = <(int, String)>{};
var length = 0;
// LINT
if (declarations case Set(:final length)) {
...
}
final Set(length: final length) = declarations; // LINT
}
}
✅ Good:
class SomeClass {
final String content;
...
void method() {
final localContent = ...;
}
void anotherMethod(String contentParam) {
...
}
void records() {
final declarations = <(int, String)>{};
var length = 0;
if (declarations case Set(length: final someLength)) {
...
}
final Set(length: final someLength) = declarations;
}
}