fd59a1c33f
Doesn't enable strict-inference but fixes a few of the more impactful cases. Change-Id: Ife08f1839036c420108bf603f4bce3ab7bfea5c4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/330000 Commit-Queue: Phil Quitslund <pquitslund@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Reviewed-by: Phil Quitslund <pquitslund@google.com>
135 lines
2.1 KiB
Dart
135 lines
2.1 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/utilities/legacy.dart';
|
|
import 'package:test_reflective_loader/test_reflective_loader.dart';
|
|
|
|
import '../rule_test_support.dart';
|
|
|
|
main() {
|
|
defineReflectiveSuite(() {
|
|
defineReflectiveTests(OnlyThrowErrorsTest);
|
|
});
|
|
}
|
|
|
|
@reflectiveTest
|
|
class OnlyThrowErrorsTest extends LintRuleTest {
|
|
@override
|
|
String get lintRule => 'only_throw_errors';
|
|
|
|
@override
|
|
void setUp() {
|
|
super.setUp();
|
|
noSoundNullSafety = false;
|
|
}
|
|
|
|
void tearDown() {
|
|
noSoundNullSafety = true;
|
|
}
|
|
|
|
test_argumentError() async {
|
|
await assertNoDiagnostics(r'''
|
|
void f() {
|
|
throw ArgumentError('hello');
|
|
}
|
|
''');
|
|
}
|
|
|
|
test_error() async {
|
|
await assertNoDiagnostics(r'''
|
|
void f() {
|
|
throw Error();
|
|
}
|
|
''');
|
|
}
|
|
|
|
test_exception() async {
|
|
await assertNoDiagnostics(r'''
|
|
void f() {
|
|
throw ArgumentError('hello');
|
|
}
|
|
''');
|
|
}
|
|
|
|
test_exceptionGeneric() async {
|
|
await assertNoDiagnostics(r'''
|
|
void f<E extends Exception>(E error) {
|
|
throw error;
|
|
}
|
|
''');
|
|
}
|
|
|
|
test_exceptionGenericUnboundedAndPromoted() async {
|
|
await assertNoDiagnostics(r'''
|
|
void f<E>(E error) {
|
|
if (error is Error) {
|
|
throw error;
|
|
}
|
|
}
|
|
''');
|
|
}
|
|
|
|
test_exceptionMixedIn() async {
|
|
await assertNoDiagnostics(r'''
|
|
// @dart=2.19
|
|
class Err extends Object with Exception {}
|
|
|
|
void f() {
|
|
throw Err();
|
|
}
|
|
''');
|
|
}
|
|
|
|
test_int() async {
|
|
await assertDiagnostics(r'''
|
|
void f() {
|
|
throw 7;
|
|
}
|
|
''', [
|
|
lint(19, 1),
|
|
]);
|
|
}
|
|
|
|
test_never() async {
|
|
await assertNoDiagnostics(r'''
|
|
void f() {
|
|
throw e();
|
|
}
|
|
|
|
Never e() => throw Exception();
|
|
''');
|
|
}
|
|
|
|
test_nullInPreNullSafe() async {
|
|
await assertDiagnostics(r'''
|
|
// @dart=2.9
|
|
void f() {
|
|
throw null;
|
|
}
|
|
''', [
|
|
lint(32, 4),
|
|
]);
|
|
}
|
|
|
|
test_object() async {
|
|
await assertDiagnostics(r'''
|
|
void f() {
|
|
throw Object();
|
|
}
|
|
''', [
|
|
lint(19, 8),
|
|
]);
|
|
}
|
|
|
|
test_string() async {
|
|
await assertDiagnostics(r'''
|
|
void f() {
|
|
throw 'hello';
|
|
}
|
|
''', [
|
|
lint(19, 7),
|
|
]);
|
|
}
|
|
}
|