Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit c696ecf

Browse files
author
Dart CI
committed
Version 2.14.0-145.0.dev
Merge commit '538e47a1fc83687a0f5d15080807a6c41599cce7' into 'dev'
2 parents cf0a921 + 538e47a commit c696ecf

File tree

15 files changed

+587
-140
lines changed

15 files changed

+587
-140
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
6+
import 'package:analysis_server/src/services/correction/fix.dart';
7+
import 'package:analyzer/dart/ast/ast.dart';
8+
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
9+
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
10+
import 'package:analyzer_plugin/utilities/range_factory.dart';
11+
12+
class RemoveReturnedValue extends CorrectionProducer {
13+
@override
14+
FixKind get fixKind => DartFixKind.REMOVE_RETURNED_VALUE;
15+
16+
@override
17+
FixKind get multiFixKind => DartFixKind.REMOVE_RETURNED_VALUE_MULTI;
18+
19+
@override
20+
Future<void> compute(ChangeBuilder builder) async {
21+
final node = this.node;
22+
if (node is ReturnStatement) {
23+
await builder.addDartFileEdit(file, (builder) {
24+
builder.addDeletion(range.endStart(node.returnKeyword, node.semicolon));
25+
});
26+
}
27+
}
28+
29+
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
30+
static RemoveReturnedValue newInstance() => RemoveReturnedValue();
31+
}

