linter: Move many unnecessary_statements tests

Not all of them; there are too many for one change.

Change-Id: I59f6a52c03ccbc4f5f81567b0f70c32fcf4b1f2e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/329569
Commit-Queue: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Phil Quitslund <pquitslund@google.com>
This commit is contained in:
Sam Rawlins 2023-10-13 03:51:23 +00:00 committed by Commit Queue
parent 7341ad214e
commit 06ab207091
2 changed files with 377 additions and 95 deletions

View file

@ -17,6 +17,275 @@ class UnnecessaryStatementsTest extends LintRuleTest {
@override
String get lintRule => 'unnecessary_statements';
test_asExpression() async {
// See https://github.com/dart-lang/linter/issues/2163.
await assertNoDiagnostics(r'''
void f(Object o) {
o as int;
}
''');
}
test_binaryOperation() async {
await assertDiagnostics(r'''
void f() {
1 + 1;
}
''', [
lint(13, 5),
]);
}
test_construcorTearoff_new() async {
await assertDiagnostics(r'''
void f() {
ArgumentError.new;
}
''', [
lint(13, 17),
]);
}
test_constructorTearoff_named() async {
await assertDiagnostics(r'''
void f() {
DateTime.now;
}
''', [
lint(13, 12),
]);
}
test_doStatement() async {
await assertNoDiagnostics(r'''
void f() {
do {} while (1 == 2);
}
''');
}
test_forEachStatement() async {
await assertNoDiagnostics(r'''
void f() {
for (var i in []) {}
}
''');
}
test_forStatement() async {
await assertNoDiagnostics(r'''
void f() {
for (; 1 == 2;) {}
}
''');
}
test_functionTearoff() async {
await assertDiagnostics(r'''
void f() {
g;
}
void g() {}
''', [
lint(13, 1),
]);
}
test_ifStatement() async {
await assertNoDiagnostics(r'''
void f() {
if (1 == 2) {
} else if (1 == 2) {
}
}
''');
}
test_instanceCreationExpression() async {
await assertNoDiagnostics(r'''
void f() {
List.empty();
}
''');
}
test_instanceField() async {
await assertDiagnostics(r'''
void f() {
C().g;
}
class C {
int g = 1;
}
''', [
lint(13, 5),
]);
}
test_instanceField2() async {
await assertDiagnostics(r'''
void f(C c) {
c.g;
}
class C {
int g = 1;
}
''', [
lint(16, 3),
]);
}
test_instanceGetter() async {
await assertNoDiagnostics(r'''
void f() {
List.empty().first;
}
''');
}
test_instanceGetter2() async {
await assertNoDiagnostics(r'''
void f(List<int> list) {
list.first;
}
''');
}
test_intLiteral() async {
await assertDiagnostics(r'''
void f() {
1;
}
''', [
lint(13, 1),
]);
}
test_listLiteral() async {
await assertDiagnostics(r'''
void f() {
[];
}
''', [
lint(13, 2),
]);
}
test_localVariable() async {
await assertDiagnostics(r'''
void f() {
var g = 1;
g;
}
''', [
lint(26, 1),
]);
}
test_mapLiteral() async {
await assertDiagnostics(r'''
void f() {
<dynamic, dynamic>{};
}
''', [
lint(13, 20),
]);
}
test_methodInvocation() async {
await assertNoDiagnostics(r'''
void f() {
g();
}
void g() {}
''');
}
test_methodInvocation2() async {
await assertNoDiagnostics(r'''
void f(List<int> list) {
list.forEach((_) {});
}
''');
}
test_methodTearoff() async {
await assertDiagnostics(r'''
void f() {
List.empty().where;
}
''', [
lint(13, 18),
]);
}
test_methodTearoff_cascaded() async {
await assertDiagnostics(r'''
void f() {
List.empty()..where;
}
''', [
lint(25, 7),
]);
}
test_methodTearoff_cascaded_followOn() async {
await assertDiagnostics(r'''
void f() {
List.empty()
..forEach((_) {})
..where;
}
''', [
lint(51, 7),
]);
}
test_methodTearoff_cascaded_returned_InLocalFunction() async {
await assertDiagnostics(r'''
void f() {
// ignore: unused_element
g() => List.empty()..where;
}
''', [
lint(60, 7),
]);
}
test_methodTearoff_cascaded_returned_InTopLevelFunction() async {
await assertDiagnostics(r'''
List<int> f() => List.empty()..where;
''', [
lint(29, 7),
]);
}
test_methodTearoff_returned_inFunctionLiteral() async {
await assertDiagnostics(r'''
void f() {
() => List.empty().where;
}
''', [
lint(13, 24),
]);
}
test_methodTearoff_returned_InLocalFunction() async {
await assertNoDiagnostics(r'''
void f() {
// ignore: unused_element
g() => List.empty().where;
}
''');
}
test_methodTearoff_returned_InTopLevelFunction() async {
await assertNoDiagnostics(r'''
Object f() => List.empty().where;
''');
}
/// https://github.com/dart-lang/linter/issues/4334
test_patternAssignment_ok() async {
await assertNoDiagnostics(r'''
@ -25,6 +294,114 @@ f() {
var result = (1, 2);
(a, b) = (a + result.$1, b + result.$2);
}
''');
}
test_rethrow() async {
await assertNoDiagnostics(r'''
void f() {
try {} catch (_) {
rethrow;
}
}
''');
}
test_returnStatement_binaryOperation() async {
await assertNoDiagnostics(r'''
int f() {
return 1 + 1;
}
''');
}
test_returnStatement_cascadedTearoff() async {
await assertDiagnostics(r'''
List<int> f() {
return List.empty()..where;
}
''', [
lint(37, 7),
]);
}
test_stringLiteral() async {
await assertDiagnostics(r'''
void f() {
"blah";
}
''', [
lint(13, 6),
]);
}
test_switchStatement() async {
await assertNoDiagnostics(r'''
void f() {
switch (~1) {}
}
''');
}
test_throwExpression() async {
await assertNoDiagnostics(r'''
void f() {
throw Exception();
}
''');
}
test_topLevelGetter() async {
await assertNoDiagnostics(r'''
void f() {
g;
}
int get g => 1;
''');
}
test_topLevelVariable() async {
await assertDiagnostics(r'''
void f() {
g;
}
int g = 1;
''', [
lint(13, 1),
]);
}
test_unaryOperation() async {
await assertDiagnostics(r'''
void f() {
~1;
}
''', [
lint(13, 2),
]);
}
test_unaryOperation_postfix() async {
await assertNoDiagnostics(r'''
void f(int x) {
x++;
}
''');
}
test_unaryOperation_prefix() async {
await assertNoDiagnostics(r'''
void f(int x) {
++x;
}
''');
}
test_whileStatement() async {
await assertNoDiagnostics(r'''
void f() {
while (1 == 2) {}
}
''');
}
}

