Add fix for unreachable_from_main

We have a fix to remove private declarations (classes, other elements, local variables, etc). No reason to not allow this for public elements in entry-point libraries.

Change-Id: Id4c0ef1b14658d701229ea71d25669982e98d6c1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/313291
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
diff --git a/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml b/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml
index a005a14..4903050 100644
--- a/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml
+++ b/pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml
@@ -2400,10 +2400,7 @@
 LintCode.unnecessary_to_list_in_spreads:
   status: hasFix
 LintCode.unreachable_from_main:
-  status: noFix
-  notes: |-
-    Theoretically we could remove the unreachable declaration, but that seems
-    like a dangerous option to provide.
+  status: hasFix
 LintCode.unrelated_type_equality_checks_expression:
   status: needsEvaluation
 LintCode.unrelated_type_equality_checks_pattern:
diff --git a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
index 7ee896f..683275f 100644
--- a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
+++ b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
@@ -781,6 +781,9 @@
     LintNames.unnecessary_this: [
       RemoveThisExpression.new,
     ],
+    LintNames.unreachable_from_main: [
+      RemoveUnusedElement.new,
+    ],
     LintNames.use_decorated_box: [
       ReplaceWithDecoratedBox.new,
     ],
diff --git a/pkg/analysis_server/lib/src/services/linter/lint_names.dart b/pkg/analysis_server/lib/src/services/linter/lint_names.dart
index e2ce6ed..99595bb 100644
--- a/pkg/analysis_server/lib/src/services/linter/lint_names.dart
+++ b/pkg/analysis_server/lib/src/services/linter/lint_names.dart
@@ -184,6 +184,7 @@
   static const String unnecessary_to_list_in_spreads =
       'unnecessary_to_list_in_spreads';
   static const String unnecessary_this = 'unnecessary_this';
+  static const String unreachable_from_main = 'unreachable_from_main';
   static const String use_decorated_box = 'use_decorated_box';
   static const String use_enums = 'use_enums';
   static const String use_full_hex_values_for_flutter_colors =
diff --git a/pkg/analysis_server/test/src/services/correction/fix/remove_unused_element_test.dart b/pkg/analysis_server/test/src/services/correction/fix/remove_unused_element_test.dart
index 5711cbd..406fd6e 100644
--- a/pkg/analysis_server/test/src/services/correction/fix/remove_unused_element_test.dart
+++ b/pkg/analysis_server/test/src/services/correction/fix/remove_unused_element_test.dart
@@ -3,6 +3,7 @@
 // BSD-style license that can be found in the LICENSE file.
 
 import 'package:analysis_server/src/services/correction/fix.dart';
+import 'package:analysis_server/src/services/linter/lint_names.dart';
 import 'package:analyzer/error/error.dart';
 import 'package:analyzer/src/error/codes.dart';
 import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
@@ -12,11 +13,49 @@
 
 void main() {
   defineReflectiveSuite(() {
+    defineReflectiveTests(RemoveUnreachableFromMainTest);
     defineReflectiveTests(RemoveUnusedElementTest);
   });
 }
 
 @reflectiveTest
+class RemoveUnreachableFromMainTest extends FixProcessorLintTest {
+  @override
+  FixKind get kind => DartFixKind.REMOVE_UNUSED_ELEMENT;
+
+  @override
+  String get lintCode => LintNames.unreachable_from_main;
+
+  Future<void> test_class() async {
+    await resolveTestCode(r'''
+void main() {}
+class C {}
+''');
+    await assertHasFix(r'''
+void main() {}
+''');
+  }
+
+  Future<void> test_method() async {
+    await resolveTestCode(r'''
+void main() {
+  C();
+}
+class C {
+  void m() {}
+}
+''');
+    await assertHasFix(r'''
+void main() {
+  C();
+}
+class C {
+}
+''');
+  }
+}
+
+@reflectiveTest
 class RemoveUnusedElementTest extends FixProcessorTest {
   @override
   FixKind get kind => DartFixKind.REMOVE_UNUSED_ELEMENT;
@@ -87,8 +126,7 @@
     await resolveTestCode(r'''
 class _A {}
 void f(p) {
-  if (p is _A) {
-  }
+  if (p is _A) {}
 }
 ''');
     // We don't know what to do  with the reference.
@@ -97,8 +135,7 @@
 
   Future<void> test_class_notUsed_noReference() async {
     await resolveTestCode(r'''
-class _A {
-}
+class _A {}
 ''');
     await assertHasFix(r'''
 ''');
@@ -161,24 +198,20 @@
   Future<void> test_functionTop_notUsed_noReference() async {
     await resolveTestCode(r'''
 _f() {}
-void f() {
-}
+void f() {}
 ''');
     await assertHasFix(r'''
-void f() {
-}
+void f() {}
 ''');
   }
 
   Future<void> test_functionTypeAlias_notUsed_noReference() async {
     await resolveTestCode(r'''
 typedef _F(a, b);
-void f() {
-}
+void f() {}
 ''');
     await assertHasFix(r'''
-void f() {
-}
+void f() {}
 ''');
   }