simplify_ variable_ pattern
Learn about the simplify_variable_pattern linter rule.
Avoid unnecessary member names in variable patterns.
Details
#AVOID redundant member names in variable patterns.
When a variable pattern declares a variable with the same name as a member of the matched type, the member name is redundant and can be omitted.
BAD:
void f(Object o) {
if (o case String(length: length)) {
print('string is $length characters long');
}
}
GOOD:
void f(Object o) {
if (o case String(:var length)) {
print('string is $length characters long');
}
}
Enable
#
To enable the simplify_variable_pattern rule, add simplify_variable_pattern
under
linter > rules in your analysis_options.yaml
file:
linter:
rules:
- simplify_variable_pattern
If you're instead using the YAML map syntax to configure linter rules,
add simplify_variable_pattern: true under linter > rules:
linter:
rules:
simplify_variable_pattern: true
Unless stated otherwise, the documentation on this site reflects Dart 3.10.3. Report an issue.