pkg/analysis_server/lib/src/services/correction/fix.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,12 @@ class DartFixKind {
621621
'dart.fix.remove.questionMark.multi',
622622
DartFixKindPriority.IN_FILE,
623623
'Remove unnecessary question marks in file');
624+
static const REMOVE_RETURNED_VALUE = FixKind('dart.fix.remove.returnedValue',
625+
DartFixKindPriority.DEFAULT, 'Remove invalid returned value');
626+
static const REMOVE_RETURNED_VALUE_MULTI = FixKind(
627+
'dart.fix.remove.returnedValue.multi',
628+
DartFixKindPriority.IN_FILE,
629+
'Remove invalid returned values in file');
624630
static const REMOVE_THIS_EXPRESSION = FixKind(
625631
'dart.fix.remove.thisExpression',
626632
DartFixKindPriority.DEFAULT,

pkg/analysis_server/lib/src/services/correction/fix_internal.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ import 'package:analysis_server/src/services/correction/dart/remove_operator.dar
109109
import 'package:analysis_server/src/services/correction/dart/remove_parameters_in_getter_declaration.dart';
110110
import 'package:analysis_server/src/services/correction/dart/remove_parentheses_in_getter_invocation.dart';
111111
import 'package:analysis_server/src/services/correction/dart/remove_question_mark.dart';
112+
import 'package:analysis_server/src/services/correction/dart/remove_returned_value.dart';
112113
import 'package:analysis_server/src/services/correction/dart/remove_this_expression.dart';
113114
import 'package:analysis_server/src/services/correction/dart/remove_type_annotation.dart';
114115
import 'package:analysis_server/src/services/correction/dart/remove_type_arguments.dart';
@@ -458,6 +459,15 @@ class FixProcessor extends BaseProcessor {
458459
],
459460
)
460461
],
462+
LintNames.avoid_returning_null_for_void: [
463+
FixInfo(
464+
canBeAppliedToFile: true,
465+
canBeBulkApplied: true,
466+
generators: [
467+
RemoveReturnedValue.newInstance,
468+
],
469+
)
470+
],
461471
LintNames.avoid_single_cascade_in_expression_statements: [
462472
FixInfo(
463473
canBeAppliedToFile: true,

pkg/analysis_server/lib/src/services/linter/lint_names.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class LintNames {
2525
'avoid_return_types_on_setters';
2626
static const String avoid_returning_null_for_future =
2727
'avoid_returning_null_for_future';
28+
static const String avoid_returning_null_for_void =
29+
'avoid_returning_null_for_void';
2830
static const String avoid_single_cascade_in_expression_statements =
2931
'avoid_single_cascade_in_expression_statements';
3032
static const String avoid_types_as_parameter_names =
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analysis_server/src/services/correction/fix.dart';
6+
import 'package:analysis_server/src/services/linter/lint_names.dart';
7+
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
8+
import 'package:test_reflective_loader/test_reflective_loader.dart';
9+
10+
import 'bulk/bulk_fix_processor.dart';
11+
import 'fix_processor.dart';
12+
13+
void main() {
14+
defineReflectiveSuite(() {
15+
defineReflectiveTests(RemoveReturnedValueBulkTest);
16+
defineReflectiveTests(RemoveReturnedValueTest);
17+
});
18+
}
19+
20+
@reflectiveTest
21+
class RemoveReturnedValueBulkTest extends BulkFixProcessorTest {
22+
@override
23+
String get lintCode => LintNames.avoid_returning_null_for_void;
24+
25+
Future<void> test_simple() async {
26+
await resolveTestCode('''
27+
void f(bool b) {
28+
if (b) {
29+
return null;
30+
} else {
31+
return null;
32+
}
33+
}
34+
''');
35+
await assertHasFix('''
36+
void f(bool b) {
37+
if (b) {
38+
return;
39+
} else {
40+
return;
41+
}
42+
}
43+
''');
44+
}
45+
}
46+
47+
@reflectiveTest
48+
class RemoveReturnedValueTest extends FixProcessorLintTest {
49+
@override
50+
FixKind get kind => DartFixKind.REMOVE_RETURNED_VALUE;
51+
52+
@override
53+
String get lintCode => LintNames.avoid_returning_null_for_void;
54+
55+
Future<void> test_simple() async {
56+
await resolveTestCode('''
57+
void f() {
58+
return null;
59+
}
60+
''');
61+
await assertHasFix('''
62+
void f() {
63+
return;
64+
}
65+
''');
66+
}
67+
}

pkg/analysis_server/test/src/services/correction/fix/test_all.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ import 'remove_parameters_in_getter_declaration_test.dart'
126126
import 'remove_parentheses_in_getter_invocation_test.dart'
127127
as remove_parentheses_in_getter_invocation;
128128
import 'remove_question_mark_test.dart' as remove_question_mark;
129+
import 'remove_returned_value_test.dart' as remove_returned_value;
129130
import 'remove_this_expression_test.dart' as remove_this_expression;
130131
import 'remove_type_annotation_test.dart' as remove_type_annotation;
131132
import 'remove_type_arguments_test.dart' as remove_type_arguments;
@@ -290,6 +291,7 @@ void main() {
290291
remove_parameters_in_getter_declaration.main();
291292
remove_parentheses_in_getter_invocation.main();
292293
remove_question_mark.main();
294+
remove_returned_value.main();
293295
remove_this_expression.main();
294296
remove_type_annotation.main();
295297
remove_type_arguments.main();

pkg/analyzer/lib/src/summary2/library_builder.dart

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -82,39 +82,6 @@ class LibraryBuilder {
8282
}
8383
}
8484

85-
/// Add top-level declaration of the library units to the local scope.
86-
void addLocalDeclarations() {
87-
for (var linkingUnit in context.units) {
88-
for (var node in linkingUnit.unit.declarations) {
89-
if (node is ast.ClassDeclaration) {
90-
// Handled in ElementBuilder.
91-
} else if (node is ast.ClassTypeAlias) {
92-
// Handled in ElementBuilder.
93-
} else if (node is ast.EnumDeclarationImpl) {
94-
// Handled in ElementBuilder.
95-
} else if (node is ast.ExtensionDeclarationImpl) {
96-
// Handled in ElementBuilder.
97-
} else if (node is ast.FunctionDeclarationImpl) {
98-
// Handled in ElementBuilder.
99-
} else if (node is ast.FunctionTypeAlias) {
100-
// Handled in ElementBuilder.
101-
} else if (node is ast.GenericTypeAlias) {
102-
// Handled in ElementBuilder.
103-
} else if (node is ast.MixinDeclarationImpl) {
104-
// Handled in ElementBuilder.
105-
} else if (node is ast.TopLevelVariableDeclaration) {
106-
// Handled in ElementBuilder.
107-
} else {
108-
throw UnimplementedError('${node.runtimeType}');
109-
}
110-
}
111-
}
112-
if ('$uri' == 'dart:core') {
113-
localScope.declare('dynamic', reference.getChild('dynamic'));
114-
localScope.declare('Never', reference.getChild('Never'));
115-
}
116-
}
117-
11885
/// Return `true` if the export scope was modified.
11986
bool addToExportScope(String name, Reference reference) {
12087
if (name.startsWith('_')) return false;
@@ -135,6 +102,8 @@ class LibraryBuilder {
135102
as LibraryElementImpl;
136103
}
137104

105+
/// Build elements for declarations in the library units, add top-level
106+
/// declarations to the local scope, for combining into export scopes.
138107
void buildElements() {
139108
for (var unitContext in context.units) {
140109
var elementBuilder = ElementBuilder(
@@ -147,6 +116,10 @@ class LibraryBuilder {
147116
}
148117
elementBuilder.buildDeclarationElements(unitContext.unit);
149118
}
119+
if ('$uri' == 'dart:core') {
120+
localScope.declare('dynamic', reference.getChild('dynamic'));
121+
localScope.declare('Never', reference.getChild('Never'));
122+
}
150123
}
151124

152125
void buildEnumChildren() {

pkg/analyzer/lib/src/summary2/link.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ class Linker {
119119
}
120120

121121
for (var library in builders.values) {
122-
library.addLocalDeclarations();
123122
library.buildElements();
124123
}
125124

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'package:analyzer/src/error/codes.dart';
6+
import 'package:test_reflective_loader/test_reflective_loader.dart';
7+
8+
import '../dart/resolution/context_collection_resolution.dart';
9+
10+
main() {
11+
defineReflectiveSuite(() {
12+
defineReflectiveTests(ImplicitDynamicFieldTest);
13+
defineReflectiveTests(ImplicitDynamicFieldWithoutNullSafetyTest);
14+
});
15+
}
16+
17+
@reflectiveTest
18+
class ImplicitDynamicFieldTest extends PubPackageResolutionTest
19+
with ImplicitDynamicFieldTestCases {}
20+
21+
mixin ImplicitDynamicFieldTestCases on PubPackageResolutionTest {
22+
@override
23+
void setUp() {
24+
super.setUp();
25+
writeTestPackageAnalysisOptionsFile(
26+
AnalysisOptionsFileConfig(implicitDynamic: false),
27+
);
28+
}
29+
30+
test_instance_explicitDynamic_initialized() async {
31+
await assertNoErrorsInCode('''
32+
class C {
33+
dynamic f = (<dynamic>[])[0];
34+
}
35+
''');
36+
}
37+
38+
test_instance_explicitDynamic_uninitialized() async {
39+
await assertNoErrorsInCode('''
40+
class C {
41+
dynamic f;
42+
}
43+
''');
44+
}
45+
46+
test_instance_final_initialized() async {
47+
await assertErrorsInCode('''
48+
class C {
49+
final f = (<dynamic>[])[0];
50+
}
51+
''', [
52+
error(LanguageCode.IMPLICIT_DYNAMIC_FIELD, 18, 20),
53+
]);
54+
}
55+
56+
test_instance_final_uninitialized() async {
57+
await assertErrorsInCode('''
58+
class C {
59+
final f;
60+
C(this.f);
61+
}
62+
''', [
63+
error(LanguageCode.IMPLICIT_DYNAMIC_FIELD, 18, 1),
64+
]);
65+
}
66+
67+
test_instance_var_initialized() async {
68+
await assertErrorsInCode('''
69+
class C {
70+
var f = (<dynamic>[])[0];
71+
}
72+
''', [
73+
error(LanguageCode.IMPLICIT_DYNAMIC_FIELD, 16, 20),
74+
]);
75+
}
76+
77+
test_instance_var_initialized_inference() async {
78+
await assertNoErrorsInCode('''
79+
class C {
80+
var f = 0;
81+
}
82+
''');
83+
}
84+
85+
test_instance_var_uninitialized() async {
86+
await assertErrorsInCode('''
87+
class C {
88+
var f;
89+
}
90+
''', [
91+
error(LanguageCode.IMPLICIT_DYNAMIC_FIELD, 16, 1),
92+
]);
93+
}
94+
95+
test_instance_var_uninitialized_multiple() async {
96+
await assertErrorsInCode('''
97+
class C {
98+
var f, g = 42, h;
99+
}
100+
''', [
101+
error(LanguageCode.IMPLICIT_DYNAMIC_FIELD, 16, 1),
102+
error(LanguageCode.IMPLICIT_DYNAMIC_FIELD, 27, 1),
103+
]);
104+
}
105+
106+
test_static_var_initialized() async {
107+
await assertErrorsInCode('''
108+
class C {
109+
static var f = (<dynamic>[])[0];
110+
}
111+
''', [
112+
error(LanguageCode.IMPLICIT_DYNAMIC_FIELD, 23, 20),
113+
]);
114+
}
115+
}
116+
117+
@reflectiveTest
118+
class ImplicitDynamicFieldWithoutNullSafetyTest extends PubPackageResolutionTest
119+
with ImplicitDynamicFieldTestCases, WithoutNullSafetyMixin {}

0 commit comments

Comments
 (0)