Skip to content

Commit 85dec34

Browse files
pqcommit-bot@chromium.org
authored andcommitted
add fix for unnecessary_new lint
See: https://github.com/dart-lang/linter/issues/1374. Change-Id: I945d393b0b46cf87b5400be1bfdb501c31646988 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/95669 Commit-Queue: Phil Quitslund <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 17bf9d1 commit 85dec34

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ class DartFixKind {
253253
static const REMOVE_UNUSED_IMPORT = const FixKind(
254254
'REMOVE_UNUSED_IMPORT', 50, "Remove unused import",
255255
appliedTogetherMessage: "Remove all unused imports in this file");
256+
static const REMOVE_UNNECESSARY_NEW = const FixKind(
257+
'REMOVE_UNNECESSARY_NEW', 50, "Remove unnecessary new keyword");
256258
static const RENAME_TO_CAMEL_CASE =
257259
const FixKind('RENAME_TO_CAMEL_CASE', 50, "Rename to '{0}'");
258260
static const REPLACE_BOOLEAN_WITH_BOOL = const FixKind(

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,9 @@ class FixProcessor {
616616
if (name == LintNames.unnecessary_lambdas) {
617617
await _addFix_replaceWithTearOff();
618618
}
619+
if (name == LintNames.unnecessary_new) {
620+
await _addFix_removeNewKeyword();
621+
}
619622
if (name == LintNames.unnecessary_override) {
620623
await _addFix_removeMethodDeclaration();
621624
}
@@ -2926,6 +2929,18 @@ class FixProcessor {
29262929
}
29272930
}
29282931

2932+
Future<void> _addFix_removeNewKeyword() async {
2933+
final instanceCreationExpression = node;
2934+
if (instanceCreationExpression is InstanceCreationExpression) {
2935+
final newToken = instanceCreationExpression.keyword;
2936+
var changeBuilder = _newDartChangeBuilder();
2937+
await changeBuilder.addFileEdit(file, (DartFileEditBuilder builder) {
2938+
builder.addDeletion(range.startStart(newToken, newToken.next));
2939+
});
2940+
_addFixFromBuilder(changeBuilder, DartFixKind.REMOVE_UNNECESSARY_NEW);
2941+
}
2942+
}
2943+
29292944
Future<void> _addFix_removeParameters_inGetterDeclaration() async {
29302945
// TODO(brianwilkerson) Determine whether this await is necessary.
29312946
await null;
@@ -4311,6 +4326,7 @@ class LintNames {
43114326
'unnecessary_brace_in_string_interp';
43124327
static const String unnecessary_const = 'unnecessary_const';
43134328
static const String unnecessary_lambdas = 'unnecessary_lambdas';
4329+
static const String unnecessary_new = 'unnecessary_new';
43144330
static const String unnecessary_override = 'unnecessary_override';
43154331
static const String unnecessary_this = 'unnecessary_this';
43164332
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (c) 2019, 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/correction/fix_internal.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+
main() {
13+
defineReflectiveSuite(() {
14+
defineReflectiveTests(UnnecessaryNewTest);
15+
});
16+
}
17+
18+
@reflectiveTest
19+
class UnnecessaryNewTest extends FixProcessorLintTest {
20+
@override
21+
FixKind get kind => DartFixKind.REMOVE_UNNECESSARY_NEW;
22+
23+
@override
24+
String get lintCode => LintNames.unnecessary_new;
25+
26+
test_constructor() async {
27+
await resolveTestUnit('''
28+
class A { A(); }
29+
m(){
30+
final a = /*LINT*/new A();
31+
}
32+
''');
33+
await assertHasFix('''
34+
class A { A(); }
35+
m(){
36+
final a = /*LINT*/A();
37+
}
38+
''');
39+
}
40+
}

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
@@ -75,6 +75,7 @@ import 'remove_type_annotation_test.dart' as remove_type_annotation;
7575
import 'remove_type_arguments_test.dart' as remove_type_arguments;
7676
import 'remove_unnecessary_cast_test.dart' as remove_unnecessary_cast;
7777
import 'remove_unnecessary_const_test.dart' as remove_unnecessary_const;
78+
import 'remove_unnecessary_new_test.dart' as remove_unnecessary_new;
7879
import 'remove_unused_catch_clause_test.dart' as remove_unused_catch_clause;
7980
import 'remove_unused_catch_stack_test.dart' as remove_unused_catch_stack;
8081
import 'remove_unused_import_test.dart' as remove_unused_import;
@@ -161,6 +162,7 @@ main() {
161162
remove_type_arguments.main();
162163
remove_unnecessary_cast.main();
163164
remove_unnecessary_const.main();
165+
remove_unnecessary_new.main();
164166
remove_unused_catch_clause.main();
165167
remove_unused_catch_stack.main();
166168
remove_unused_import.main();

0 commit comments

Comments
 (0)