Skip to content

code generated local variables should be var or final based on enabled lints #49561

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
Tracked by #55825
pq opened this issue Jul 28, 2022 · 3 comments
Open
Tracked by #55825
Labels
area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. devexp-refactoring Issues with analysis server refactorings P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug

Comments

@pq
Copy link
Member

pq commented Jul 28, 2022

Specifically, code generation should respect:

@pq pq added legacy-area-analyzer Use area-devexp instead. P3 A lower priority bug or feature request devexp-refactoring Issues with analysis server refactorings labels Jul 28, 2022
@srawlins srawlins added the type-enhancement A request for a change that isn't a bug label Mar 14, 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
@FMorschel
Copy link
Contributor

I've already worked on (quick fix):

And I am currently working on (completion):

We also have refactorings to consider here (refactoring):

Do we have anything on what specific features we have that create new variables, @bwilkerson? If we don't, I think I can take a look around this week and find out at least most things.

Your #50018 (comment) (linked above):

It seems like the Dart analyzer ought to be able to determine that a declaration should be var if its redefined, final it is isn't, and const if it is a constant (not sure about that last).

I wish it were that simple. The problem, of course, is that the variable didn't exist before the extract operation, so it's guaranteed that it isn't re-assigned yet. What we can't know is whether the user will assign a new value after this operation.

The point about const is interesting. If the original expression is a const expression, then perhaps we should use const for it. (It isn't guaranteed that that's the right thing either, but it is an interesting signal.)

That said, the original point is valid and something we have been discussing. The question is, what signals can we use to determine the user's preferences? One signal that comes to mind is style-based lints that have been enabled. In this case, it seems clear that if the user has enabled prefer_final_locals then they probably want the variable to be declared using final. The question is whether that's sufficient.

@lrhn
Copy link
Member

lrhn commented Mar 31, 2025

This is something that user-created code generation might also want to consider.
(Might want code generators to have access to the enabled lints of the repository they're generating into. Alternatively they can just say "generated code should not be linted" and do whatever they want. @davidmorgan)

@davidmorgan
Copy link
Contributor

Thanks Lasse.

For the most part I think "generated code should not be linted" is the right path. We're also talking about arranging for generated code to not be (re)formatted, here dart-lang/build#3812.

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-refactoring Issues with analysis server refactorings 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

6 participants