linter icon indicating copy to clipboard operation
linter copied to clipboard

False `prefer_const_constructors_in_immutables` on class that can never be const constructed

Open goderbauer opened this issue 3 years ago • 0 comments

The following code produces a misleading prefer_const_constructors_in_immutables lint:

@immutable
class Foo {
  Foo(this.list) : assert(list.length > 2); // prefer_const_constructors_in_immutables
  final List<int> list;
}

My understanding from https://github.com/flutter/flutter/issues/102460#issuecomment-1109424737 and https://github.com/dart-lang/language/issues/1296 is that this class can never be const-constructed. So, suggesting to give it a const constructor is ... not helpful. :)

Instead, there should actually be the opposite warning: If you do make this constructor const, it should warn that this class can actually never be const constructed and that the const should be removed. I filed https://github.com/dart-lang/linter/issues/3367 for that.

goderbauer avatar Apr 27 '22 22:04 goderbauer