View file

@ -2,93 +2,6 @@
// 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.
void constructorTearOffs() {
MyClass.new; // LINT
MyClass.named; // LINT
var m = MyClass.new;
m().foo; // LINT
m().field; // LINT
}
String f(Object o) {
// See: https://github.com/dart-lang/linter/issues/2163
o as int; // OK
return '';
}
notReturned() {
1; // LINT
1 + 1; // LINT
foo; // LINT
new MyClass().foo; // LINT
new MyClass()..foo; // LINT
new MyClass()
..getter // OK
..foo() // OK
..foo; // LINT
[]; // LINT
<dynamic, dynamic>{}; // LINT
"blah"; // LINT
~1; // LINT
getter; // OK
field; // LINT
new MyClass().getter; // OK
new MyClass().field; // LINT
var myClass = new MyClass();
myClass; // LINT
myClass.getter; // OK
myClass.field; // LINT
new MyClass(); // OK
foo(); // OK
new MyClass().foo(); // OK
var x = 2; // OK
x++; // OK
x--; // OK
++x; // OK
--x; // OK
try {
throw new Exception(); // OK
} catch (x) {
rethrow; // OK
}
}
asConditionAndReturnOk() {
if (true == someBool) // OK
{
return 1 + 1; // OK
} else if (false == someBool) {
return foo; // OK
}
while (new MyClass() != null) // OK
{
return new MyClass().foo; // OK
}
while (null == someBool) // OK
{
return new MyClass()..foo; // LINT
}
for (; someBool ?? someBool;) // OK
{
return <dynamic, dynamic>{}; // OK
}
do {} while ("blah".isEmpty); // OK
for (var i in []) {} // OK
switch (~1) // OK
{
}
() => new MyClass().foo; // LINT
myfun() => new MyClass().foo; // OK
myfun2() => new MyClass()..foo; // LINT
}
myfun() => new MyClass().foo; // OK
myfun2() => new MyClass()..foo; // LINT
expressionBranching() {
null ?? 1 + 1; // LINT
null ?? foo; // LINT
@ -175,15 +88,7 @@ inOtherStatements() {
bool someBool = true;
bool foo() => true;
get getter => true;
int field = 0;
class MyClass {
int field = 0;
bool foo() => true;
get getter => true;
MyClass();
MyClass.named();
}