diff --git a/pkg/analysis_server/test/src/services/correction/fix/convert_to_constant_pattern_test.dart b/pkg/analysis_server/test/src/services/correction/fix/convert_to_constant_pattern_test.dart index 7e0f1078a00..6b6fe8ac916 100644 --- a/pkg/analysis_server/test/src/services/correction/fix/convert_to_constant_pattern_test.dart +++ b/pkg/analysis_server/test/src/services/correction/fix/convert_to_constant_pattern_test.dart @@ -32,34 +32,6 @@ void f(Object x) { void f(Object x) { if (x case const (int)) {} } -'''); - } - - Future test_constType_matchType() async { - await resolveTestCode(''' -void f(Type x) { - if (x case int) {} -} -'''); - await assertHasFix(''' -void f(Type x) { - if (x case const (int)) {} -} -'''); - } - - Future test_constType_matchType_withImportPrefix() async { - await resolveTestCode(''' -import 'dart:math' as math; -void f(Type x) { - if (x case math.Random) {} -} -'''); - await assertHasFix(''' -import 'dart:math' as math; -void f(Type x) { - if (x case const (math.Random)) {} -} '''); } } diff --git a/pkg/linter/CHANGELOG.md b/pkg/linter/CHANGELOG.md index 0d2faaa5fb8..e504b90fc3c 100644 --- a/pkg/linter/CHANGELOG.md +++ b/pkg/linter/CHANGELOG.md @@ -3,6 +3,8 @@ - new lint: `unnecessary_const_in_enum_constructor` - new lint: `unnecessary_type_name_in_constructor` - stable: `unnecessary_null_checks` +- update `type_literal_in_constant_pattern` to ignore cases where the matched + pattern is of type `Type`. # 3.12.0 diff --git a/pkg/linter/lib/src/rules/type_literal_in_constant_pattern.dart b/pkg/linter/lib/src/rules/type_literal_in_constant_pattern.dart index 86b144526ef..62da2ad97f6 100644 --- a/pkg/linter/lib/src/rules/type_literal_in_constant_pattern.dart +++ b/pkg/linter/lib/src/rules/type_literal_in_constant_pattern.dart @@ -47,6 +47,17 @@ class _Visitor extends SimpleAstVisitor { return; } + // OK to use raw types if the matched value is of type Type. + var matchedValueType = node.matchedValueType; + if (matchedValueType != null && + // Have to use `isSubtypeOf` to catch generic type parameters. + context.typeSystem.isSubtypeOf( + matchedValueType, + context.typeProvider.typeType, + )) { + return; + } + var expressionType = node.expression.staticType; if (expressionType != null && expressionType.isDartCoreType) { rule.reportAtNode(node); diff --git a/pkg/linter/test/rules/type_literal_in_constant_pattern_test.dart b/pkg/linter/test/rules/type_literal_in_constant_pattern_test.dart index a863b6f0684..db93bd5565a 100644 --- a/pkg/linter/test/rules/type_literal_in_constant_pattern_test.dart +++ b/pkg/linter/test/rules/type_literal_in_constant_pattern_test.dart @@ -62,9 +62,9 @@ void f(Object? x) { } test_constType_matchType() async { - await assertDiagnosticsFromMarkdown(r''' + await assertNoDiagnostics(r''' void f(Type x) { - if (x case [!int!]) {} + if (x case int) {} } '''); } @@ -78,9 +78,9 @@ void f(Type x) { } test_constType_matchType_nested() async { - await assertDiagnosticsFromMarkdown(r''' + await assertNoDiagnostics(r''' void f(A x) { - if (x case A(type: [!int!])) {} + if (x case A(type: int)) {} } class A { @@ -100,9 +100,17 @@ void f(T x) { /// Nobody will write such code, but just in case. test_constType_matchTypeParameter_boundType() async { - await assertDiagnosticsFromMarkdown(r''' + await assertNoDiagnostics(r''' void f(T x) { - if (x case [!int!]) {} + if (x case int) {} +} +'''); + } + + test_constType_matchTypeParameter_variable() async { + await assertNoDiagnostics(r''' +void f() { + if (T case int) {} } '''); }