Skip to content

Commit a2af8a5

Browse files
parloughCommit Queue
authored andcommitted
[analyzer] Mark switch expression's matched expression as used
Fixes unused_result being incorrectly reported. Change-Id: Ifcf06bdb3e2134cdd738f3f880290e5c1be7e16b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/448224 Reviewed-by: Paul Berry <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent 7431020 commit a2af8a5

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

pkg/analyzer/lib/src/error/use_result_verifier.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ class UseResultVerifier {
253253
parent is RecordLiteral ||
254254
parent is ReturnStatement ||
255255
parent is SetOrMapLiteral ||
256+
parent is SwitchExpression ||
256257
parent is SwitchExpressionCase ||
257258
parent is SwitchStatement ||
258259
parent is ThrowExpression ||

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1444,11 +1444,27 @@ class C {
14441444
''');
14451445
}
14461446

1447-
/// https://github.com/dart-lang/sdk/issues/52314
14481447
test_switchExpression() async {
14491448
await assertNoErrorsInCode(r'''
14501449
import 'package:meta/meta.dart';
14511450
1451+
void main() {
1452+
print(switch (methodWithAnnotation()) {
1453+
true => true,
1454+
_ => false,
1455+
});
1456+
}
1457+
1458+
@useResult
1459+
bool methodWithAnnotation() => true;
1460+
''');
1461+
}
1462+
1463+
/// https://github.com/dart-lang/sdk/issues/52314
1464+
test_switchExpressionCase() async {
1465+
await assertNoErrorsInCode(r'''
1466+
import 'package:meta/meta.dart';
1467+
14521468
class C {
14531469
bool m(Object o) {
14541470
return switch (o) {

0 commit comments

Comments
 (0)