From 87f230401f780e8849c90562d4f12fd10f9f38e1 Mon Sep 17 00:00:00 2001 From: Rohit Date: Wed, 17 Sep 2025 19:02:15 -0400 Subject: [PATCH] `unreachable_from_main`: exempt `setUpClass`, `tearDownClass` The PR dart-lang/tools#2164 introduces the ability to define `setUpClass` and `tearDownClass` static methods in test classes. Currently, such methods are linted as `unreachable_from_main` when they should not be linted just like other test methods --- .../lib/src/rules/unreachable_from_main.dart | 4 +- .../rules/unreachable_from_main_test.dart | 46 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/pkg/linter/lib/src/rules/unreachable_from_main.dart b/pkg/linter/lib/src/rules/unreachable_from_main.dart index 0d941190c96a..e25fe3fb9182 100644 --- a/pkg/linter/lib/src/rules/unreachable_from_main.dart +++ b/pkg/linter/lib/src/rules/unreachable_from_main.dart @@ -129,7 +129,9 @@ class _DeclarationGatherer { rawName.startsWith('test_') || rawName.startsWith('solo_test_') || rawName == 'setUp' || - rawName == 'tearDown'; + rawName == 'tearDown' || + rawName == 'setUpClass' || + rawName == 'tearDownClass'; if (!isOverride(element) && !isTestMethod) { declarations.add(member); } diff --git a/pkg/linter/test/rules/unreachable_from_main_test.dart b/pkg/linter/test/rules/unreachable_from_main_test.dart index c34a22c229bb..19853291c659 100644 --- a/pkg/linter/test/rules/unreachable_from_main_test.dart +++ b/pkg/linter/test/rules/unreachable_from_main_test.dart @@ -1151,6 +1151,29 @@ mixin M {} ); } + test_setUpClass_class_static_member_reachable() async { + await assertNoDiagnostics(''' +void main() { + A(); +} + +final class A { + static setUpClass() {} +} +'''); + } + + test_setUpClass_top_level_unreachable() async { + await assertDiagnostics( + ''' +void main() {} + +setUpClass() {} +''', + [lint(16, 10)], + ); + } + test_staticField_unreachable() async { await assertDiagnostics( r''' @@ -1349,6 +1372,29 @@ class C { ); } + test_tearDownClass_class_static_member_reachable() async { + await assertNoDiagnostics(''' +void main() { + A(); +} + +final class A { + static tearDownClass() {} +} +'''); + } + + test_tearDownClass_top_level_unreachable() async { + await assertDiagnostics( + ''' +void main() {} + +tearDownClass() {} +''', + [lint(16, 13)], + ); + } + test_topLevelFunction_reachable() async { await assertNoDiagnostics(r''' void main() {