linter icon indicating copy to clipboard operation
linter copied to clipboard

`prefer_const_constructors` false positive for deferred elements

Open pq opened this issue 2 years ago • 3 comments

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
}

pq avatar May 10 '22 16:05 pq

See also: https://github.com/dart-lang/sdk/issues/48991

pq avatar May 10 '22 16:05 pq

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

pq avatar May 10 '22 17:05 pq

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

pq avatar May 10 '22 20:05 pq

I think this should be fixed with https://github.com/dart-lang/sdk/commit/19859a0f9107c92649bf780aba5f4744b09d441f

kallentu avatar Aug 02 '23 22:08 kallentu