Add a fix for avoid_returning_null_for_void

Change-Id: Ia38d0115157a914fc8f19d6e026e34c4752207de
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/201102
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Brian Wilkerson 2021-05-23 15:13:00 +00:00 committed by commit-bot@chromium.org
parent cc02fd844b
commit 93670dacc1
6 changed files with 118 additions and 0 deletions

View file

@ -0,0 +1,31 @@
// Copyright (c) 2021, 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_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 RemoveReturnedValue extends CorrectionProducer {
@override
FixKind get fixKind => DartFixKind.REMOVE_RETURNED_VALUE;
@override
FixKind get multiFixKind => DartFixKind.REMOVE_RETURNED_VALUE_MULTI;
@override
Future<void> compute(ChangeBuilder builder) async {
final node = this.node;
if (node is ReturnStatement) {
await builder.addDartFileEdit(file, (builder) {
builder.addDeletion(range.endStart(node.returnKeyword, node.semicolon));
});
}
}
/// Return an instance of this class. Used as a tear-off in `FixProcessor`.
static RemoveReturnedValue newInstance() => RemoveReturnedValue();
}

View file

@ -621,6 +621,12 @@ class DartFixKind {
'dart.fix.remove.questionMark.multi',
DartFixKindPriority.IN_FILE,
'Remove unnecessary question marks in file');
static const REMOVE_RETURNED_VALUE = FixKind('dart.fix.remove.returnedValue',
DartFixKindPriority.DEFAULT, 'Remove invalid returned value');
static const REMOVE_RETURNED_VALUE_MULTI = FixKind(
'dart.fix.remove.returnedValue.multi',
DartFixKindPriority.IN_FILE,
'Remove invalid returned values in file');
static const REMOVE_THIS_EXPRESSION = FixKind(
'dart.fix.remove.thisExpression',
DartFixKindPriority.DEFAULT,

View file

@ -109,6 +109,7 @@ import 'package:analysis_server/src/services/correction/dart/remove_operator.dar
import 'package:analysis_server/src/services/correction/dart/remove_parameters_in_getter_declaration.dart';
import 'package:analysis_server/src/services/correction/dart/remove_parentheses_in_getter_invocation.dart';
import 'package:analysis_server/src/services/correction/dart/remove_question_mark.dart';
import 'package:analysis_server/src/services/correction/dart/remove_returned_value.dart';
import 'package:analysis_server/src/services/correction/dart/remove_this_expression.dart';
import 'package:analysis_server/src/services/correction/dart/remove_type_annotation.dart';
import 'package:analysis_server/src/services/correction/dart/remove_type_arguments.dart';
@ -458,6 +459,15 @@ class FixProcessor extends BaseProcessor {
],
)
],
LintNames.avoid_returning_null_for_void: [
FixInfo(
canBeAppliedToFile: true,
canBeBulkApplied: true,
generators: [
RemoveReturnedValue.newInstance,
],
)
],
LintNames.avoid_single_cascade_in_expression_statements: [
FixInfo(
canBeAppliedToFile: true,

View file

@ -25,6 +25,8 @@ class LintNames {
'avoid_return_types_on_setters';
static const String avoid_returning_null_for_future =
'avoid_returning_null_for_future';
static const String avoid_returning_null_for_void =
'avoid_returning_null_for_void';
static const String avoid_single_cascade_in_expression_statements =
'avoid_single_cascade_in_expression_statements';
static const String avoid_types_as_parameter_names =

View file

@ -0,0 +1,67 @@
// Copyright (c) 2021, 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 'bulk/bulk_fix_processor.dart';
import 'fix_processor.dart';
void main() {
defineReflectiveSuite(() {
defineReflectiveTests(RemoveReturnedValueBulkTest);
defineReflectiveTests(RemoveReturnedValueTest);
});
}
@reflectiveTest
class RemoveReturnedValueBulkTest extends BulkFixProcessorTest {
@override
String get lintCode => LintNames.avoid_returning_null_for_void;
Future<void> test_simple() async {
await resolveTestCode('''
void f(bool b) {
if (b) {
return null;
} else {
return null;
}
}
''');
await assertHasFix('''
void f(bool b) {
if (b) {
return;
} else {
return;
}
}
''');
}
}
@reflectiveTest
class RemoveReturnedValueTest extends FixProcessorLintTest {
@override
FixKind get kind => DartFixKind.REMOVE_RETURNED_VALUE;
@override
String get lintCode => LintNames.avoid_returning_null_for_void;
Future<void> test_simple() async {
await resolveTestCode('''
void f() {
return null;
}
''');
await assertHasFix('''
void f() {
return;
}
''');
}
}

View file

@ -126,6 +126,7 @@ import 'remove_parameters_in_getter_declaration_test.dart'
import 'remove_parentheses_in_getter_invocation_test.dart'
as remove_parentheses_in_getter_invocation;
import 'remove_question_mark_test.dart' as remove_question_mark;
import 'remove_returned_value_test.dart' as remove_returned_value;
import 'remove_this_expression_test.dart' as remove_this_expression;
import 'remove_type_annotation_test.dart' as remove_type_annotation;
import 'remove_type_arguments_test.dart' as remove_type_arguments;
@ -290,6 +291,7 @@ void main() {
remove_parameters_in_getter_declaration.main();
remove_parentheses_in_getter_invocation.main();
remove_question_mark.main();
remove_returned_value.main();
remove_this_expression.main();
remove_type_annotation.main();
remove_type_arguments.main();