Closed
Description
Given the following code, after Dart 3.4, the prefer_const_constructors
lint is shown when invoking the Interval
constructor in such conditions:
import 'package:meta/meta.dart' show redeclare;
extension type const Size(int size) implements int {
@redeclare
Size operator -() => Size(-size);
}
final class Interval {
final Size size;
const Interval(this.size);
Interval get descending => Interval(-size);
// ^^^^^^^^^^^^^^^ Use 'const' with the constructor to improve performance.
}
Running the Add 'const' modifier quick fix causes the code to violate const_eval_extension_type_method
:
final class Interval {
final Size size;
const Interval(this.size);
Interval get descending => const Interval(-size);
// ^^^^^ Extension type methods can't be used in constant expressions.
}
A similar false positive—although I remember seeing this one on Dart 3.3 as well—can also be seen in a slightly different situation:
void main() {
final interval = Interval(-Size(5));
// ^^^^^^^^^^^^^^^^^^ Use 'const' with the constructor to improve performance.
}
Where applying the quick fix leads to the same broken code:
void main() {
final interval = const Interval(-Size(5));
// ^^^^^^^^ Extension type methods can't be used in constant expressions.
}
See dart info
General info
- Dart 3.4.0 (stable) (Mon May 6 07:59:58 2024 -0700) on "macos_arm64"
- on macos / Version 14.3.1 (Build 23D60)
- locale is ca-ES
Project info
- sdk constraint: '>=3.4.0 <4.0.0'
- dependencies: collection, meta
- dev_dependencies: test, very_good_analysis