Skip to content

proposal: provide_const_constructors #59273

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

Open
dleurs opened this issue Aug 25, 2023 · 5 comments
Open

proposal: provide_const_constructors #59273

dleurs opened this issue Aug 25, 2023 · 5 comments
Labels
area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. devexp-linter Issues with the analyzer's support for the linter package linter-lint-proposal linter-set-flutter linter-status-pending P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug

Comments

@dleurs
Copy link

dleurs commented Aug 25, 2023

provide_const_constructors

  • Adding const for constructor definition when possible, inside the class
  • Currently prefer_const_constructors only check const when class is called, and so only when constructor definition is already const.

Description

BAD:

class A {
  A();
}

GOOD:

class A {
  const A();
}

Links :

@pq
Copy link
Member

pq commented Aug 25, 2023

In your use case, if it's reasonable to mark your classes as @immutable, you might consider using prefer_const_constructors_in_immutables?

@dleurs
Copy link
Author

dleurs commented Aug 28, 2023

I can but could be great if it was not necessary

@EArminjon
Copy link

EArminjon commented Aug 28, 2023

Could be a great feature. We are looking for a way to encourage our developers through a linter rule to use a const constructor when possible. If they forget @immutable, we will have the same issue.

No linter issue here :
Capture d’écran 2023-08-28 à 17 39 55
Capture d’écran 2023-08-28 à 17 40 23

In addition, linter didn't warn to add const constructor when we use @immutable :

Capture d’écran 2023-08-28 à 17 42 43

Linter only warn if @immutable is used and constructor is present :

Capture d’écran 2023-08-28 à 17 42 03

@sagar-tide
Copy link

It would be really helpful to get this lint warning without having to annotate classes with @immutable

@bwilkerson
Copy link
Member

Linter only warn if @immutable is used and constructor is present :

That seems like a bug that should be fixed.

We are looking for a way to encourage our developers through a linter rule to use a const constructor when possible.

I'm not being facetious, but the question I have is: why?

Making widgets be const objects improves performance because the repaint mechanism uses identity to trim unnecessary work. But unless you are depending on identity, or unless you need canonicalization in order to lower your memory use, making everything const doesn't necessarily bring any value. I'd be interested in knowing why you think it brings value to use const everywhere that it's possible to do so.

@srawlins srawlins added the type-enhancement A request for a change that isn't a bug label Mar 27, 2024
@devoncarew devoncarew added devexp-linter Issues with the analyzer's support for the linter package legacy-area-analyzer Use area-devexp instead. labels Nov 20, 2024
@devoncarew devoncarew transferred this issue from dart-archive/linter Nov 20, 2024
@pq pq added the P3 A lower priority bug or feature request label Nov 20, 2024
@bwilkerson bwilkerson added area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. and removed legacy-area-analyzer Use area-devexp instead. labels Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. devexp-linter Issues with the analyzer's support for the linter package linter-lint-proposal linter-set-flutter linter-status-pending P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

7 participants