From 26b221937920dcd1d8f7bf2736113eba69cad6e5 Mon Sep 17 00:00:00 2001 From: Kallen Tu Date: Fri, 13 Jun 2025 09:58:25 -0700 Subject: [PATCH] [linter] Dot shorthands: Add tests for omit_obvious_types. Make sure that we're not suggesting to omit types for these two lints with dot shorthands, which shouldn't be the case since we didn't add extra logic for `hasObviousType`. Added a few tests. Fixes: https://github.com/dart-lang/sdk/issues/60910 Bug: https://github.com/dart-lang/sdk/issues/60893 Change-Id: If088ad7e5ed1d2ed7f202a462baab5461ae046fb Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/434541 Commit-Queue: Kallen Tu Reviewed-by: Phil Quitslund Reviewed-by: Samuel Rawlins --- ...mit_obvious_local_variable_types_test.dart | 42 +++++++++++++++++++ .../omit_obvious_property_types_test.dart | 18 ++++++++ 2 files changed, 60 insertions(+) diff --git a/pkg/linter/test/rules/omit_obvious_local_variable_types_test.dart b/pkg/linter/test/rules/omit_obvious_local_variable_types_test.dart index e7160d981f9..73530e43286 100644 --- a/pkg/linter/test/rules/omit_obvious_local_variable_types_test.dart +++ b/pkg/linter/test/rules/omit_obvious_local_variable_types_test.dart @@ -58,6 +58,18 @@ class A { ); } + test_cascade_dotShorthand() async { + await assertNoDiagnostics(r''' +f() { + A a = .new()..x..x..x; +} + +class A { + final x = 0; +} +'''); + } + test_dot_shorthand() async { await assertNoDiagnostics(r''' f() { @@ -77,6 +89,16 @@ f() { ); } + test_forEach_inferredList_dotShorthands() async { + await assertNoDiagnostics(r''' +enum E { a, b, c } + +f() { + for (E e in [.a, .b, .c]) { } +} +'''); + } + test_forEach_listWithNonObviousElement() async { await assertNoDiagnostics(r''' f() { @@ -183,6 +205,16 @@ class A {} ); } + test_instanceCreation_nonGeneric_dotShorthand() async { + await assertNoDiagnostics(r''' +f() { + A a = .new(); +} + +class A {} +'''); + } + test_list() async { await assertDiagnostics( r''' @@ -194,6 +226,16 @@ f() { ); } + test_list_dotShorthand() async { + await assertNoDiagnostics(r''' +enum E { a, b, c } + +f() { + List a = [.a, .b, .c]; +} +'''); + } + test_list_ok1() async { await assertNoDiagnostics(r''' f() { diff --git a/pkg/linter/test/rules/omit_obvious_property_types_test.dart b/pkg/linter/test/rules/omit_obvious_property_types_test.dart index 636a2871e78..b0e328094cd 100644 --- a/pkg/linter/test/rules/omit_obvious_property_types_test.dart +++ b/pkg/linter/test/rules/omit_obvious_property_types_test.dart @@ -203,6 +203,16 @@ class C {} ); } + test_instanceCreation_nonGeneric_static_dotShorthand() async { + await assertNoDiagnostics(r''' +class A { + static C c = .new(); +} + +class C {} +'''); + } + test_instanceCreation_nonGeneric_topLevel() async { await assertDiagnostics( r''' @@ -214,6 +224,14 @@ class C {} ); } + test_instanceCreation_nonGeneric_topLevel_dotShorthand() async { + await assertNoDiagnostics(r''' +C c = .new(); + +class C {} +'''); + } + test_list_ok1_static() async { await assertNoDiagnostics(r''' class A {