Skip to content

False positive prefer_const_constructors incorrectly suggests adding const when an extension type operator is used in an argument #59474

Closed
@albertms10

Description

@albertms10

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

Metadata

Metadata

Assignees

Labels

P2A bug or feature request we're likely to work ondevexp-linterIssues with the analyzer's support for the linter packagelegacy-area-analyzerUse area-devexp instead.linter-false-positiveIssues related to lint rules that report a problem when it isn't a problem.linter-set-fluttertype-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions