Skip to content

Commit

Permalink
add fix for unnecessary_const lints
Browse files Browse the repository at this point in the history
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 <pquitslund@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
  • Loading branch information
pq authored and commit-bot@chromium.org committed Mar 6, 2019
1 parent 9645b19 commit 5441ff6
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/analysis_server/lib/src/services/correction/fix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -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(
Expand Down
16 changes: 16 additions & 0 deletions pkg/analysis_server/lib/src/services/correction/fix_internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -2690,6 +2693,18 @@ class FixProcessor {
}
}

Future<void> _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<void> _addFix_removeDeadCode() async {
AstNode coveringNode = this.coveredNode;
if (coveringNode is Expression) {
Expand Down Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
''');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 5441ff6

Please sign in to comment.