Description
While working on migrating dart:ui
, @yjbanov noticed a lot of instances of the analyzer hint unnecessary_null_comparison
hint, especially for code like this:
void f(C c) {
assert(c != null);
...
}
(this arose because the migration tool could not find any code that passed a null value into c
, so it made its type non-nullable). At first glance, it might seem sensible to remove the assertion on the grounds that c
can no longer be null, but that's only true in strong mode. Flutter will have a long tail of users running in weak mode until Dart 3.0, and assertions like these are necessary to help prevent them from passing nulls to Flutter APIs. It's going to require some discipline on the part of the Flutter team to remember not to delete these assertions, and that discipline will be made a lot more difficult by hints like this one.
Maintainers of Dart packages are going to run into a similar problem during the migration period. This makes me wonder if we should:
(a) remove the unnecessary_null_comparison
hint so that users aren't fooled by it into removing assertions like these?
(b) exclude certain patterns from firing the unnecessary_null_comparison
hint, such as assert(x != null)
and if (x == null) { throw ... }
?
(c) add a lint suggesting that users add assert(x != null)
for non-nullable function parameters?