From b0b393773fa7d7965c6c9ea3a501e93dc60812f9 Mon Sep 17 00:00:00 2001 From: pq Date: Tue, 9 May 2023 19:38:35 +0000 Subject: [PATCH] fix unused result checking for switch expression cases Fixes: https://github.com/dart-lang/sdk/issues/52314 Change-Id: I02a952ee94084599273193b509987f46ac6198e7 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/302381 Commit-Queue: Phil Quitslund Reviewed-by: Brian Wilkerson --- .../lib/src/error/use_result_verifier.dart | 1 + .../src/diagnostics/unused_result_test.dart | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/pkg/analyzer/lib/src/error/use_result_verifier.dart b/pkg/analyzer/lib/src/error/use_result_verifier.dart index fb024943fc5..b9e54899d63 100644 --- a/pkg/analyzer/lib/src/error/use_result_verifier.dart +++ b/pkg/analyzer/lib/src/error/use_result_verifier.dart @@ -235,6 +235,7 @@ class UseResultVerifier { parent is PropertyAccess || parent is ReturnStatement || parent is SetOrMapLiteral || + parent is SwitchExpressionCase || parent is SwitchStatement || parent is ThrowExpression || parent is VariableDeclaration || diff --git a/pkg/analyzer/test/src/diagnostics/unused_result_test.dart b/pkg/analyzer/test/src/diagnostics/unused_result_test.dart index 9ba34d67af3..7bd72f9f31e 100644 --- a/pkg/analyzer/test/src/diagnostics/unused_result_test.dart +++ b/pkg/analyzer/test/src/diagnostics/unused_result_test.dart @@ -1222,6 +1222,25 @@ const a = 'a'; '''); } + /// https://github.com/dart-lang/sdk/issues/52314 + test_switchExpression() async { + await assertNoErrorsInCode(r''' +import 'package:meta/meta.dart'; + +class C { + bool m(Object o) { + return switch (o) { + String() => methodWithAnnotation(), + _ => false, + }; + } + + @useResult + bool methodWithAnnotation() => true; +} +'''); + } + test_topLevelFunction_prefixExpression_bang() async { await assertNoErrorsInCode(r''' import 'package:meta/meta.dart';