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

Lint idea: top-level final/const variable defined without explicit type #5100

Open
GregoryConrad opened this issue Sep 28, 2024 · 4 comments
Labels
P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@GregoryConrad
Copy link

This would be a nice-to-have opt-in lint. Can helpful when handling more complex types.

GOOD:

const int i = 0;
final ComplexType foo = createThing();

BAD:

const i = 0;
final foo = createThing();

Rationale: increases code readability (especially for code reviews, when you don't have an IDE handy) by having type information specified explicitly in the source code. While it is sometimes obvious (like in the case of the const int i = 0 above), it is fairly useful when dealing with non-obvious types.

@dart-github-bot
Copy link

Summary: The issue proposes a new lint rule that flags top-level final and const variables without explicit type declarations. This rule aims to improve code readability by encouraging explicit type annotations, especially for complex types.

@dart-github-bot dart-github-bot added the type-enhancement A request for a change that isn't a bug label Sep 28, 2024
@srawlins srawlins transferred this issue from dart-lang/sdk Sep 30, 2024
@srawlins
Copy link
Member

srawlins commented Sep 30, 2024

Thanks for the request. It isn't clear if you are concerned specifically about local variables or top-level variables, but we have the following existing rules:

What you are asking for might be akin to the "DO type annotate fields and top-level variables if the type isn't obvious" rule, for which we appear to not have a backing lint rule.

@srawlins srawlins added the P2 A bug or feature request we're likely to work on label Sep 30, 2024
@bwilkerson
Copy link
Member

Note that omit_obvious_local_variable_types and specify_nonobvious_local_variable_types are experimental.

@GregoryConrad
Copy link
Author

GregoryConrad commented Sep 30, 2024

It isn't clear if you are concerned specifically about local variables or top-level variables

I'm moreso concerned with top-level variables here.

What you are asking for might be akin to the "DO type annotate fields and top-level variables if the type isn't obvious" rule, for which we appear to not have a backing lint rule.

That is more or less what I actually want, but the type_annotate_public_apis rule linked on that page appears to not do enough of what I'd like.

To explain my thinking some more, I'd be fine with an obvious assumed type like const i = 0;, but I figured for the sake of lint implementation, the lint could just require all top-level variables to have an explicit type specified. I have no preference either way; having a lint like specify_nonobvious_local_variable_types, but for top-level variables, would also satisfy my use-case.

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 type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants