Skip to content

Commit 364dd89

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Fix unnecessary_new in analyzer.
R=brianwilkerson@google.com Change-Id: I70f80baa3da4b14c0a52c1f28da16a9ad26d7dda Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/127424 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
1 parent 723baf7 commit 364dd89

File tree

399 files changed

+4359
-4686
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

399 files changed

+4359
-4686
lines changed

pkg/analyzer/analysis_options.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ analyzer:
2121
# is mechanical, via `dartfmt --fix-optional-const`, but not worth the
2222
# churn today.
2323
unnecessary_const: ignore
24-
# TODO(srawlins): At the time of writing, 3200 violations in lib/. The fix
25-
# is mechanical, via `dartfmt --fix-optional-new`, but not worth the churn
26-
# today.
27-
unnecessary_new: ignore
2824

2925
linter:
3026
rules:

pkg/analyzer/lib/analyzer.dart

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ CompilationUnit parseCompilationUnit(String contents,
5454
FeatureSet featureSet}) {
5555
// TODO(paulberry): make featureSet a required parameter
5656
featureSet ??= FeatureSet.fromEnableFlags([]);
57-
Source source = new StringSource(contents, name);
57+
Source source = StringSource(contents, name);
5858
return _parseSource(contents, source, featureSet,
5959
suppressErrors: suppressErrors, parseFunctionBodies: parseFunctionBodies);
6060
}
@@ -79,17 +79,17 @@ CompilationUnit parseDartFile(String path,
7979
FeatureSet featureSet}) {
8080
// TODO(paulberry): Make featureSet a required parameter
8181
featureSet ??= FeatureSet.fromEnableFlags([]);
82-
String contents = new File(path).readAsStringSync();
83-
var sourceFactory = new SourceFactory(
84-
[new ResourceUriResolver(PhysicalResourceProvider.INSTANCE)]);
82+
String contents = File(path).readAsStringSync();
83+
var sourceFactory =
84+
SourceFactory([ResourceUriResolver(PhysicalResourceProvider.INSTANCE)]);
8585

8686
var absolutePath = pathos.absolute(path);
8787
var source = sourceFactory.forUri(pathos.toUri(absolutePath).toString());
8888
if (source == null) {
89-
throw new ArgumentError("Can't get source for path $path");
89+
throw ArgumentError("Can't get source for path $path");
9090
}
9191
if (!source.exists()) {
92-
throw new ArgumentError("Source $source doesn't exist");
92+
throw ArgumentError("Source $source doesn't exist");
9393
}
9494

9595
return _parseSource(contents, source, featureSet,
@@ -118,15 +118,15 @@ CompilationUnit parseDirectives(String contents,
118118
{String name, bool suppressErrors = false, FeatureSet featureSet}) {
119119
// TODO(paulberry): make featureSet a required parameter.
120120
featureSet ??= FeatureSet.fromEnableFlags([]);
121-
var source = new StringSource(contents, name);
122-
var errorCollector = new _ErrorCollector();
123-
var reader = new CharSequenceReader(contents);
124-
var scanner = new Scanner(source, reader, errorCollector)
121+
var source = StringSource(contents, name);
122+
var errorCollector = _ErrorCollector();
123+
var reader = CharSequenceReader(contents);
124+
var scanner = Scanner(source, reader, errorCollector)
125125
..configureFeatures(featureSet);
126126
var token = scanner.tokenize();
127-
var parser = new Parser(source, errorCollector, featureSet: featureSet);
127+
var parser = Parser(source, errorCollector, featureSet: featureSet);
128128
var unit = parser.parseDirectives(token);
129-
unit.lineInfo = new LineInfo(scanner.lineStarts);
129+
unit.lineInfo = LineInfo(scanner.lineStarts);
130130

131131
if (errorCollector.hasErrors && !suppressErrors) throw errorCollector.group;
132132

@@ -142,15 +142,15 @@ String stringLiteralToString(StringLiteral literal) {
142142
CompilationUnit _parseSource(
143143
String contents, Source source, FeatureSet featureSet,
144144
{bool suppressErrors = false, bool parseFunctionBodies = true}) {
145-
var reader = new CharSequenceReader(contents);
146-
var errorCollector = new _ErrorCollector();
147-
var scanner = new Scanner(source, reader, errorCollector)
145+
var reader = CharSequenceReader(contents);
146+
var errorCollector = _ErrorCollector();
147+
var scanner = Scanner(source, reader, errorCollector)
148148
..configureFeatures(featureSet);
149149
var token = scanner.tokenize();
150-
var parser = new Parser(source, errorCollector, featureSet: featureSet)
150+
var parser = Parser(source, errorCollector, featureSet: featureSet)
151151
..parseFunctionBodies = parseFunctionBodies;
152152
var unit = parser.parseCompilationUnit(token)
153-
..lineInfo = new LineInfo(scanner.lineStarts);
153+
..lineInfo = LineInfo(scanner.lineStarts);
154154

155155
if (errorCollector.hasErrors && !suppressErrors) throw errorCollector.group;
156156

@@ -165,7 +165,7 @@ class _ErrorCollector extends AnalysisErrorListener {
165165

166166
/// The group of errors collected.
167167
AnalyzerErrorGroup get group =>
168-
new AnalyzerErrorGroup.fromAnalysisErrors(_errors);
168+
AnalyzerErrorGroup.fromAnalysisErrors(_errors);
169169

170170
/// Whether any errors where collected.
171171
bool get hasErrors => _errors.isNotEmpty;

pkg/analyzer/lib/dart/analysis/declared_variables.dart

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ class DeclaredVariables {
5252
DartObject getBool(TypeProvider typeProvider, String name) {
5353
String value = _declaredVariables[name];
5454
if (value == null) {
55-
return new DartObjectImpl(typeProvider.boolType, BoolState.UNKNOWN_VALUE);
55+
return DartObjectImpl(typeProvider.boolType, BoolState.UNKNOWN_VALUE);
5656
}
5757
if (value == "true") {
58-
return new DartObjectImpl(typeProvider.boolType, BoolState.TRUE_STATE);
58+
return DartObjectImpl(typeProvider.boolType, BoolState.TRUE_STATE);
5959
} else if (value == "false") {
60-
return new DartObjectImpl(typeProvider.boolType, BoolState.FALSE_STATE);
60+
return DartObjectImpl(typeProvider.boolType, BoolState.FALSE_STATE);
6161
}
62-
return new DartObjectImpl(typeProvider.nullType, NullState.NULL_STATE);
62+
return DartObjectImpl(typeProvider.nullType, NullState.NULL_STATE);
6363
}
6464

6565
/// Return the value of the variable with the given [name] interpreted as an
@@ -69,15 +69,15 @@ class DeclaredVariables {
6969
DartObject getInt(TypeProvider typeProvider, String name) {
7070
String value = _declaredVariables[name];
7171
if (value == null) {
72-
return new DartObjectImpl(typeProvider.intType, IntState.UNKNOWN_VALUE);
72+
return DartObjectImpl(typeProvider.intType, IntState.UNKNOWN_VALUE);
7373
}
7474
int bigInteger;
7575
try {
7676
bigInteger = int.parse(value);
7777
} on FormatException {
78-
return new DartObjectImpl(typeProvider.nullType, NullState.NULL_STATE);
78+
return DartObjectImpl(typeProvider.nullType, NullState.NULL_STATE);
7979
}
80-
return new DartObjectImpl(typeProvider.intType, new IntState(bigInteger));
80+
return DartObjectImpl(typeProvider.intType, IntState(bigInteger));
8181
}
8282

8383
/// Return the value of the variable with the given [name] interpreted as a
@@ -89,9 +89,8 @@ class DeclaredVariables {
8989
DartObject getString(TypeProvider typeProvider, String name) {
9090
String value = _declaredVariables[name];
9191
if (value == null) {
92-
return new DartObjectImpl(
93-
typeProvider.stringType, StringState.UNKNOWN_VALUE);
92+
return DartObjectImpl(typeProvider.stringType, StringState.UNKNOWN_VALUE);
9493
}
95-
return new DartObjectImpl(typeProvider.stringType, new StringState(value));
94+
return DartObjectImpl(typeProvider.stringType, StringState(value));
9695
}
9796
}

pkg/analyzer/lib/dart/analysis/utilities.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ ParseStringResult parseString(
124124
ParseStringResult result =
125125
ParseStringResultImpl(content, unit, errorCollector.errors);
126126
if (throwIfDiagnostics && result.errors.isNotEmpty) {
127-
throw new ArgumentError('Content produced diagnostics when parsed');
127+
throw ArgumentError('Content produced diagnostics when parsed');
128128
}
129129
return result;
130130
}
@@ -149,13 +149,13 @@ Future<ResolvedUnitResult> resolveFile(
149149
/// If a [resourceProvider] is given, it will be used to access the file system.
150150
AnalysisContext _createAnalysisContext(
151151
{@required String path, ResourceProvider resourceProvider}) {
152-
AnalysisContextCollection collection = new AnalysisContextCollection(
152+
AnalysisContextCollection collection = AnalysisContextCollection(
153153
includedPaths: <String>[path],
154154
resourceProvider: resourceProvider ?? PhysicalResourceProvider.INSTANCE,
155155
);
156156
List<AnalysisContext> contexts = collection.contexts;
157157
if (contexts.length != 1) {
158-
throw new ArgumentError('path must be an absolute path to a single file');
158+
throw ArgumentError('path must be an absolute path to a single file');
159159
}
160160
return contexts[0];
161161
}

pkg/analyzer/lib/dart/ast/ast.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3069,8 +3069,8 @@ abstract class ImportDirective implements NamespaceDirective {
30693069
// hides and shows
30703070
//
30713071
NodeList<Combinator> combinators1 = import1.combinators;
3072-
List<String> allHides1 = new List<String>();
3073-
List<String> allShows1 = new List<String>();
3072+
List<String> allHides1 = List<String>();
3073+
List<String> allShows1 = List<String>();
30743074
int length1 = combinators1.length;
30753075
for (int i = 0; i < length1; i++) {
30763076
Combinator combinator = combinators1[i];
@@ -3092,8 +3092,8 @@ abstract class ImportDirective implements NamespaceDirective {
30923092
}
30933093
}
30943094
NodeList<Combinator> combinators2 = import2.combinators;
3095-
List<String> allHides2 = new List<String>();
3096-
List<String> allShows2 = new List<String>();
3095+
List<String> allHides2 = List<String>();
3096+
List<String> allShows2 = List<String>();
30973097
int length2 = combinators2.length;
30983098
for (int i = 0; i < length2; i++) {
30993099
Combinator combinator = combinators2[i];

pkg/analyzer/lib/dart/ast/standard_ast_factory.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ import 'package:analyzer/dart/ast/ast_factory.dart';
66
import 'package:analyzer/src/dart/ast/ast_factory.dart';
77

88
/// Gets an instance of [AstFactory] based on the standard AST implementation.
9-
final AstFactory astFactory = new AstFactoryImpl();
9+
final AstFactory astFactory = AstFactoryImpl();

pkg/analyzer/lib/dart/ast/visitor.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ import 'package:analyzer/dart/ast/ast.dart';
4949
class BreadthFirstVisitor<R> extends GeneralizingAstVisitor<R> {
5050
/// A queue holding the nodes that have not yet been visited in the order in
5151
/// which they ought to be visited.
52-
Queue<AstNode> _queue = new Queue<AstNode>();
52+
Queue<AstNode> _queue = Queue<AstNode>();
5353

5454
/// A visitor, used to visit the children of the current node, that will add
5555
/// the nodes it visits to the [_queue].
5656
_BreadthFirstChildVisitor _childVisitor;
5757

5858
/// Initialize a newly created visitor.
5959
BreadthFirstVisitor() {
60-
_childVisitor = new _BreadthFirstChildVisitor(this);
60+
_childVisitor = _BreadthFirstChildVisitor(this);
6161
}
6262

6363
/// Visit all nodes in the tree starting at the given [root] node, in
@@ -2096,7 +2096,7 @@ class ThrowingAstVisitor<R> implements AstVisitor<R> {
20962096
R visitYieldStatement(YieldStatement node) => _throw(node);
20972097

20982098
R _throw(AstNode node) {
2099-
throw new Exception('Missing implementation of visit${node.runtimeType}');
2099+
throw Exception('Missing implementation of visit${node.runtimeType}');
21002100
}
21012101
}
21022102

@@ -2113,7 +2113,7 @@ class TimedAstVisitor<T> implements AstVisitor<T> {
21132113
/// Initialize a newly created visitor to time calls to the given base
21142114
/// visitor's visits.
21152115
TimedAstVisitor(this._baseVisitor, [Stopwatch watch])
2116-
: stopwatch = watch ?? new Stopwatch();
2116+
: stopwatch = watch ?? Stopwatch();
21172117

21182118
@override
21192119
T visitAdjacentStrings(AdjacentStrings node) {

pkg/analyzer/lib/dart/element/visitor.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,6 @@ class ThrowingElementVisitor<R> implements ElementVisitor<R> {
469469
R visitTypeParameterElement(TypeParameterElement element) => _throw(element);
470470

471471
R _throw(Element element) {
472-
throw new Exception(
473-
'Missing implementation of visit${element.runtimeType}');
472+
throw Exception('Missing implementation of visit${element.runtimeType}');
474473
}
475474
}

pkg/analyzer/lib/error/error.dart

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ HashMap<String, ErrorCode> _uniqueNameToCodeMap;
844844
*/
845845
ErrorCode errorCodeByUniqueName(String uniqueName) {
846846
if (_uniqueNameToCodeMap == null) {
847-
_uniqueNameToCodeMap = new HashMap<String, ErrorCode>();
847+
_uniqueNameToCodeMap = HashMap<String, ErrorCode>();
848848
for (ErrorCode errorCode in errorCodeValues) {
849849
_uniqueNameToCodeMap[errorCode.uniqueName] = errorCode;
850850
}
@@ -932,7 +932,7 @@ class AnalysisError implements Diagnostic {
932932
if (correctionTemplate != null) {
933933
this._correction = formatList(correctionTemplate, arguments);
934934
}
935-
_problemMessage = new DiagnosticMessageImpl(
935+
_problemMessage = DiagnosticMessageImpl(
936936
filePath: source?.fullName,
937937
length: length,
938938
message: message,
@@ -946,7 +946,7 @@ class AnalysisError implements Diagnostic {
946946
AnalysisError.forValues(this.source, int offset, int length, this.errorCode,
947947
String message, this._correction,
948948
{List<DiagnosticMessage> contextMessages = const []}) {
949-
_problemMessage = new DiagnosticMessageImpl(
949+
_problemMessage = DiagnosticMessageImpl(
950950
filePath: source?.fullName,
951951
length: length,
952952
message: message,
@@ -1005,8 +1005,7 @@ class AnalysisError implements Diagnostic {
10051005
case ErrorSeverity.INFO:
10061006
return Severity.info;
10071007
default:
1008-
throw new StateError(
1009-
'Invalid error severity: ${errorCode.errorSeverity}');
1008+
throw StateError('Invalid error severity: ${errorCode.errorSeverity}');
10101009
}
10111010
}
10121011

@@ -1039,7 +1038,7 @@ class AnalysisError implements Diagnostic {
10391038

10401039
@override
10411040
String toString() {
1042-
StringBuffer buffer = new StringBuffer();
1041+
StringBuffer buffer = StringBuffer();
10431042
buffer.write((source != null) ? source.fullName : "<unknown source>");
10441043
buffer.write("(");
10451044
buffer.write(offset);
@@ -1056,7 +1055,7 @@ class AnalysisError implements Diagnostic {
10561055
* a single list of errors.
10571056
*/
10581057
static List<AnalysisError> mergeLists(List<List<AnalysisError>> errorLists) {
1059-
Set<AnalysisError> errors = new HashSet<AnalysisError>();
1058+
Set<AnalysisError> errors = HashSet<AnalysisError>();
10601059
for (List<AnalysisError> errorList in errorLists) {
10611060
errors.addAll(errorList);
10621061
}

pkg/analyzer/lib/error/listener.dart

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ abstract class AnalysisErrorListener {
2222
/**
2323
* An error listener that ignores errors that are reported to it.
2424
*/
25-
static final AnalysisErrorListener NULL_LISTENER = new _NullErrorListener();
25+
static final AnalysisErrorListener NULL_LISTENER = _NullErrorListener();
2626

2727
/**
2828
* This method is invoked when an [error] has been found by the analysis
@@ -79,9 +79,9 @@ class ErrorReporter {
7979
*/
8080
ErrorReporter(this._errorListener, this._defaultSource) {
8181
if (_errorListener == null) {
82-
throw new ArgumentError("An error listener must be provided");
82+
throw ArgumentError("An error listener must be provided");
8383
} else if (_defaultSource == null) {
84-
throw new ArgumentError("A default source must be provided");
84+
throw ArgumentError("A default source must be provided");
8585
}
8686
this._source = _defaultSource;
8787
}
@@ -139,8 +139,8 @@ class ErrorReporter {
139139
*/
140140
void reportErrorForOffset(ErrorCode errorCode, int offset, int length,
141141
[List<Object> arguments]) {
142-
_errorListener.onError(
143-
new AnalysisError(_source, offset, length, errorCode, arguments));
142+
_errorListener
143+
.onError(AnalysisError(_source, offset, length, errorCode, arguments));
144144
}
145145

146146
/**
@@ -167,7 +167,7 @@ class ErrorReporter {
167167
*/
168168
void reportErrorMessage(
169169
ErrorCode errorCode, int offset, int length, Message message) {
170-
_errorListener.onError(new AnalysisError.forValues(
170+
_errorListener.onError(AnalysisError.forValues(
171171
_source, offset, length, errorCode, message.message, message.tip));
172172
}
173173

@@ -200,7 +200,7 @@ class ErrorReporter {
200200
if (type is FunctionType) {
201201
String name = type.name;
202202
if (name != null && name.isNotEmpty) {
203-
StringBuffer buffer = new StringBuffer();
203+
StringBuffer buffer = StringBuffer();
204204
buffer.write(name);
205205
(type as TypeImpl).appendTo(buffer, withNullability: false);
206206
return buffer.toString();
@@ -216,7 +216,7 @@ class ErrorReporter {
216216
String displayName = computeDisplayName(argument);
217217
List<_TypeToConvert> types =
218218
typeGroups.putIfAbsent(displayName, () => <_TypeToConvert>[]);
219-
types.add(new _TypeToConvert(i, argument, displayName));
219+
types.add(_TypeToConvert(i, argument, displayName));
220220
}
221221
}
222222
for (List<_TypeToConvert> typeGroup in typeGroups.values) {
@@ -230,7 +230,7 @@ class ErrorReporter {
230230
for (_TypeToConvert typeToConvert in typeGroup) {
231231
for (Element element in typeToConvert.allElements()) {
232232
Set<Element> elements = nameToElementMap.putIfAbsent(
233-
element.name, () => new Set<Element>());
233+
element.name, () => Set<Element>());
234234
elements.add(element);
235235
}
236236
}
@@ -243,7 +243,7 @@ class ErrorReporter {
243243
String name = element.name;
244244
if (nameToElementMap[name].length > 1) {
245245
if (buffer == null) {
246-
buffer = new StringBuffer();
246+
buffer = StringBuffer();
247247
buffer.write('where ');
248248
} else {
249249
buffer.write(', ');
@@ -293,7 +293,7 @@ class RecordingErrorListener implements AnalysisErrorListener {
293293

294294
@override
295295
void onError(AnalysisError error) {
296-
_errors ??= new HashSet<AnalysisError>();
296+
_errors ??= HashSet<AnalysisError>();
297297
_errors.add(error);
298298
}
299299
}
@@ -323,7 +323,7 @@ class _TypeToConvert {
323323

324324
List<Element> allElements() {
325325
if (_allElements == null) {
326-
Set<Element> elements = new Set<Element>();
326+
Set<Element> elements = Set<Element>();
327327

328328
void addElementsFrom(DartType type) {
329329
if (type is FunctionType) {

0 commit comments

Comments
 (0)