Skip to content

Commit

Permalink
fix for unnecessary_null_aware_assignments
Browse files Browse the repository at this point in the history
Fixes: dart-lang/lints#66

Change-Id: Ib14902c76910d550fbf69161a257d59cd268be9c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/236701
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Phil Quitslund <pquitslund@google.com>
  • Loading branch information
pq authored and Commit Bot committed Mar 14, 2022
1 parent 2ce7aa3 commit 0d130c3
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// 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.

import 'package:analysis_server/src/services/correction/dart/abstract_producer.dart';
import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/source/source_range.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:analyzer_plugin/utilities/range_factory.dart';

class RemoveAssignment extends CorrectionProducer {
@override
bool get canBeAppliedInBulk => true;

@override
bool get canBeAppliedToFile => true;

@override
FixKind get fixKind => DartFixKind.REMOVE_ASSIGNMENT;

@override
FixKind get multiFixKind => DartFixKind.REMOVE_ASSIGNMENT_MULTI;

@override
Future<void> compute(ChangeBuilder builder) async {
var expression = node;
if (expression is! AssignmentExpression) {
return;
}

SourceRange sourceRange;
var parent = expression.parent;
while (parent is ParenthesizedExpression) {
parent = parent.parent;
}
if (parent is ExpressionStatement) {
sourceRange = utils.getLinesRange(range.node(parent));
} else {
sourceRange = range.endEnd(
expression.leftHandSide.endToken, expression.rightHandSide.endToken);
}

await builder.addDartFileEdit(file, (builder) {
builder.addDeletion(sourceRange);
});
}

/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
static RemoveAssignment newInstance() => RemoveAssignment();
}
Original file line number Diff line number Diff line change
Expand Up @@ -1783,7 +1783,7 @@ LintCode.unnecessary_late:
LintCode.unnecessary_new:
status: hasFix
LintCode.unnecessary_null_aware_assignments:
status: needsFix
status: hasFix
LintCode.unnecessary_null_checks:
status: needsEvaluation
LintCode.unnecessary_null_in_if_null_operators:
Expand Down
10 changes: 10 additions & 0 deletions pkg/analysis_server/lib/src/services/correction/fix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,16 @@ class DartFixKind {
DartFixKindPriority.IN_FILE,
'Remove arguments in file',
);
static const REMOVE_ASSIGNMENT = FixKind(
'dart.fix.remove.assignment',
DartFixKindPriority.DEFAULT,
'Remove assignment',
);
static const REMOVE_ASSIGNMENT_MULTI = FixKind(
'dart.fix.remove.assignment.multi',
DartFixKindPriority.IN_FILE,
'Remove unnecessary assignments everywhere in file',
);
static const REMOVE_AWAIT = FixKind(
'dart.fix.remove.await',
DartFixKindPriority.DEFAULT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ import 'package:analysis_server/src/services/correction/dart/qualify_reference.d
import 'package:analysis_server/src/services/correction/dart/remove_abstract.dart';
import 'package:analysis_server/src/services/correction/dart/remove_annotation.dart';
import 'package:analysis_server/src/services/correction/dart/remove_argument.dart';
import 'package:analysis_server/src/services/correction/dart/remove_assignment.dart';
import 'package:analysis_server/src/services/correction/dart/remove_await.dart';
import 'package:analysis_server/src/services/correction/dart/remove_comparison.dart';
import 'package:analysis_server/src/services/correction/dart/remove_const.dart';
Expand Down Expand Up @@ -628,6 +629,9 @@ class FixProcessor extends BaseProcessor {
LintNames.unnecessary_new: [
RemoveUnnecessaryNew.newInstance,
],
LintNames.unnecessary_null_aware_assignments: [
RemoveAssignment.newInstance,
],
LintNames.unnecessary_null_in_if_null_operators: [
RemoveIfNullOperator.newInstance,
],
Expand Down
2 changes: 2 additions & 0 deletions pkg/analysis_server/lib/src/services/linter/lint_names.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ class LintNames {
static const String unnecessary_lambdas = 'unnecessary_lambdas';
static const String unnecessary_late = 'unnecessary_late';
static const String unnecessary_new = 'unnecessary_new';
static const String unnecessary_null_aware_assignments =
'unnecessary_null_aware_assignments';
static const String unnecessary_null_in_if_null_operators =
'unnecessary_null_in_if_null_operators';
static const String unnecessary_nullable_for_final_variable_declarations =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// 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.

import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analysis_server/src/services/linter/lint_names.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';

import 'fix_processor.dart';

void main() {
defineReflectiveSuite(() {
defineReflectiveTests(RemoveAssignmentBulkTest);
defineReflectiveTests(RemoveAssignmentTest);
});
}

@reflectiveTest
class RemoveAssignmentBulkTest extends BulkFixProcessorTest {
@override
String get lintCode => LintNames.unnecessary_null_aware_assignments;

Future<void> test_singleFile() async {
await resolveTestCode('''
void f() {
var x;
var y;
x ??= null;
y ??= null;
}
''');
await assertHasFix('''
void f() {
var x;
var y;
}
''');
}
}

@reflectiveTest
class RemoveAssignmentTest extends FixProcessorLintTest {
@override
FixKind get kind => DartFixKind.REMOVE_ASSIGNMENT;

@override
String get lintCode => LintNames.unnecessary_null_aware_assignments;

Future<void> test_assignment() async {
await resolveTestCode('''
void f() {
var x;
x ??= null;
}
''');
await assertHasFix('''
void f() {
var x;
}
''');
}

Future<void> test_assignment_compound() async {
await resolveTestCode('''
void f(x, y) {
y = x ??= null;
}
''');
await assertHasFix('''
void f(x, y) {
y = x;
}
''');
}

Future<void> test_assignment_parenthesized() async {
await resolveTestCode('''
void f(int? x) {
(x ??= null);
}
''');
await assertHasFix('''
void f(int? x) {
}
''');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ import 'qualify_reference_test.dart' as qualify_reference;
import 'remove_abstract_test.dart' as remove_abstract;
import 'remove_annotation_test.dart' as remove_annotation;
import 'remove_argument_test.dart' as remove_argument;
import 'remove_assignment_test.dart' as remove_assignment;
import 'remove_await_test.dart' as remove_await;
import 'remove_comparison_test.dart' as remove_comparison;
import 'remove_const_test.dart' as remove_const;
Expand Down Expand Up @@ -337,6 +338,7 @@ void main() {
remove_abstract.main();
remove_annotation.main();
remove_argument.main();
remove_assignment.main();
remove_await.main();
remove_comparison.main();
remove_const.main();
Expand Down

0 comments on commit 0d130c3

Please sign in to comment.