dart-lang / linter

Linter for Dart.
https://dart.dev/tools/linter-rules
BSD 3-Clause "New" or "Revised" License
634 stars 170 forks source link

Questions on null_check_on_nullable_type_parameter #3256

Open srawlins opened 2 years ago

srawlins commented 2 years ago

I was looking at test cases in order to implement a quick fix for null_check_on_nullable_type_parameter but came across some test cases that surprised me.

Future<T> / await p!:

Future<T> m40<T extends Object?>(T? p) async => await p!; // LINT

This case does not generate a lint report, but I expected it would.

List<T> / [p!]:

void ff<T>(T? p) {
  <T>[p!]; // LINT
}

This case also does not generate a lint report, but I expected it would.

Stream<T> / yield p!:

Stream<T> m41<T extends Object?>(T? p) async* { yield p!; }

This case also does not generate a lint report, but I expected it would.

srawlins commented 2 years ago

CC @leafpetersen who filed the initial lint rule request

CC @a14n who implemented the lint rule

CC @goderbauer in case this affects the decision to add this to lints/recommended.yaml as per https://github.com/dart-lang/lints/issues/64

a14n commented 2 years ago

This case also does not generate a lint report, but I expected it would.

I expected the same. Those cases were just forgotten during the implementation. I guess the following method should be extended to cover those cases. https://github.com/dart-lang/linter/blob/8c01f686b0d17233c49f8d7b86fe1b984d64086c/lib/src/rules/unnecessary_null_checks.dart#L39-L83.