Skip to content

Commit 42d8cdf

Browse files
scheglovCommit Queue
authored andcommitted
Issue 53405. Report PATTERN_NEVER_MATCHES_VALUE_TYPE got record patterns.
Bug: #53405 Change-Id: I139fc620f10f18ab2b44b806ab24fa09e9956f61 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/323685 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
1 parent 40c2a63 commit 42d8cdf

4 files changed

Lines changed: 57 additions & 8 deletions

File tree

pkg/analyzer/lib/src/dart/ast/ast.dart

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14926,6 +14926,8 @@ final class RecordPatternImpl extends DartPatternImpl implements RecordPattern {
1492614926
@override
1492714927
final Token rightParenthesis;
1492814928

14929+
bool hasDuplicateNamedField = false;
14930+
1492914931
RecordPatternImpl({
1493014932
required this.leftParenthesis,
1493114933
required List<PatternFieldImpl> fields,
@@ -14970,14 +14972,22 @@ final class RecordPatternImpl extends DartPatternImpl implements RecordPattern {
1497014972
ResolverVisitor resolverVisitor,
1497114973
SharedMatchContext context,
1497214974
) {
14973-
resolverVisitor.analyzeRecordPattern(
14975+
final result = resolverVisitor.analyzeRecordPattern(
1497414976
context,
1497514977
this,
1497614978
fields: resolverVisitor.buildSharedPatternFields(
1497714979
fields,
1497814980
mustBeNamed: false,
1497914981
),
1498014982
);
14983+
14984+
if (!hasDuplicateNamedField) {
14985+
resolverVisitor.checkPatternNeverMatchesValueType(
14986+
context: context,
14987+
pattern: this,
14988+
requiredType: result.requiredType,
14989+
);
14990+
}
1498114991
}
1498214992

1498314993
@override

pkg/analyzer/lib/src/dart/resolver/shared_type_analyzer.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ class SharedTypeAnalyzerErrors
8383
required covariant SharedPatternField original,
8484
required covariant SharedPatternField duplicate,
8585
}) {
86+
if (objectOrRecordPattern is RecordPatternImpl) {
87+
objectOrRecordPattern.hasDuplicateNamedField = true;
88+
}
8689
_errorReporter.reportError(
8790
DiagnosticFactory().duplicatePatternField(
8891
source: _errorReporter.source,

pkg/analyzer/test/src/dart/resolution/record_pattern_test.dart

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,8 @@ void f(() x) {
305305
}
306306
}
307307
''', [
308-
error(HintCode.UNUSED_LOCAL_VARIABLE, 47, 1),
308+
error(WarningCode.PATTERN_NEVER_MATCHES_VALUE_TYPE, 39, 10),
309+
error(WarningCode.UNUSED_LOCAL_VARIABLE, 47, 1),
309310
]);
310311
final node = findNode.singleGuardedPattern.pattern;
311312
assertResolvedNodeText(node, r'''
@@ -338,7 +339,8 @@ void f(() x) {
338339
}
339340
}
340341
''', [
341-
error(HintCode.UNUSED_LOCAL_VARIABLE, 46, 1),
342+
error(WarningCode.PATTERN_NEVER_MATCHES_VALUE_TYPE, 39, 9),
343+
error(WarningCode.UNUSED_LOCAL_VARIABLE, 46, 1),
342344
]);
343345
final node = findNode.singleGuardedPattern.pattern;
344346
assertResolvedNodeText(node, r'''
@@ -369,7 +371,8 @@ void f(({int b}) x) {
369371
}
370372
}
371373
''', [
372-
error(HintCode.UNUSED_LOCAL_VARIABLE, 53, 1),
374+
error(WarningCode.PATTERN_NEVER_MATCHES_VALUE_TYPE, 46, 9),
375+
error(WarningCode.UNUSED_LOCAL_VARIABLE, 53, 1),
373376
]);
374377
final node = findNode.singleGuardedPattern.pattern;
375378
assertResolvedNodeText(node, r'''
@@ -400,7 +403,8 @@ void f(({int a, int b}) x) {
400403
}
401404
}
402405
''', [
403-
error(HintCode.UNUSED_LOCAL_VARIABLE, 60, 1),
406+
error(WarningCode.PATTERN_NEVER_MATCHES_VALUE_TYPE, 53, 9),
407+
error(WarningCode.UNUSED_LOCAL_VARIABLE, 60, 1),
404408
]);
405409
final node = findNode.singleGuardedPattern.pattern;
406410
assertResolvedNodeText(node, r'''
@@ -432,7 +436,8 @@ void f(() x) {
432436
}
433437
}
434438
''', [
435-
error(HintCode.UNUSED_LOCAL_VARIABLE, 44, 1),
439+
error(WarningCode.PATTERN_NEVER_MATCHES_VALUE_TYPE, 39, 8),
440+
error(WarningCode.UNUSED_LOCAL_VARIABLE, 44, 1),
436441
]);
437442
final node = findNode.singleGuardedPattern.pattern;
438443
assertResolvedNodeText(node, r'''
@@ -461,7 +466,8 @@ void f((int, String) x) {
461466
}
462467
}
463468
''', [
464-
error(HintCode.UNUSED_LOCAL_VARIABLE, 55, 1),
469+
error(WarningCode.PATTERN_NEVER_MATCHES_VALUE_TYPE, 50, 8),
470+
error(WarningCode.UNUSED_LOCAL_VARIABLE, 55, 1),
465471
]);
466472
final node = findNode.singleGuardedPattern.pattern;
467473
assertResolvedNodeText(node, r'''
@@ -561,7 +567,8 @@ void f(({int foo}) x) {
561567
}
562568
}
563569
''', [
564-
error(HintCode.UNUSED_LOCAL_VARIABLE, 58, 1),
570+
error(WarningCode.PATTERN_NEVER_MATCHES_VALUE_TYPE, 48, 12),
571+
error(WarningCode.UNUSED_LOCAL_VARIABLE, 58, 1),
565572
]);
566573
final node = findNode.singleGuardedPattern.pattern;
567574
assertResolvedNodeText(node, r'''
@@ -625,6 +632,7 @@ void f(({int foo}) x) {
625632
}
626633
}
627634
''', [
635+
error(WarningCode.PATTERN_NEVER_MATCHES_VALUE_TYPE, 48, 5),
628636
error(CompileTimeErrorCode.MISSING_NAMED_PATTERN_FIELD_NAME, 49, 3),
629637
]);
630638
final node = findNode.singleGuardedPattern.pattern;

pkg/analyzer/test/src/diagnostics/pattern_never_matches_value_type_test.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,34 @@ void f(String x) {
10401040
]);
10411041
}
10421042

1043+
test_refutable_pattern_reportPattern_match() async {
1044+
await assertErrorsInCode('''
1045+
void f((int,) x) {
1046+
switch (x) {
1047+
case (int f,):
1048+
break;
1049+
}
1050+
}
1051+
''', [
1052+
error(WarningCode.UNUSED_LOCAL_VARIABLE, 48, 1),
1053+
]);
1054+
}
1055+
1056+
test_refutable_pattern_reportPattern_notMatch() async {
1057+
await assertErrorsInCode('''
1058+
void f((int,) x) {
1059+
switch (x) {
1060+
case (int f1, int f2):
1061+
break;
1062+
}
1063+
}
1064+
''', [
1065+
error(WarningCode.PATTERN_NEVER_MATCHES_VALUE_TYPE, 43, 16),
1066+
error(WarningCode.UNUSED_LOCAL_VARIABLE, 48, 2),
1067+
error(WarningCode.UNUSED_LOCAL_VARIABLE, 56, 2),
1068+
]);
1069+
}
1070+
10431071
test_refutable_pattern_wildcard_match() async {
10441072
await assertNoErrorsInCode('''
10451073
void f(num x) {

0 commit comments

Comments
 (0)