linter
linter copied to clipboard
`prefer_const_constructors` false positive for deferred elements
prefer_const_constructors
is wrongly recommending const
in cases where a deferred element cannot be used in a constant expression.
For example:
a.dart:
class A {
const A();
}
const aa = A();
b.dart:
import 'a.dart' deferred as a;
class B {
const B(Object a);
}
main() {
var b = B(a.aa); // FALSE POSITIVE
}
See also: https://github.com/dart-lang/sdk/issues/48991
It looks like the issue is the LinterContext.canBeConst
which is not noticing that the deferred element can't be used in a constant context.
Test here: https://dart-review.googlesource.com/c/sdk/+/244281
This may take a bit of doing on the analyzer side. TL;DR constant evaluation is tricky (and complex).
See @scheglov's comment in https://github.com/dart-lang/sdk/issues/48991
I think this should be fixed with https://github.com/dart-lang/sdk/commit/19859a0f9107c92649bf780aba5f4744b09d441f