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

prefer_const_constructors: false positive in tenrary expression #4531

Closed
Pante opened this issue Jul 5, 2023 · 2 comments
Closed

prefer_const_constructors: false positive in tenrary expression #4531

Pante opened this issue Jul 5, 2023 · 2 comments
Assignees
Labels
false-positive P2 A bug or feature request we're likely to work on set-flutter Affects a rule in the recommended Flutter rule set type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@Pante
Copy link

Pante commented Jul 5, 2023

Describe the issue
When trying to create a const object in a ternary expression, both parts must either be const or non-const. Having a mix will result in a compilation error.

To Reproduce

class A<T> {
  const A();
}

class B<T> extends A<T> {
  B();
}
A<T> foo<T>(bool flag) => flag ? A() : B(); // warns that A() should be const

A<T> foo<T>(bool flag) => flag ? const A() : B(); // compile error: A value of type 'Object' can't be returned from the function 'foo' because it has a return type of 'A<T>'

Expected behavior
No warning

Additional context
I'm unsure if this is related to #3802.

@Pante Pante changed the title prefer_const_constructors false positive in tenrary expression prefer_const_constructors: false positive in tenrary expression Jul 5, 2023
@github-actions github-actions bot added the set-flutter Affects a rule in the recommended Flutter rule set label Jul 5, 2023
@lrhn
Copy link
Member

lrhn commented Jul 5, 2023

Adding the const changes the type of the literal from A<T> to A<Never>.
That itself suggests that the lint might be out of place.

The error then comes from A<Never> and B<T> not having any shared superintetface below Object.

@bwilkerson bwilkerson added false-positive P2 A bug or feature request we're likely to work on labels Jul 10, 2023
@srawlins srawlins added the type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) label Apr 3, 2024
@srawlins srawlins self-assigned this Apr 12, 2024
copybara-service bot pushed a commit to dart-lang/sdk that referenced this issue Apr 16, 2024
Fixes dart-lang/linter#4531

This involves moving `approximateContextType`, but it is not changed.

Change-Id: I477377a0ae233a7043f1d2e9a9d511cb435514bc
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/362602
Commit-Queue: Samuel Rawlins <[email protected]>
Reviewed-by: Phil Quitslund <[email protected]>
@srawlins
Copy link
Member

Fixed with dart-lang/sdk@f15bcc9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
false-positive P2 A bug or feature request we're likely to work on set-flutter Affects a rule in the recommended Flutter rule set type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

4 participants