$ dartanalyzer --version
dartanalyzer version 2.6.0-edge.36985859e421a9fbbdfc56379506b2a367c7d4e9
I have the prefer_const_constructors lint enabled. Running the analyzer over this file:
void main() {
Duration(seconds: 10);
Duration(seconds: 10);
Duration(seconds: 10);
Duration(seconds: 10);
Duration(seconds: 10);
Duration(seconds: 10);
Duration(seconds: 10);
}
shows no issues:
$ dartanalyzer main.dart
Analyzing main.dart...
No issues found!
That is already strange as all the Durations should be const! Adding const to one of the Durations suddenly makes the analyzer detect that all the Durations should be const as well:
void main() {
const Duration(seconds: 10); // Note the const here
Duration(seconds: 10);
Duration(seconds: 10);
Duration(seconds: 10);
Duration(seconds: 10);
Duration(seconds: 10);
Duration(seconds: 10);
}
$ dartanalyzer main.dart
Analyzing main.dart...
lint • Prefer const with constant constructors. • main.dart:3:3 • prefer_const_constructors
lint • Prefer const with constant constructors. • main.dart:4:3 • prefer_const_constructors
lint • Prefer const with constant constructors. • main.dart:5:3 • prefer_const_constructors
lint • Prefer const with constant constructors. • main.dart:6:3 • prefer_const_constructors
lint • Prefer const with constant constructors. • main.dart:7:3 • prefer_const_constructors
lint • Prefer const with constant constructors. • main.dart:8:3 • prefer_const_constructors
6 lints found.
I don't know if it's related, but there seems to be some general weirdness in prefer_const_constructors. I tried (and failed) to determine why I needed https://github.com/flutter/flutter/pull/37978 to satisfy the analyzer on one system but not another. (I could not identify any relevant differences in my environment.)
Thanks for the report!
@scheglov: I wonder if you have any insights into this one? (We're deferring to LinterContextImpl.canBeConst):
(We should also remove the extra visitor if it's no longer needed...)
https://dart-review.googlesource.com/c/sdk/+/115613 for Duration.
As for type parameters, the reason is that CompileTimeErrorCode.CONST_WITH_TYPE_PARAMETERS is reported in ErrorVerifier, not during constant evaluation.
@scheglov: is this fixed as of 2246f0a?
Yes, the title issue is fixed.
I did not close it yet because I wanted to get back to not reporting errors for "constants" with type parameters.
Ok cool. Thank you!
With c11ca7d4395d5ca2f2df3349e7ab92b320021fce we can remove the _Collector work-around.
Obviously this means that you need a new analyzer ;-)
Fantastic! @scheglov: do we need a new analyzer published w/ the required fixes?
@pq Yes, we do need a new version of analyzer, for this fix, to remove _Collector, and hopefully also to remove the MockDartSdk from the linter. But I'm also making to changes to TypeProvider and the way ClassElement(s) are turned into InterfaceType(s). So, we might publish analyzer and switch linter to it, but we will need another publishing and linter changes soonish.