Files
sdk/pkg/linter/test/rules/avoid_empty_else_test.dart
T
Paul Berry 9f6d1c1029 [messages] Start using toplevel diagnostic constants.
Changes the analyzer and related packages so that when they refer to
diagnostic constants, they do so via the import prefix `diag`, which
refers to the appropriate `diagnostic.dart` file containing the top
level diagnostic constant declarations, rather than the static
declarations inside `DiagnosticCode`-derived classes (which will soon
be removed).

This CL was created by the following steps:

- Run the script
  `pkg/analyzer_utilities/tool/messages/switch_to_toplevel_diagnostics.dart`.

- Execute `dart fix --apply --code=unused_import,unnecessary_import`
  on the following directories (this removes imports that are no
  longer necessary due to the change):
  - `pkg/analysis_server`
  - `pkg/analyzer`
  - `pkg/linter`
  - `pkg/analysis_server_plugin`
  - `pkg/analyzer_plugin`
  - `pkg/analyzer_testing`
  - `pkg/front_end`
  - `pkg/analyzer_cli`

- Execute `dart format` on the following files and directories:
  - `pkg/analysis_server`
  - `pkg/analyzer`
  - `pkg/linter`
  - `pkg/analysis_server_plugin`
  - `pkg/analyzer_plugin`
  - `pkg/analyzer_testing`
  - `pkg/front_end/test/scanner_test.dart`

  (Note that `pkg/front_end` and `pkg/analyzer_cli` are not
  re-formatted as whole directories because they contain `.dart` files
  that are test cases rather than source code, and reformatting those
  files might change test expectations.)

- Manually add `diag` to
  pkg/front_end/test/spell_checking_list_tests.txt.

- Manually fix the ignore comment in
  `pkg/analyzer_testing/lib/src/analysis_rule/pub_package_resolution.dart`. (The
  script `switch_to_toplevel_diagnostics.dart` automatically adds it
  after `import 'package:analyzer/src/diagnostic/diagnostic.dart' as
  diag;`, but then executing `dart format` bumps the ignore comment to
  the following line, where it has no effect.)

Change-Id: I6a6a69643022aab2b5a6224fb4124eead243260d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/461521
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2025-11-12 16:53:42 -08:00

101 lines
1.7 KiB
Dart

// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// 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/src/diagnostic/diagnostic.dart' as diag;
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../rule_test_support.dart';
void main() {
defineReflectiveSuite(() {
defineReflectiveTests(AvoidEmptyElseTest);
});
}
@reflectiveTest
class AvoidEmptyElseTest extends LintRuleTest {
@override
String get lintRule => LintNames.avoid_empty_else;
test_else_emptyStatement_hasElseIf() async {
await assertDiagnostics(
r'''
void f() {
var x = 0;
var y = 1;
if (x > y)
print('');
else if (x < y)
print('');
else ;
print('');
}
''',
[lint(105, 1)],
);
}
test_else_emptyStatement_noElseIf() async {
await assertDiagnostics(
r'''
void f() {
var x = 0;
var y = 1;
if (x > y)
print('');
else ;
print('');
}
''',
[lint(72, 1)],
);
}
test_else_noEmptyStatement_enclosed() async {
await assertNoDiagnostics(r'''
void f() {
var x = 0;
var y = 1;
if (x > y) {
print('');
} else {
print('');
}
}
''');
}
test_else_noEmptyStatement_notEnclosed() async {
await assertNoDiagnostics(r'''
void f() {
var x = 0;
var y = 1;
if (x > y)
print('');
else
print('');
}
''');
}
test_else_noStatement_notEnclosed() async {
await assertDiagnostics(
r'''
void f() {
var x = 0;
var y = 1;
if (x > y)
print('');
else
}
''',
[
// No lint
error(diag.expectedToken, 67, 4),
error(diag.missingIdentifier, 72, 1),
],
);
}
}