Rules / Suspicious / no-wildcard-variable-uses
no-wildcard-variable-uses
Flags references to a wildcard variable or parameter (a name made solely of
underscores, such as _ or __).
An all-underscore name marks a binding as intentionally unused, and under
Dart's wildcard-variable semantics such names are non-binding — reading one
back does not retrieve the value you expect. Referencing it is therefore
either a mistake or a reliance on behavior that is changing. Declaring a
wildcard is fine; only uses in expression position are reported. Give the
binding a real name if you actually need its value.
Invalid
example.dartdart
// Referencing a wildcard variable or parameter is not allowed.
int a(int _) => _;
int b(int _) => _ * 2;
void c() {
var _ = 1;
print(_);
}
void d() {
var __ = 2;
print(__);
}
int e(int _) {
return _ + 1;
}
void f() {
var _ = 0;
_ = 5;
}
Don't use a wildcard parameter or variable.
Don't use a wildcard parameter or variable.
4
5int b(int _) => _ * 2;
∙
Don't use a wildcard parameter or variable.
8 var _ = 1;
9 print(_);
∙
Don't use a wildcard parameter or variable.
13 var __ = 2;
14 print(__);
∙
Don't use a wildcard parameter or variable.
17int e(int _) {
18 return _ + 1;
∙
Don't use a wildcard parameter or variable.
Valid
example.dartdart
// Wildcards may be declared but never referenced.
void a() {
var x = 1;
print(x);
}
int b(int value) => value;
void c(int _) {}
void d() {
var _ = 1;
}
void e() {
[1, 2].forEach((_) {});
}
void f() {
for (var _ in <int>[]) {}
}
How to configure
Set the severity of no-wildcard-variable-uses in your falcon.json:
falcon.jsonjson
{
"linter": {
"rules": {
"suspicious": {
"no-wildcard-variable-uses": "error"
}
}
}
}