Rules / Style / avoid-redundant-pattern-field-names
avoid-redundant-pattern-field-names
Flags redundant field names in object and record patterns.
When a pattern binds a field to a variable of the same name — Point(x: x)
or (name: name) — the explicit label merely repeats the getter name for no
benefit. The shorthand Point(:x) / (:name) is shorter and equivalent.
Only a field whose label matches its bound variable is flagged; a genuine
rename such as Point(x: px) is left alone.
Invalid
example.dartdart
class Point {
final int x;
final int y;
const Point(this.x, this.y);
}
void objectPatterns(Object o) {
switch (o) {
case Point(
x: x,
y: y,
):
print('$x$y');
default:
break;
}
}
void ifCase(Object o) {
if (o case Point(x: x)) {
print(x);
}
}
int switchExpr(Object o) => switch (o) {
Point(y: y) => y,
_ => 0,
};
void recordPattern(Object o) {
switch (o) {
case (
first: first,
second: second,
):
print('$first$second');
default:
break;
}
}
Redundant field name in pattern; use the shorthand (e.g. `:x`).
Redundant field name in pattern; use the shorthand (e.g. `:x`).
Redundant field name in pattern; use the shorthand (e.g. `:x`).
19void ifCase(Object o) {
20 if (o case Point(x: x)) {
∙
Redundant field name in pattern; use the shorthand (e.g. `:x`).
25int switchExpr(Object o) => switch (o) {
26 Point(y: y) => y,
∙
Redundant field name in pattern; use the shorthand (e.g. `:x`).
32 case (
33 first: first,
∙
Redundant field name in pattern; use the shorthand (e.g. `:x`).
33 first: first,
34 second: second,
∙
Valid
example.dartdart
class Point {
final int x;
final int y;
const Point(this.x, this.y);
}
void shorthand(Object o) {
switch (o) {
case Point(:final x, :final y):
print('$x$y');
default:
break;
}
}
void renamed(Object o) {
switch (o) {
case Point(x: final a, y: final b):
print('$a$b');
default:
break;
}
}
void differentNames(Object o) {
if (o case Point(x: y)) {
print(y);
}
}
int switchExpr(Object o) => switch (o) {
Point(:final x) => x,
_ => 0,
};
void records(Object o) {
switch (o) {
case (first: final a, second: final b):
print('$a$b');
default:
break;
}
}
How to configure
Set the severity of avoid-redundant-pattern-field-names in your falcon.json:
falcon.jsonjson
{
"linter": {
"rules": {
"style": {
"avoid-redundant-pattern-field-names": "error"
}
}
}
}