From 5441ff672b92223623215dc2b892b4578c2fc561 Mon Sep 17 00:00:00 2001 From: pq Date: Wed, 6 Mar 2019 15:28:59 +0000 Subject: [PATCH] add fix for unnecessary_const lints See: https://github.com/dart-lang/linter/issues/1374. Change-Id: Ieeddf25718ddde77a9e52cb93cbcf34ca2c1f81f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/95542 Commit-Queue: Phil Quitslund Reviewed-by: Brian Wilkerson --- .../lib/src/services/correction/fix.dart | 3 ++ .../src/services/correction/fix_internal.dart | 16 ++++++++ .../fix/remove_unnecessary_const_test.dart | 40 +++++++++++++++++++ .../src/services/correction/fix/test_all.dart | 2 + 4 files changed, 61 insertions(+) create mode 100644 pkg/analysis_server/test/src/services/correction/fix/remove_unnecessary_const_test.dart diff --git a/pkg/analysis_server/lib/src/services/correction/fix.dart b/pkg/analysis_server/lib/src/services/correction/fix.dart index 1ef51d608b62..baecf8aaa084 100644 --- a/pkg/analysis_server/lib/src/services/correction/fix.dart +++ b/pkg/analysis_server/lib/src/services/correction/fix.dart @@ -79,6 +79,7 @@ bool hasFix(ErrorCode errorCode) => errorCode.name == LintNames.prefer_conditional_assignment || errorCode.name == LintNames.prefer_const_declarations || errorCode.name == LintNames.unnecessary_brace_in_string_interp || + errorCode.name == LintNames.unnecessary_const || errorCode.name == LintNames.unnecessary_lambdas || errorCode.name == LintNames.unnecessary_this)); @@ -243,6 +244,8 @@ class DartFixKind { static const REMOVE_UNNECESSARY_CAST = const FixKind( 'REMOVE_UNNECESSARY_CAST', 50, "Remove unnecessary cast", appliedTogetherMessage: "Remove all unnecessary casts in file"); + static const REMOVE_UNNECESSARY_CONST = const FixKind( + 'REMOVE_UNNECESSARY_CONST', 50, "Remove unnecessary const keyword"); static const REMOVE_UNUSED_CATCH_CLAUSE = const FixKind( 'REMOVE_UNUSED_CATCH_CLAUSE', 50, "Remove unused 'catch' clause"); static const REMOVE_UNUSED_CATCH_STACK = const FixKind( diff --git a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart index e042cd014c75..8fe7da1b88de 100644 --- a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart +++ b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart @@ -610,6 +610,9 @@ class FixProcessor { if (name == LintNames.unnecessary_brace_in_string_interp) { await _addFix_removeInterpolationBraces(); } + if (name == LintNames.unnecessary_const) { + await _addFix_removeConstKeyword(); + } if (name == LintNames.unnecessary_lambdas) { await _addFix_replaceWithTearOff(); } @@ -2690,6 +2693,18 @@ class FixProcessor { } } + Future _addFix_removeConstKeyword() async { + final instanceCreationExpression = node; + if (instanceCreationExpression is InstanceCreationExpression) { + final constToken = instanceCreationExpression.keyword; + var changeBuilder = _newDartChangeBuilder(); + await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) { + builder.addDeletion(range.startStart(constToken, constToken.next)); + }); + _addFixFromBuilder(changeBuilder, DartFixKind.REMOVE_UNNECESSARY_CONST); + } + } + Future _addFix_removeDeadCode() async { AstNode coveringNode = this.coveredNode; if (coveringNode is Expression) { @@ -4294,6 +4309,7 @@ class LintNames { static const String type_init_formals = 'type_init_formals'; static const String unnecessary_brace_in_string_interp = 'unnecessary_brace_in_string_interp'; + static const String unnecessary_const = 'unnecessary_const'; static const String unnecessary_lambdas = 'unnecessary_lambdas'; static const String unnecessary_override = 'unnecessary_override'; static const String unnecessary_this = 'unnecessary_this'; diff --git a/pkg/analysis_server/test/src/services/correction/fix/remove_unnecessary_const_test.dart b/pkg/analysis_server/test/src/services/correction/fix/remove_unnecessary_const_test.dart new file mode 100644 index 000000000000..440b948701b6 --- /dev/null +++ b/pkg/analysis_server/test/src/services/correction/fix/remove_unnecessary_const_test.dart @@ -0,0 +1,40 @@ +// Copyright (c) 2019, 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/correction/fix_internal.dart'; +import 'package:analyzer_plugin/utilities/fixes/fixes.dart'; +import 'package:test_reflective_loader/test_reflective_loader.dart'; + +import 'fix_processor.dart'; + +main() { + defineReflectiveSuite(() { + defineReflectiveTests(UnnecessaryConstTest); + }); +} + +@reflectiveTest +class UnnecessaryConstTest extends FixProcessorLintTest { + @override + FixKind get kind => DartFixKind.REMOVE_UNNECESSARY_CONST; + + @override + String get lintCode => LintNames.unnecessary_const; + + test_constConstructor() async { + await resolveTestUnit(''' +class A { const A(); } +m(){ + const a = /*LINT*/const A(); +} +'''); + await assertHasFix(''' +class A { const A(); } +m(){ + const a = /*LINT*/A(); +} +'''); + } +} diff --git a/pkg/analysis_server/test/src/services/correction/fix/test_all.dart b/pkg/analysis_server/test/src/services/correction/fix/test_all.dart index 6a259e9a8f6e..40400abc0cef 100644 --- a/pkg/analysis_server/test/src/services/correction/fix/test_all.dart +++ b/pkg/analysis_server/test/src/services/correction/fix/test_all.dart @@ -74,6 +74,7 @@ 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; import 'remove_unnecessary_cast_test.dart' as remove_unnecessary_cast; +import 'remove_unnecessary_const_test.dart' as remove_unnecessary_const; import 'remove_unused_catch_clause_test.dart' as remove_unused_catch_clause; import 'remove_unused_catch_stack_test.dart' as remove_unused_catch_stack; import 'remove_unused_import_test.dart' as remove_unused_import; @@ -159,6 +160,7 @@ main() { remove_type_annotation.main(); remove_type_arguments.main(); remove_unnecessary_cast.main(); + remove_unnecessary_const.main(); remove_unused_catch_clause.main(); remove_unused_catch_stack.main(); remove_unused_import.main();