Missing variable pattern in one branch of logical-or patternDRT-W1483
The analyzer produces this diagnostic when one branch of a logical-or pattern doesn't declare a variable that is declared on the other branch of the same pattern.
Example
The following code produces this diagnostic because the right-hand side of
the logical-or pattern doesn't declare the variable a
:
void f((int, int) r) {
if (r case (var a, 0) || (0, _)) {
print(a);
}
}
Common fixes
If the variable needs to be referenced in the controlled statements, then add a declaration of the variable to every branch of the logical-or pattern:
void f((int, int) r) {
if (r case (var a, 0) || (0, var a)) {
print(a);
}
}
If the variable doesn't need to be referenced in the controlled statements, then remove the declaration of the variable from every branch of the logical-or pattern:
void f((int, int) r) {
if (r case (_, 0) || (0, _)) {
print('found a zero');
}
}
If the variable needs to be referenced if one branch of the pattern matches but not when the other matches, then break the pattern into two pieces:
void f((int, int) r) {
switch (r) {
case (var a, 0):
print(a);
case (0, _):
print('found a zero');
}
}