Skip to content

Commit 75617a1

Browse files
pqCommit Queue
authored and
Commit Queue
committed
[wildcards] quick fixes for DEAD_CODE_LATE_WILDCARD_VARIABLE_INITIALIZER
Fixes: #56221 Change-Id: Ie599d570bf6a6f13987a4175f955eb522ae20a10 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/375784 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent 1395cbb commit 75617a1

File tree

6 files changed

+65
-8
lines changed

6 files changed

+65
-8
lines changed

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

+14-3
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,25 @@ class RemoveInitializer extends ResolvedCorrectionProducer {
1313
@override
1414
final CorrectionApplicability applicability;
1515

16+
/// If true, remove the `late` keyword.
17+
final bool _removeLate;
18+
1619
/// Initialize a newly created instance that can't apply bulk and in-file
1720
/// fixes.
1821
RemoveInitializer({required super.context})
19-
: applicability = CorrectionApplicability.singleLocation;
22+
: applicability = CorrectionApplicability.singleLocation,
23+
_removeLate = true;
2024

2125
/// Initialize a newly created instance that can apply bulk and in-file fixes.
2226
RemoveInitializer.bulkFixable({required super.context})
23-
: applicability = CorrectionApplicability.automatically;
27+
: applicability = CorrectionApplicability.automatically,
28+
_removeLate = true;
29+
30+
/// Initialize a newly created instance that can't apply bulk and in-file
31+
/// fixes and will not remove the `late` keyword if present.
32+
RemoveInitializer.notLate({required super.context})
33+
: applicability = CorrectionApplicability.singleLocation,
34+
_removeLate = false;
2435

2536
@override
2637
FixKind get fixKind => DartFixKind.REMOVE_INITIALIZER;
@@ -53,7 +64,7 @@ class RemoveInitializer extends ResolvedCorrectionProducer {
5364
);
5465
});
5566
// Delete the `late` keyword if present.
56-
if (variable.isLate) {
67+
if (_removeLate && variable.isLate) {
5768
var parent = node.parent;
5869
if (parent != null) {
5970
await builder.addDartFileEdit(file, (builder) {

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

+11
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ class RemoveLate extends ResolvedCorrectionProducer {
5555
}
5656
}
5757
}
58+
} else {
59+
var grandParent = node.parent?.parent;
60+
if (grandParent is VariableDeclarationList) {
61+
var lateKeyword = grandParent.lateKeyword;
62+
if (lateKeyword != null) {
63+
return _LateKeywordLocation(
64+
lateKeyword: lateKeyword,
65+
nextToken: lateKeyword.next!,
66+
);
67+
}
68+
}
5869
}
5970

6071
return null;

pkg/analysis_server/lib/src/services/correction/error_fix_status.yaml

+3-5
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@
4545
#
4646
# Stats:
4747
# - 42 "needsEvaluation"
48-
# - 307 "needsFix"
49-
# - 441 "hasFix"
48+
# - 306 "needsFix"
49+
# - 442 "hasFix"
5050
# - 517 "noFix"
5151

5252
AnalysisOptionsErrorCode.INCLUDED_FILE_PARSE_ERROR:
@@ -3390,9 +3390,7 @@ WarningCode.DEAD_CODE:
33903390
WarningCode.DEAD_CODE_CATCH_FOLLOWING_CATCH:
33913391
status: hasFix
33923392
WarningCode.DEAD_CODE_LATE_WILDCARD_VARIABLE_INITIALIZER:
3393-
status: needsFix
3394-
notes: |-
3395-
Remove the initializer or remove the late keyword.
3393+
status: hasFix
33963394
WarningCode.DEAD_CODE_ON_CATCH_SUBTYPE:
33973395
status: hasFix
33983396
WarningCode.DEPRECATED_EXPORT_USE:

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

+4
Original file line numberDiff line numberDiff line change
@@ -1607,6 +1607,10 @@ final _builtInNonLintProducers = <ErrorCode, List<ProducerGenerator>>{
16071607
// a place where it can be reached (when possible).
16081608
RemoveDeadCode.new,
16091609
],
1610+
WarningCode.DEAD_CODE_LATE_WILDCARD_VARIABLE_INITIALIZER: [
1611+
RemoveInitializer.notLate,
1612+
RemoveLate.new,
1613+
],
16101614
WarningCode.DEAD_CODE_ON_CATCH_SUBTYPE: [
16111615
// TODO(brianwilkerson): Add a fix to move the unreachable catch clause to
16121616
// a place where it can be reached (when possible).

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

+20
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,29 @@ void main() {
1313
defineReflectiveSuite(() {
1414
defineReflectiveTests(RemoveInitializerBulkTest);
1515
defineReflectiveTests(RemoveInitializerTest);
16+
defineReflectiveTests(RemoveDeadWildcardInitializerTest);
1617
});
1718
}
1819

20+
@reflectiveTest
21+
class RemoveDeadWildcardInitializerTest extends FixProcessorTest {
22+
@override
23+
FixKind get kind => DartFixKind.REMOVE_INITIALIZER;
24+
25+
Future<void> test_deadLateWildcardVariableInitializer() async {
26+
await resolveTestCode('''
27+
f() {
28+
late var _ = 0;
29+
}
30+
''');
31+
await assertHasFix('''
32+
f() {
33+
late var _;
34+
}
35+
''');
36+
}
37+
}
38+
1939
@reflectiveTest
2040
class RemoveInitializerBulkTest extends BulkFixProcessorTest {
2141
@override

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

+13
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@ class RemoveLateTest extends FixProcessorTest {
3838
@override
3939
FixKind get kind => DartFixKind.REMOVE_LATE;
4040

41+
Future<void> test_deadLateWildcardVariableInitializer() async {
42+
await resolveTestCode('''
43+
f() {
44+
late var _ = 0;
45+
}
46+
''');
47+
await assertHasFix('''
48+
f() {
49+
var _ = 0;
50+
}
51+
''');
52+
}
53+
4154
Future<void> test_it() async {
4255
await resolveTestCode('''
4356
void f(Object? x) {

0 commit comments

Comments
 (0)