From 3bdc1f9c59fc6bb9c968523c12dee124b6291ac4 Mon Sep 17 00:00:00 2001 From: pq Date: Thu, 16 May 2024 17:31:30 +0000 Subject: [PATCH] [wildcards] `UNUSED_CATCH_CLAUSE` tests Fixes: https://github.com/dart-lang/sdk/issues/55721 Change-Id: I5e675c61a7d1eb0f6192f98146fd2dbd5dd0e2e5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/366407 Commit-Queue: Phil Quitslund Reviewed-by: Kallen Tu --- .../diagnostics/unused_catch_clause_test.dart | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/pkg/analyzer/test/src/diagnostics/unused_catch_clause_test.dart b/pkg/analyzer/test/src/diagnostics/unused_catch_clause_test.dart index 82b14009cde..004f729f476 100644 --- a/pkg/analyzer/test/src/diagnostics/unused_catch_clause_test.dart +++ b/pkg/analyzer/test/src/diagnostics/unused_catch_clause_test.dart @@ -2,6 +2,7 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. +import 'package:analyzer/dart/analysis/features.dart'; import 'package:analyzer/src/error/codes.g.dart'; import 'package:test_reflective_loader/test_reflective_loader.dart'; @@ -10,6 +11,7 @@ import '../dart/resolution/context_collection_resolution.dart'; main() { defineReflectiveSuite(() { defineReflectiveTests(UnusedCatchClauseTest); + defineReflectiveTests(UnusedCatchClauseTestWildCardVariablesTest); }); } @@ -27,6 +29,16 @@ main() { ]); } + test_on_unusedStack_wildcard() async { + await assertNoErrorsInCode(r''' +main() { + try { + } on String catch (exception, _) { + } +} +'''); + } + test_on_usedException() async { await assertNoErrorsInCode(r''' main() { @@ -48,6 +60,26 @@ main() { '''); } + test_unusedException_underscores() async { + await assertNoErrorsInCode(r''' +main() { + try { + } catch (__) { + } +} +'''); + } + + test_unusedException_wildcard() async { + await assertNoErrorsInCode(r''' +main() { + try { + } catch (_) { + } +} +'''); + } + test_usedException() async { await assertNoErrorsInCode(r''' main() { @@ -59,3 +91,24 @@ main() { '''); } } + +@reflectiveTest +class UnusedCatchClauseTestWildCardVariablesTest extends UnusedCatchClauseTest { + @override + List get experiments => [ + ...super.experiments, + Feature.wildcard_variables.enableString, + ]; + + test_on_unusedStack_underscores() async { + await assertErrorsInCode(r''' +main() { + try { + } on String catch (exception, __) { + } +} +''', [ + error(WarningCode.UNUSED_CATCH_STACK, 49, 2), + ]); + } +}