sdk
sdk copied to clipboard
[analyzer] Incorrect `unused_local_variable` warning.
a is used here:
main() {
final a = foo(null);
a!;
}
foo(x) => x;
$ dart analyze ~/play1/bug1c8i.dart
Analyzing bug1c8i.dart... 0.6s
warning • bug1c8i.dart:2:9 • The value of the local variable 'a' isn't used. Try removing the variable or using it. •
unused_local_variable
1 issue found.
I agree. I think we have to consider a null check to be a use because removing the null check could change the semantics of the program. But only in the case where the variable's type is nullable. If it's non-nullable then the null check is invalid and shouldn't be counted as a use.
I'm going the other way :)
A downside of calling this a "use" is that the rule will allow (some, maybe very few) unused variables to go unreported. As in, variables whose only "use" is an unintentional null-assert statement-expression.
A downside of calling this a non-use is that we will not support the pattern of checking if an otherwise-unused local variable is null, and throwing if it is, by operating on it with a null-assert statement-expression.
I think the first downside outweighs the second.