Skip to content

Commit 0d130c3

Browse files
pqCommit Bot
authored andcommitted
fix for unnecessary_null_aware_assignments
Fixes: https://github.com/dart-lang/lints/issues/66 Change-Id: Ib14902c76910d550fbf69161a257d59cd268be9c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/236701 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent 2ce7aa3 commit 0d130c3

File tree

7 files changed

+159
-1
lines changed

7 files changed

+159
-1
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright (c) 2022, 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/source/source_range.dart';
9+
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
10+
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
11+
import 'package:analyzer_plugin/utilities/range_factory.dart';
12+
13+
class RemoveAssignment extends CorrectionProducer {
14+
@override
15+
bool get canBeAppliedInBulk => true;
16+
17+
@override
18+
bool get canBeAppliedToFile => true;
19+
20+
@override
21+
FixKind get fixKind => DartFixKind.REMOVE_ASSIGNMENT;
22+
23+
@override
24+
FixKind get multiFixKind => DartFixKind.REMOVE_ASSIGNMENT_MULTI;
25+
26+
@override
27+
Future<void> compute(ChangeBuilder builder) async {
28+
var expression = node;
29+
if (expression is! AssignmentExpression) {
30+
return;
31+
}
32+
33+
SourceRange sourceRange;
34+
var parent = expression.parent;
35+
while (parent is ParenthesizedExpression) {
36+
parent = parent.parent;
37+
}
38+
if (parent is ExpressionStatement) {
39+
sourceRange = utils.getLinesRange(range.node(parent));
40+
} else {
41+
sourceRange = range.endEnd(
42+
expression.leftHandSide.endToken, expression.rightHandSide.endToken);
43+
}
44+
45+
await builder.addDartFileEdit(file, (builder) {
46+
builder.addDeletion(sourceRange);
47+
});
48+
}
49+
50+
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
51+
static RemoveAssignment newInstance() => RemoveAssignment();
52+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1783,7 +1783,7 @@ LintCode.unnecessary_late:
17831783
LintCode.unnecessary_new:
17841784
status: hasFix
17851785
LintCode.unnecessary_null_aware_assignments:
1786-
status: needsFix
1786+
status: hasFix
17871787
LintCode.unnecessary_null_checks:
17881788
status: needsEvaluation
17891789
LintCode.unnecessary_null_in_if_null_operators:

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,16 @@ class DartFixKind {
829829
DartFixKindPriority.IN_FILE,
830830
'Remove arguments in file',
831831
);
832+
static const REMOVE_ASSIGNMENT = FixKind(
833+
'dart.fix.remove.assignment',
834+
DartFixKindPriority.DEFAULT,
835+
'Remove assignment',
836+
);
837+
static const REMOVE_ASSIGNMENT_MULTI = FixKind(
838+
'dart.fix.remove.assignment.multi',
839+
DartFixKindPriority.IN_FILE,
840+
'Remove unnecessary assignments everywhere in file',
841+
);
832842
static const REMOVE_AWAIT = FixKind(
833843
'dart.fix.remove.await',
834844
DartFixKindPriority.DEFAULT,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ import 'package:analysis_server/src/services/correction/dart/qualify_reference.d
104104
import 'package:analysis_server/src/services/correction/dart/remove_abstract.dart';
105105
import 'package:analysis_server/src/services/correction/dart/remove_annotation.dart';
106106
import 'package:analysis_server/src/services/correction/dart/remove_argument.dart';
107+
import 'package:analysis_server/src/services/correction/dart/remove_assignment.dart';
107108
import 'package:analysis_server/src/services/correction/dart/remove_await.dart';
108109
import 'package:analysis_server/src/services/correction/dart/remove_comparison.dart';
109110
import 'package:analysis_server/src/services/correction/dart/remove_const.dart';
@@ -628,6 +629,9 @@ class FixProcessor extends BaseProcessor {
628629
LintNames.unnecessary_new: [
629630
RemoveUnnecessaryNew.newInstance,
630631
],
632+
LintNames.unnecessary_null_aware_assignments: [
633+
RemoveAssignment.newInstance,
634+
],
631635
LintNames.unnecessary_null_in_if_null_operators: [
632636
RemoveIfNullOperator.newInstance,
633637
],

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ class LintNames {
136136
static const String unnecessary_lambdas = 'unnecessary_lambdas';
137137
static const String unnecessary_late = 'unnecessary_late';
138138
static const String unnecessary_new = 'unnecessary_new';
139+
static const String unnecessary_null_aware_assignments =
140+
'unnecessary_null_aware_assignments';
139141
static const String unnecessary_null_in_if_null_operators =
140142
'unnecessary_null_in_if_null_operators';
141143
static const String unnecessary_nullable_for_final_variable_declarations =
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright (c) 2022, 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 'fix_processor.dart';
11+
12+
void main() {
13+
defineReflectiveSuite(() {
14+
defineReflectiveTests(RemoveAssignmentBulkTest);
15+
defineReflectiveTests(RemoveAssignmentTest);
16+
});
17+
}
18+
19+
@reflectiveTest
20+
class RemoveAssignmentBulkTest extends BulkFixProcessorTest {
21+
@override
22+
String get lintCode => LintNames.unnecessary_null_aware_assignments;
23+
24+
Future<void> test_singleFile() async {
25+
await resolveTestCode('''
26+
void f() {
27+
var x;
28+
var y;
29+
x ??= null;
30+
y ??= null;
31+
}
32+
''');
33+
await assertHasFix('''
34+
void f() {
35+
var x;
36+
var y;
37+
}
38+
''');
39+
}
40+
}
41+
42+
@reflectiveTest
43+
class RemoveAssignmentTest extends FixProcessorLintTest {
44+
@override
45+
FixKind get kind => DartFixKind.REMOVE_ASSIGNMENT;
46+
47+
@override
48+
String get lintCode => LintNames.unnecessary_null_aware_assignments;
49+
50+
Future<void> test_assignment() async {
51+
await resolveTestCode('''
52+
void f() {
53+
var x;
54+
x ??= null;
55+
}
56+
''');
57+
await assertHasFix('''
58+
void f() {
59+
var x;
60+
}
61+
''');
62+
}
63+
64+
Future<void> test_assignment_compound() async {
65+
await resolveTestCode('''
66+
void f(x, y) {
67+
y = x ??= null;
68+
}
69+
''');
70+
await assertHasFix('''
71+
void f(x, y) {
72+
y = x;
73+
}
74+
''');
75+
}
76+
77+
Future<void> test_assignment_parenthesized() async {
78+
await resolveTestCode('''
79+
void f(int? x) {
80+
(x ??= null);
81+
}
82+
''');
83+
await assertHasFix('''
84+
void f(int? x) {
85+
}
86+
''');
87+
}
88+
}

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
@@ -129,6 +129,7 @@ import 'qualify_reference_test.dart' as qualify_reference;
129129
import 'remove_abstract_test.dart' as remove_abstract;
130130
import 'remove_annotation_test.dart' as remove_annotation;
131131
import 'remove_argument_test.dart' as remove_argument;
132+
import 'remove_assignment_test.dart' as remove_assignment;
132133
import 'remove_await_test.dart' as remove_await;
133134
import 'remove_comparison_test.dart' as remove_comparison;
134135
import 'remove_const_test.dart' as remove_const;
@@ -337,6 +338,7 @@ void main() {
337338
remove_abstract.main();
338339
remove_annotation.main();
339340
remove_argument.main();
341+
remove_assignment.main();
340342
remove_await.main();
341343
remove_comparison.main();
342344
remove_const.main();

0 commit comments

Comments
 (0)