avoid-shadowing
- Group
- Suspicious
- 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
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;
} void locals() { final value = 1; final value = 1; var total = 2; var total = 2; final inherited = 3; final inherited = 3; void refresh() {} void refresh() {} final (status, other) = (1, 2); final (status, other) = (1, 2); for (final value in <int>[1]) {} 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; void run() { final helper = 1; void inspect() { final value = 1; final value = 1; final inherited = 2; static void inspect() { final first = 1; int value = 0; late final callback = (int value) => value;Valid
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.
{
"linter": {
"rules": {
"suspicious": {
"avoid-shadowing": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/suspicious/avoid-shadowing: <reason>More suspicious rules
- avoid-shadowing-type-parameters
avoid_shadowing_type_parametersrecommendedFlags a type parameter that shadows a type parameter of an enclosing declaration.
- avoid-suspicious-super-overrides
Finds getters hiding fields supplied via the immediate super constructor's
this.formals or declaring primary-constructor parameters. - avoid-throw-in-catch-block
avoid-throw-in-catch-blockFlags
throwstatements inside acatchblock. - avoid-top-level-members-in-teststest
Requires public declarations in test files, except main, to be private.
- avoid-types-as-parameter-names
avoid_types_as_parameter_namesrecommendedFlags implicit formal parameters, catch variables, and type parameters that shadow a type in their enclosing scope.
- avoid-unrelated-type-assertions
avoid-unrelated-type-assertionsrecommendedFlags an
ischeck proven impossible by canonical resolved types. - banned-usage
Ban resolved API references, not identifiers that merely share their spelling.
- cascade-after-null-coalescing
Flags a cascade after an unparenthesized null-coalescing expression.