googlearchive / pedantic

How to get the most value from Dart static analysis
https://pub.dev/packages/pedantic
BSD 3-Clause "New" or "Revised" License
324 stars 56 forks source link

Show lint warning when a late marked field is final and initialized when declaring. #84

Closed dark-chocolate closed 3 years ago

dark-chocolate commented 3 years ago

In the following example, you can see the class members a and b are both initialized at the time of declaration. There's no benefit of marking them late as it is redundant. Can we show a lint warning here?

class Foo {
  late final int a = 0; // late is redundant
  late final int b = getB(); // late is redundant
  int getB() => 1;
}
davidmorgan commented 3 years ago

That sounds like a question for analyzer

https://github.com/dart-lang/sdk/issues/new

or linter

https://github.com/dart-lang/linter

:)

a14n commented 3 years ago

@dark-chocolate late final are lazy initialized unlike final. For instance try the following code in dartpad:

class A {
  late final String a = (){print('init a'); return 'a';}();
  final String b = (){print('init b'); return 'b';}();
}

main() {
  A()..a..b;
}
dark-chocolate commented 3 years ago

@davidmorgan I'm so sorry for the wrong thread.

@a14n Right sir, I forgot the lazy initialization usage of it.