Skip to content

Commit

Permalink
[wildcards] quick fixes for `DEAD_CODE_LATE_WILDCARD_VARIABLE_INITIAL…
Browse files Browse the repository at this point in the history
…IZER`

Fixes: #56221

Change-Id: Ie599d570bf6a6f13987a4175f955eb522ae20a10
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/375784
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Phil Quitslund <pquitslund@google.com>
  • Loading branch information
pq authored and Commit Queue committed Jul 16, 2024
1 parent 1395cbb commit 75617a1
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,25 @@ class RemoveInitializer extends ResolvedCorrectionProducer {
@override
final CorrectionApplicability applicability;

/// If true, remove the `late` keyword.
final bool _removeLate;

/// Initialize a newly created instance that can't apply bulk and in-file
/// fixes.
RemoveInitializer({required super.context})
: applicability = CorrectionApplicability.singleLocation;
: applicability = CorrectionApplicability.singleLocation,
_removeLate = true;

/// Initialize a newly created instance that can apply bulk and in-file fixes.
RemoveInitializer.bulkFixable({required super.context})
: applicability = CorrectionApplicability.automatically;
: applicability = CorrectionApplicability.automatically,
_removeLate = true;

/// Initialize a newly created instance that can't apply bulk and in-file
/// fixes and will not remove the `late` keyword if present.
RemoveInitializer.notLate({required super.context})
: applicability = CorrectionApplicability.singleLocation,
_removeLate = false;

@override
FixKind get fixKind => DartFixKind.REMOVE_INITIALIZER;
Expand Down Expand Up @@ -53,7 +64,7 @@ class RemoveInitializer extends ResolvedCorrectionProducer {
);
});
// Delete the `late` keyword if present.
if (variable.isLate) {
if (_removeLate && variable.isLate) {
var parent = node.parent;
if (parent != null) {
await builder.addDartFileEdit(file, (builder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ class RemoveLate extends ResolvedCorrectionProducer {
}
}
}
} else {
var grandParent = node.parent?.parent;
if (grandParent is VariableDeclarationList) {
var lateKeyword = grandParent.lateKeyword;
if (lateKeyword != null) {
return _LateKeywordLocation(
lateKeyword: lateKeyword,
nextToken: lateKeyword.next!,
);
}
}
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
#
# Stats:
# - 42 "needsEvaluation"
# - 307 "needsFix"
# - 441 "hasFix"
# - 306 "needsFix"
# - 442 "hasFix"
# - 517 "noFix"

AnalysisOptionsErrorCode.INCLUDED_FILE_PARSE_ERROR:
Expand Down Expand Up @@ -3390,9 +3390,7 @@ WarningCode.DEAD_CODE:
WarningCode.DEAD_CODE_CATCH_FOLLOWING_CATCH:
status: hasFix
WarningCode.DEAD_CODE_LATE_WILDCARD_VARIABLE_INITIALIZER:
status: needsFix
notes: |-
Remove the initializer or remove the late keyword.
status: hasFix
WarningCode.DEAD_CODE_ON_CATCH_SUBTYPE:
status: hasFix
WarningCode.DEPRECATED_EXPORT_USE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1607,6 +1607,10 @@ final _builtInNonLintProducers = <ErrorCode, List<ProducerGenerator>>{
// a place where it can be reached (when possible).
RemoveDeadCode.new,
],
WarningCode.DEAD_CODE_LATE_WILDCARD_VARIABLE_INITIALIZER: [
RemoveInitializer.notLate,
RemoveLate.new,
],
WarningCode.DEAD_CODE_ON_CATCH_SUBTYPE: [
// TODO(brianwilkerson): Add a fix to move the unreachable catch clause to
// a place where it can be reached (when possible).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,29 @@ void main() {
defineReflectiveSuite(() {
defineReflectiveTests(RemoveInitializerBulkTest);
defineReflectiveTests(RemoveInitializerTest);
defineReflectiveTests(RemoveDeadWildcardInitializerTest);
});
}

@reflectiveTest
class RemoveDeadWildcardInitializerTest extends FixProcessorTest {
@override
FixKind get kind => DartFixKind.REMOVE_INITIALIZER;

Future<void> test_deadLateWildcardVariableInitializer() async {
await resolveTestCode('''
f() {
late var _ = 0;
}
''');
await assertHasFix('''
f() {
late var _;
}
''');
}
}

@reflectiveTest
class RemoveInitializerBulkTest extends BulkFixProcessorTest {
@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ class RemoveLateTest extends FixProcessorTest {
@override
FixKind get kind => DartFixKind.REMOVE_LATE;

Future<void> test_deadLateWildcardVariableInitializer() async {
await resolveTestCode('''
f() {
late var _ = 0;
}
''');
await assertHasFix('''
f() {
var _ = 0;
}
''');
}

Future<void> test_it() async {
await resolveTestCode('''
void f(Object? x) {
Expand Down

0 comments on commit 75617a1

Please sign in to comment.