-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Make prefer_collection_literals less suggestive for LinkedHashMap and LinkedHashSet #57993
Comments
And that doesn't work because So the fact that |
The situation is a bit strange, and I'm not sure what the right answer is here. On the one hand, the specification states that a set or map literal will preserve the order in which the elements or entries appear in the code, so you're guaranteed to get exactly the behavior you're looking for without explicitly referencing On the other hand, wanting to be explicit about the fact that the set or map preserves order seems perfectly reasonable. Unfortunately, the specification only says that the static type of a set or map literal is either It seems to me that we have two equally valid use cases:
I expect that the tension we're running into here is from trying to make a single lint serve both use cases. The alternative is to have two lint rules, but that runs into the problem of making it harder for users to decide which rule is the one they want to use. |
Thanks for the analysis Brian! My gut is that the second use case is the most common:
and that loosening this lint is a reasonable compromise. If this frustrates users down the road we could consider the ultimate solution of 2 lints but I'm hoping it won't come to that... WDYT? |
FWIW: if we do poke this hole, I'd like to add a nice bit to the docs explaining the intent. |
This lint is now included in Loosening this check should be a high priority. |
- always_declare_return_types - annotate_overrides - omit_local_variable_types - prefer_conditional_assignment - prefer_if_null_operators - prefer_single_quotes - use_function_type_syntax_for_parameters Ignore prefer_collection_literals lints since they would require incorrect code where a `{}` (statically a `Map`) is assigned to a static `LinkedHashMap`. See https://github.com/dart-lang/linter/issues/1649
Thanks @natebosch. I agree. I still lean towards adding the exception (with an explanatory doc). Unless there are any strong objections, I'll make the change. |
Looking a little at our tests, we do allow these cases: LinkedHashSet<int> ss6 = LinkedHashSet<int>(); // OK
LinkedHashMap hashMap = LinkedHashMap(); // OK Of course this then disagrees w/ |
Thinking about this some more, I'm not convinced that final lruCache = LinkedHashMap<String, Stuff>(); is sufficient to emphasize ordering. If I were to be reviewing that code, I'd probably ask for an explanatory comment. Of course in that case the explicit type is redundant. I guess I'm now firmly back on the fence. 😬 |
I don't entirely agree. Using Even with a comment, I think that using a
instead of:
|
- always_declare_return_types - annotate_overrides - omit_local_variable_types - prefer_conditional_assignment - prefer_if_null_operators - prefer_single_quotes - use_function_type_syntax_for_parameters Ignore prefer_collection_literals lints since they would require incorrect code where a `{}` (statically a `Map`) is assigned to a static `LinkedHashMap`. See https://github.com/dart-lang/linter/issues/1649
Also, given that: LinkedHashSet<int> ss6 = LinkedHashSet<int>(); // OK
LinkedHashMap hashMap = LinkedHashMap(); // OK works, then I claim that: var ss6 = LinkedHashSet<int>();
var hashMap = LinkedHashMap(); should work too, at least for consistency. |
My primary concern right now is that the following code is impossible (AFAICT) to write while using this lint: class Foo {
LinkedHashMap m;
void foo() {
m = {}; // The map literal type 'Map<dynamic, dynamic>' isn't of expected type 'LinkedHashMap<dynamic, dynamic>'.
m = LinkedHashMap(); // Use collection literals when possible.
}
} |
This has brought some (visual) mess to the Flutter code as well, such as in flutter/flutter#61190 (comment). The following code will report "invalid_assignment", final LinkedHashMap<MouseTrackerAnnotation, Matrix4> annotations = <MouseTrackerAnnotation, Matrix4>{}; The following code will report "prefer_collection_literals", final LinkedHashMap<MouseTrackerAnnotation, Matrix4> annotations =
LinkedHashMap<MouseTrackerAnnotation, Matrix4>(); And this is what we ended up having to use, which is pretty clumsy, final LinkedHashMap<MouseTrackerAnnotation, Matrix4> annotations = <MouseTrackerAnnotation, Matrix4>{}
as LinkedHashMap<MouseTrackerAnnotation, Matrix4>; And it appears a lot. I doubt if making "prefer_collection_literals" less suggestive will work, since the Flutter repo is pretty strict on lint errors. I would really hope that the literal |
The reason that it isn't is that the Dart Language Specification explicitly states that
The fact that it's a There's probably a good reason for not doing so, but out of curiosity, have you considered writing
The specification also states that
So you don't need to give it a type of |
The |
With the change suggested in this issue you won't get a lint error. We won't make I'm not sure if the change requested here is difficult. @pq - is this something we can prioritize? |
Towards #1649 Allow using `LinkedHashSet` and `LinkedHashMap` for named arguments, assignment to a variable, and when used in a binary expression where a static type is pushed down to the arguments. Refactor `_shouldSkipLinkedHashLint`. Extract a method to determine the type that is enforced statically for the expression rather than duplicate the subsequent check for each type of parent node.
dart-lang/linter#2340 should fix some of the false positives. After that the only decision left will be whether to allow |
Use collection literals where possible. Because a lot of this code explicitly uses `LinkedHashMap` to indicate that order is important, add appropriate `// ignore` comments until https://github.com/dart-lang/linter/issues/1649 is addressed. PiperOrigin-RevId: 360552809
Use collection literals where possible. Because a lot of this code explicitly uses `LinkedHashMap` to indicate that order is important, add appropriate `// ignore` comments until https://github.com/dart-lang/linter/issues/1649 is addressed. PiperOrigin-RevId: 360552809
* Internal changes PiperOrigin-RevId: 298455490 * Internal changes PiperOrigin-RevId: 299212474 * Internal changes PiperOrigin-RevId: 299384685 * Internal changes PiperOrigin-RevId: 299911795 * Apply sort_child_properties_last lint so we can enable it in google3. This enhances readability in *majority* of the cases by moving properties of a widget to the top. See: https://dart-lang.github.io/linter/lints/sort_child_properties_last.html Tested: TAP --sample ran all affected tests and none failed http://test/OCL:300415528:BASE:300394685:1583970262840:214ae086 PiperOrigin-RevId: 300472053 * Internal changes PiperOrigin-RevId: 300583916 * Internal changes PiperOrigin-RevId: 301253904 * Internal changes PiperOrigin-RevId: 301413179 * Correct the point location computation to be inside of scale's range if it is outside of scale range but only outside by less than epsilon in order to avoid potential mislocation caused by floating point computation. PiperOrigin-RevId: 301644128 * Internal changes PiperOrigin-RevId: 301919359 * Internal changes PiperOrigin-RevId: 305070616 * Point renderer has now the option to select all points overlapping the interaction point PiperOrigin-RevId: 306413187 * Internal Changes PiperOrigin-RevId: 306960748 * Fix missing_return errors for anonymous closures. These are mostly closures whose type dictates that a Future of something-or-other be returned. Fixes include: * Make the closure async * Add a return statement Tested: TAP for global presubmit queue http://test/OCL:307493015:BASE:307542828:1587474095828:2c1a77e8 PiperOrigin-RevId: 308003343 * Internal change PiperOrigin-RevId: 308058514 * Allow using up/down arrow keys to change the hovered bar in a chart. Also add preventDefault so the page doesn't scroll. PiperOrigin-RevId: 308932972 * Internal change PiperOrigin-RevId: 309073218 * Internal changes PiperOrigin-RevId: 309494957 * Add TriangleSymbolRenderer PiperOrigin-RevId: 310472091 * Expose additional classes PiperOrigin-RevId: 310943783 * Change MaterialGray shade900 from blue to correct gray PiperOrigin-RevId: 311587781 * Pass `DateTimeFactory` to `TimeSeriesChart` PiperOrigin-RevId: 311764962 * Add setting to allow series to be always visible and unclickable in the chart legend. PiperOrigin-RevId: 314159954 * Export additional classes PiperOrigin-RevId: 314565994 * Add custom legend ordering by sorting the behaviors series list based on a list with ordered series IDs. PiperOrigin-RevId: 315910283 * Add custom axis renderer for range ticks. PiperOrigin-RevId: 315976899 * Internal changes. PiperOrigin-RevId: 318221788 * Set datum index for legend entry PiperOrigin-RevId: 319291348 * Internal changes PiperOrigin-RevId: 322200941 * Internal changes PiperOrigin-RevId: 322451897 * Internal changes. PiperOrigin-RevId: 322661249 * Internal changes PiperOrigin-RevId: 322671049 * Internal changes PiperOrigin-RevId: 322827055 * Override setState fn with mounted check in baseChartState. PiperOrigin-RevId: 325307554 * Internal changes PiperOrigin-RevId: 326067746 * Internal changes PiperOrigin-RevId: 328177790 * Internal changes PiperOrigin-RevId: 331718263 * Internal changes PiperOrigin-RevId: 331738992 * Internal changes PiperOrigin-RevId: 331765706 * Automated g4 rollback of changelist 331765706. *** Reason for rollback *** Manually rolled back on behalf of: oreflow. Reason Given: This CL auto rolled back a rollback. Rolling forward to a broken revision *** Original change description *** Automated g4 rollback of changelist 331738992. *** Reason for rollback *** TAP has detected 10 or more targets failed to build at cl/331738992. TO ROLLFORWARD (without additional approval): Use go/undo-autorollback and consider filing a go/autorollback-bug. To see all broken targets visit http://test/331738992 or go/newly-broken?p=cl:331738992 if the former is slow to load. To prevent noise from flakes, TAP double-checked the following target fails to build: https://sponge.corp.google.com/in... *** PiperOrigin-RevId: 331767278 * Internal changes PiperOrigin-RevId: 336974207 * Internal changes PiperOrigin-RevId: 337212864 * Test-only change to prepare for new Flutter version PiperOrigin-RevId: 339319134 * Internal changes PiperOrigin-RevId: 340251505 * Internal changes PiperOrigin-RevId: 340463455 * Internal changes PiperOrigin-RevId: 340658840 * Internal changes PiperOrigin-RevId: 342347503 * Internal changes PiperOrigin-RevId: 345724352 * Internal changes PiperOrigin-RevId: 347872889 * Internal changes PiperOrigin-RevId: 350249482 * Clean up violations of Dart lint unnecessary_parenthesis In preparation for null safety migration, I'm first fixing numerous analysis complaints in the hope that it will make the migration go a little bit more smoothly. Remove unnecessary parentheses. PiperOrigin-RevId: 360550907 * Clean up violations of Dart lint annotate_overrides Add missing `@override` annotations. This caught a bug where `hashCode` was not overridden as intended because it was accidentally mistyped as `hashcode`. PiperOrigin-RevId: 360551084 * Clean up violations of Dart lint unnecessary_getters_setters Replace trivial getters and setters with fields. PiperOrigin-RevId: 360551490 * Clean up violations of Dart lint prefer_collection_literals Use collection literals where possible. Because a lot of this code explicitly uses `LinkedHashMap` to indicate that order is important, add appropriate `// ignore` comments until https://github.com/dart-lang/linter/issues/1649 is addressed. PiperOrigin-RevId: 360552809 * Clean up less trivial violations of miscellaneous Dart lints * prefer_null_aware_operators * hash_and_equals * only_throw_errors * avoid_bool_literals_in_conditional_expressions Also replace usage of the deprecated zero-argument `List` constructor. PiperOrigin-RevId: 360552940 * Clean up trivial violations of miscellaneous Dart lints * unnecessary_this * prefer_final_fields * avoid_single_cascade_in_expression_statements * avoid_renaming_method_parameters Also: * Remove unused variables. * Fix some cases where fields were implicitly `dynamic` because their type was omitted. PiperOrigin-RevId: 360553040 * bump intl version to 0.18.0 intl: ">=0.15.2 < 0.18.0" Closes #598 PiperOrigin-RevId: 361636495 * Internal changes PiperOrigin-RevId: 361680107 Co-authored-by: nolankelly <[email protected]> Co-authored-by: Googler <[email protected]> Co-authored-by: mehmetf <[email protected]> Co-authored-by: srawlins <[email protected]> Co-authored-by: rearnshaw <[email protected]> Co-authored-by: jiamingc <[email protected]> Co-authored-by: jamesdlin <[email protected]> Co-authored-by: Artyom Sasin <[email protected]> Co-authored-by: lorrainekan <[email protected]>
final orderedMap = LinkedHashMap.of(<String, String>{}); doesn't trigger this rule, and orderedMap type is LinkedHashMap<String, String>. That's I need. |
* Internal changes PiperOrigin-RevId: 298455490 * Internal changes PiperOrigin-RevId: 299212474 * Internal changes PiperOrigin-RevId: 299384685 * Internal changes PiperOrigin-RevId: 299911795 * Apply sort_child_properties_last lint so we can enable it in google3. This enhances readability in *majority* of the cases by moving properties of a widget to the top. See: https://dart-lang.github.io/linter/lints/sort_child_properties_last.html Tested: TAP --sample ran all affected tests and none failed http://test/OCL:300415528:BASE:300394685:1583970262840:214ae086 PiperOrigin-RevId: 300472053 * Internal changes PiperOrigin-RevId: 300583916 * Internal changes PiperOrigin-RevId: 301253904 * Internal changes PiperOrigin-RevId: 301413179 * Correct the point location computation to be inside of scale's range if it is outside of scale range but only outside by less than epsilon in order to avoid potential mislocation caused by floating point computation. PiperOrigin-RevId: 301644128 * Internal changes PiperOrigin-RevId: 301919359 * Internal changes PiperOrigin-RevId: 305070616 * Point renderer has now the option to select all points overlapping the interaction point PiperOrigin-RevId: 306413187 * Internal Changes PiperOrigin-RevId: 306960748 * Fix missing_return errors for anonymous closures. These are mostly closures whose type dictates that a Future of something-or-other be returned. Fixes include: * Make the closure async * Add a return statement Tested: TAP for global presubmit queue http://test/OCL:307493015:BASE:307542828:1587474095828:2c1a77e8 PiperOrigin-RevId: 308003343 * Internal change PiperOrigin-RevId: 308058514 * Allow using up/down arrow keys to change the hovered bar in a chart. Also add preventDefault so the page doesn't scroll. PiperOrigin-RevId: 308932972 * Internal change PiperOrigin-RevId: 309073218 * Internal changes PiperOrigin-RevId: 309494957 * Add TriangleSymbolRenderer PiperOrigin-RevId: 310472091 * Expose additional classes PiperOrigin-RevId: 310943783 * Change MaterialGray shade900 from blue to correct gray PiperOrigin-RevId: 311587781 * Pass `DateTimeFactory` to `TimeSeriesChart` PiperOrigin-RevId: 311764962 * Add setting to allow series to be always visible and unclickable in the chart legend. PiperOrigin-RevId: 314159954 * Export additional classes PiperOrigin-RevId: 314565994 * Add custom legend ordering by sorting the behaviors series list based on a list with ordered series IDs. PiperOrigin-RevId: 315910283 * Add custom axis renderer for range ticks. PiperOrigin-RevId: 315976899 * Internal changes. PiperOrigin-RevId: 318221788 * Set datum index for legend entry PiperOrigin-RevId: 319291348 * Internal changes PiperOrigin-RevId: 322200941 * Internal changes PiperOrigin-RevId: 322451897 * Internal changes. PiperOrigin-RevId: 322661249 * Internal changes PiperOrigin-RevId: 322671049 * Internal changes PiperOrigin-RevId: 322827055 * Override setState fn with mounted check in baseChartState. PiperOrigin-RevId: 325307554 * Internal changes PiperOrigin-RevId: 326067746 * Internal changes PiperOrigin-RevId: 328177790 * Internal changes PiperOrigin-RevId: 331718263 * Internal changes PiperOrigin-RevId: 331738992 * Internal changes PiperOrigin-RevId: 331765706 * Automated g4 rollback of changelist 331765706. *** Reason for rollback *** Manually rolled back on behalf of: oreflow. Reason Given: This CL auto rolled back a rollback. Rolling forward to a broken revision *** Original change description *** Automated g4 rollback of changelist 331738992. *** Reason for rollback *** TAP has detected 10 or more targets failed to build at cl/331738992. TO ROLLFORWARD (without additional approval): Use go/undo-autorollback and consider filing a go/autorollback-bug. To see all broken targets visit http://test/331738992 or go/newly-broken?p=cl:331738992 if the former is slow to load. To prevent noise from flakes, TAP double-checked the following target fails to build: https://sponge.corp.google.com/in... *** PiperOrigin-RevId: 331767278 * Internal changes PiperOrigin-RevId: 336974207 * Internal changes PiperOrigin-RevId: 337212864 * Test-only change to prepare for new Flutter version PiperOrigin-RevId: 339319134 * Internal changes PiperOrigin-RevId: 340251505 * Internal changes PiperOrigin-RevId: 340463455 * Internal changes PiperOrigin-RevId: 340658840 * Internal changes PiperOrigin-RevId: 342347503 * Internal changes PiperOrigin-RevId: 345724352 * Internal changes PiperOrigin-RevId: 347872889 * Internal changes PiperOrigin-RevId: 350249482 * Clean up violations of Dart lint unnecessary_parenthesis In preparation for null safety migration, I'm first fixing numerous analysis complaints in the hope that it will make the migration go a little bit more smoothly. Remove unnecessary parentheses. PiperOrigin-RevId: 360550907 * Clean up violations of Dart lint annotate_overrides Add missing `@override` annotations. This caught a bug where `hashCode` was not overridden as intended because it was accidentally mistyped as `hashcode`. PiperOrigin-RevId: 360551084 * Clean up violations of Dart lint unnecessary_getters_setters Replace trivial getters and setters with fields. PiperOrigin-RevId: 360551490 * Clean up violations of Dart lint prefer_collection_literals Use collection literals where possible. Because a lot of this code explicitly uses `LinkedHashMap` to indicate that order is important, add appropriate `// ignore` comments until https://github.com/dart-lang/linter/issues/1649 is addressed. PiperOrigin-RevId: 360552809 * Clean up less trivial violations of miscellaneous Dart lints * prefer_null_aware_operators * hash_and_equals * only_throw_errors * avoid_bool_literals_in_conditional_expressions Also replace usage of the deprecated zero-argument `List` constructor. PiperOrigin-RevId: 360552940 * Clean up trivial violations of miscellaneous Dart lints * unnecessary_this * prefer_final_fields * avoid_single_cascade_in_expression_statements * avoid_renaming_method_parameters Also: * Remove unused variables. * Fix some cases where fields were implicitly `dynamic` because their type was omitted. PiperOrigin-RevId: 360553040 * bump intl version to 0.18.0 intl: ">=0.15.2 < 0.18.0" Closes google/charts#598 PiperOrigin-RevId: 361636495 * Internal changes PiperOrigin-RevId: 361680107 Co-authored-by: nolankelly <[email protected]> Co-authored-by: Googler <[email protected]> Co-authored-by: mehmetf <[email protected]> Co-authored-by: srawlins <[email protected]> Co-authored-by: rearnshaw <[email protected]> Co-authored-by: jiamingc <[email protected]> Co-authored-by: jamesdlin <[email protected]> Co-authored-by: Artyom Sasin <[email protected]> Co-authored-by: lorrainekan <[email protected]>
Any update on this? I don't think the PR to fix this has been touched since 2021 |
There are some unaddressed comments on that PR. Either we need to fix them or start fresh... /fyi @natebosch @bwilkerson |
There were some comments that I think I got stuck on but I can't recall which, I haven't looked at this in a long time. |
There is a basic premise in this rule which we cannot satisfy exactly: we need to disallow `LinkedHashSet()` unless the context type requires the developer to use `LinkedHashSet`. But the context type is long gone when the lint rule is run. This CL adds some logic to try to attempt figuring out the context type in the cases where users have filed bugs, but it will never be super accurate. Fixes https://github.com/dart-lang/linter/issues/4736 Fixes https://github.com/dart-lang/linter/issues/3057 Fixes https://github.com/dart-lang/linter/issues/1649 Fixes https://github.com/dart-lang/linter/issues/2795 Change-Id: I3e6c6de81084dca2825488c89830ab3e7ea63186 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/323680 Reviewed-by: Phil Quitslund <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
…pe more" This reverts commit cd8a337. Reason for revert: lint starts barking at the wrong tree: b/298917960 Original change's description: > linter: Refactor prefer_collection_literals to use context type more > > There is a basic premise in this rule which we cannot satisfy exactly: > we need to disallow `LinkedHashSet()` unless the context type requires > the developer to use `LinkedHashSet`. But the context type is long > gone when the lint rule is run. > > This CL adds some logic to try to attempt figuring out the context > type in the cases where users have filed bugs, but it will never be > super accurate. > > Fixes https://github.com/dart-lang/linter/issues/4736 > Fixes https://github.com/dart-lang/linter/issues/3057 > Fixes https://github.com/dart-lang/linter/issues/1649 > Fixes https://github.com/dart-lang/linter/issues/2795 > > Change-Id: I3e6c6de81084dca2825488c89830ab3e7ea63186 > Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/323680 > Reviewed-by: Phil Quitslund <[email protected]> > Reviewed-by: Konstantin Shcheglov <[email protected]> > Commit-Queue: Samuel Rawlins <[email protected]> Change-Id: I980053dd51ffd4447721e0ad7436b07ca704b554 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/324021 Bot-Commit: Rubber Stamper <[email protected]> Reviewed-by: Alexander Thomas <[email protected]> Commit-Queue: Ilya Yanok <[email protected]>
…ntext type more"" This reverts commit cbdae14. In addition, Fix prefer_collection_literals for methods with expression bodies Original description: linter: Refactor prefer_collection_literals to use context type more There is a basic premise in this rule which we cannot satisfy exactly: we need to disallow `LinkedHashSet()` unless the context type requires the developer to use `LinkedHashSet`. But the context type is long gone when the lint rule is run. This CL adds some logic to try to attempt figuring out the context type in the cases where users have filed bugs, but it will never be super accurate. Fixes https://github.com/dart-lang/linter/issues/4736 Fixes https://github.com/dart-lang/linter/issues/3057 Fixes https://github.com/dart-lang/linter/issues/1649 Fixes https://github.com/dart-lang/linter/issues/2795 Change-Id: I958ba69a56866c18523ce6cbf62645ef8e028f6b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/324260 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
- always_declare_return_types - annotate_overrides - omit_local_variable_types - prefer_conditional_assignment - prefer_if_null_operators - prefer_single_quotes - use_function_type_syntax_for_parameters Ignore prefer_collection_literals lints since they would require incorrect code where a `{}` (statically a `Map`) is assigned to a static `LinkedHashMap`. See https://github.com/dart-lang/linter/issues/1649
I have code where insertion order is important, and even though
Map
's default implementation is aLinkedHashMap
, I'd like to emphasize this in code by explicitly usingLinkedHashMap
:This triggers the
prefer_collection_literals
lint.I thought maybe I instead could do:
but that runs afoul of
--no-implicit-dynamic
. I thought that I could give in and be redundant:but that doesn't work either:
So I think that there's no way for me to actually initialize a type declared as a
LinkedHashMap
while appeasing the linter.I think that
prefer_collection_literals
should ignoreLinkedHashMap
(andLinkedHashSet
). In #57905, @bwilkerson asked:I claim that if someone is explicitly using
LinkedHashMap
/LinkedHashSet
instead of justMap
orSet
, then they care about being explicit. (And if we don't think that's important, then I'd argue thatLinkedHashMap
andLinkedHashSet
shouldn't even exist as separate classes.)The text was updated successfully, but these errors were encountered: