Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[linter] False positive prefer_const_constructors incorrectly suggests adding const #4978

Open
albertms10 opened this issue May 14, 2024 · 0 comments
Labels
P2 A bug or feature request we're likely to work on set-flutter Affects a rule in the recommended Flutter rule set

Comments

@albertms10
Copy link
Contributor

albertms10 commented May 14, 2024

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
@scheglov scheglov added the P2 A bug or feature request we're likely to work on label May 15, 2024
@srawlins srawlins transferred this issue from dart-lang/sdk May 24, 2024
@github-actions github-actions bot added the set-flutter Affects a rule in the recommended Flutter rule set label May 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P2 A bug or feature request we're likely to work on set-flutter Affects a rule in the recommended Flutter rule set
Projects
None yet
Development

No branches or pull requests

2 participants