Skip to content

Commit b0b0d6a

Browse files
authored
Use toList(growable:false) more (#3151)
1 parent 485a32a commit b0b0d6a

22 files changed

+101
-80
lines changed

lib/src/comment_references/model_comment_reference.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ abstract class ModelCommentReference {
3434
/// and [ResourceProvider] after construction.
3535
class _ModelCommentReferenceImpl implements ModelCommentReference {
3636
void _initAllowCache() {
37-
var referencePieces = parsed.whereType<IdentifierNode>().toList();
37+
final referencePieces =
38+
parsed.whereType<IdentifierNode>().toList(growable: false);
3839
_allowUnnamedConstructor = false;
3940
_allowUnnamedConstructorParameter = false;
4041
if (referencePieces.length >= 2) {
@@ -51,7 +52,7 @@ class _ModelCommentReferenceImpl implements ModelCommentReference {
5152
}
5253
}
5354
}
54-
// e.g. [C.new], which may be the unnamed constructor
55+
// e.g. [C.new], which may be the unnamed constructor.
5556
if (referencePieces.isNotEmpty && referencePieces.last.text == 'new') {
5657
_allowUnnamedConstructor = true;
5758
}

lib/src/dartdoc_options.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,9 @@ class ToolConfiguration {
184184
if (compileArgs is String) {
185185
args = [toolMap[compileArgsTagName].toString()];
186186
} else if (compileArgs is YamlList) {
187-
args =
188-
compileArgs.map<String>((node) => node.toString()).toList();
187+
args = compileArgs
188+
.map<String>((node) => node.toString())
189+
.toList(growable: false);
189190
} else {
190191
throw DartdocOptionError(
191192
'Tool compile arguments must be a list of strings. The tool '
@@ -307,7 +308,7 @@ class _OptionValueWithContext<T> {
307308
return (value as List<String>)
308309
.map((v) => pathContext.canonicalizeWithTilde(v))
309310
.cast<String>()
310-
.toList() as T;
311+
.toList(growable: false) as T;
311312
} else if (value is String) {
312313
return pathContext.canonicalizeWithTilde(value as String) as T;
313314
} else if (value is Map<String, String>) {

lib/src/generator/generator_frontend.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class GeneratorFrontEnd implements Generator {
3333
var categories = indexElements
3434
.whereType<Categorization>()
3535
.where((e) => e.hasCategorization)
36-
.toList();
36+
.toList(growable: false);
3737
_generatorBackend.generateCategoryJson(categories);
3838
_generatorBackend.generateSearchIndex(indexElements);
3939
}

lib/src/generator/generator_utils.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ String generateCategoryJson(Iterable<Categorization> categories, bool pretty) {
3131
final encoder =
3232
pretty ? const JsonEncoder.withIndent(' ') : const JsonEncoder();
3333

34-
return encoder.convert(indexItems.toList());
34+
return encoder.convert(indexItems.toList(growable: false));
3535
}
3636

3737
String removeHtmlTags(String? input) {
@@ -69,7 +69,7 @@ String generateSearchIndexJson(
6969
final encoder =
7070
pretty ? const JsonEncoder.withIndent(' ') : const JsonEncoder();
7171

72-
return encoder.convert(indexItems.toList());
72+
return encoder.convert(indexItems);
7373
}
7474

7575
// Compares two elements, first by fully qualified name, then by kind.

lib/src/markdown_processor.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ class MarkdownDocument extends md.Document {
352352
/// if [processFullText] is `true`.
353353
List<md.Node> parseMarkdownText(String text,
354354
{required bool processFullText}) {
355-
var lines = LineSplitter.split(text).toList();
355+
var lines = LineSplitter.split(text).toList(growable: false);
356356
md.Node? firstNode;
357357
var nodes = <md.Node>[];
358358
// TODO(srawlins): Refactor this. I think with null safety, it is more clear

lib/src/model/canonicalization.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ abstract class Canonicalization implements Locatable, Documentable {
1414
Set<String> get locationPieces;
1515

1616
List<ScoredCandidate> scoreCanonicalCandidates(Iterable<Library> libraries) {
17-
return libraries.map(_scoreElementWithLibrary).toList()..sort();
17+
return libraries.map(_scoreElementWithLibrary).toList(growable: false)
18+
..sort();
1819
}
1920

2021
ScoredCandidate _scoreElementWithLibrary(Library lib) {

lib/src/model/categorization.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ abstract class Categorization implements ModelElement {
4343
return '';
4444
});
4545

46-
_categoryNames = categorySet.toList()..sort();
47-
_subCategoryNames = subCategorySet.toList()..sort();
46+
_categoryNames = categorySet.toList(growable: false)..sort();
47+
_subCategoryNames = subCategorySet.toList(growable: false)..sort();
4848
_image ??= '';
4949
_samples ??= '';
5050
return rawDocs;

lib/src/model/container.dart

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ abstract class Container extends ModelElement
7373
];
7474

7575
late final List<ModelElement> allCanonicalModelElements =
76-
allModelElements.where((e) => e.isCanonical).toList();
76+
allModelElements.where((e) => e.isCanonical).toList(growable: false);
7777

7878
/// All methods, including operators and statics, declared as part of this
7979
/// [Container].
@@ -108,7 +108,7 @@ abstract class Container extends ModelElement
108108
model_utils.filterNonPublic(instanceMethods);
109109

110110
late final List<Method> publicInstanceMethodsSorted =
111-
publicInstanceMethods.toList()..sort();
111+
publicInstanceMethods.toList(growable: false)..sort();
112112

113113
@nonVirtual
114114
late final Iterable<Operator> declaredOperators =
@@ -128,7 +128,7 @@ abstract class Container extends ModelElement
128128
model_utils.filterNonPublic(instanceOperators);
129129

130130
late final List<Operator> publicInstanceOperatorsSorted =
131-
publicInstanceOperators.toList()..sort();
131+
publicInstanceOperators.toList(growable: false)..sort();
132132

133133
/// Fields fully declared in this [Container].
134134
Iterable<Field> get declaredFields;
@@ -147,7 +147,7 @@ abstract class Container extends ModelElement
147147
bool get hasPublicInstanceFields => publicInstanceFields.isNotEmpty;
148148

149149
late final List<Field> publicInstanceFieldsSorted =
150-
publicInstanceFields.toList()..sort(byName);
150+
publicInstanceFields.toList(growable: false)..sort(byName);
151151

152152
Iterable<Field> get constantFields => declaredFields.where((f) => f.isConst);
153153

@@ -157,7 +157,7 @@ abstract class Container extends ModelElement
157157
bool get hasPublicConstantFields => publicConstantFields.isNotEmpty;
158158

159159
late final List<Field> publicConstantFieldsSorted =
160-
publicConstantFields.toList()..sort(byName);
160+
publicConstantFields.toList(growable: false)..sort(byName);
161161

162162
Iterable<Field> get publicEnumValues => [];
163163

@@ -208,7 +208,7 @@ abstract class Container extends ModelElement
208208
bool get hasPublicStaticFields => publicStaticFieldsSorted.isNotEmpty;
209209

210210
late final List<Field> publicStaticFieldsSorted =
211-
model_utils.filterNonPublic(staticFields).toList()..sort();
211+
model_utils.filterNonPublic(staticFields).toList(growable: false)..sort();
212212

213213
Iterable<Field> get staticFields => declaredFields.where((f) => f.isStatic);
214214

@@ -218,17 +218,21 @@ abstract class Container extends ModelElement
218218
bool get hasPublicVariableStaticFields =>
219219
publicVariableStaticFieldsSorted.isNotEmpty;
220220

221-
late final List<Field> publicVariableStaticFieldsSorted =
222-
model_utils.filterNonPublic(variableStaticFields).toList()..sort();
221+
late final List<Field> publicVariableStaticFieldsSorted = model_utils
222+
.filterNonPublic(variableStaticFields)
223+
.toList(growable: false)
224+
..sort();
223225

224226
Iterable<Method> get staticMethods =>
225227
declaredMethods.where((m) => m.isStatic);
226228

227229
bool get hasPublicStaticMethods =>
228230
model_utils.filterNonPublic(publicStaticMethodsSorted).isNotEmpty;
229231

230-
late final List<Method> publicStaticMethodsSorted =
231-
model_utils.filterNonPublic(staticMethods).toList()..sort();
232+
late final List<Method> publicStaticMethodsSorted = model_utils
233+
.filterNonPublic(staticMethods)
234+
.toList(growable: false)
235+
..sort();
232236

233237
/// For subclasses to add items after the main pass but before the
234238
/// parameter-global.

lib/src/model/documentation_comment.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ mixin DocumentationComment
257257
var invocationIndex = 0;
258258
return await _replaceAllMappedAsync(rawDocs, _basicToolPattern,
259259
(basicMatch) async {
260-
var args = _splitUpQuotedArgs(basicMatch[1]!).toList();
260+
final args = _splitUpQuotedArgs(basicMatch[1]!);
261261
// Tool name must come first.
262262
if (args.isEmpty) {
263263
warn(PackageWarning.toolError,
@@ -267,8 +267,8 @@ mixin DocumentationComment
267267
// Count the number of invocations of tools in this dartdoc block,
268268
// so that tools can differentiate different blocks from each other.
269269
invocationIndex++;
270-
return await config.tools.runner.run(args, content: basicMatch[2]!,
271-
toolErrorCallback: (String message) async {
270+
return await config.tools.runner.run(args.toList(),
271+
content: basicMatch[2]!, toolErrorCallback: (String message) async {
272272
warn(PackageWarning.toolError, message: message);
273273
}, environment: _toolsEnvironment(invocationIndex: invocationIndex));
274274
});

lib/src/model/inheriting_container.dart

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ mixin Constructable on InheritingContainer {
2828

2929
@override
3030
late final List<Constructor> publicConstructorsSorted =
31-
model_utils.filterNonPublic(constructors).toList()..sort();
31+
model_utils.filterNonPublic(constructors).toList(growable: false)..sort();
3232

3333
static Iterable<MapEntry<String, CommentReferable>> _constructorGenerator(
3434
Iterable<Constructor> source) sync* {
@@ -171,7 +171,8 @@ mixin TypeImplementing on InheritingContainer {
171171
List<InheritingContainer>? _publicImplementorsSorted;
172172

173173
Iterable<InheritingContainer> get publicImplementorsSorted =>
174-
_publicImplementorsSorted ??= publicImplementors.toList()..sort(byName);
174+
_publicImplementorsSorted ??= publicImplementors.toList(growable: false)
175+
..sort(byName);
175176
}
176177

177178
/// A [Container] that participates in inheritance in Dart.
@@ -354,7 +355,7 @@ abstract class InheritingContainer extends Container
354355
// Elements in the inheritance chain starting from [this.element]
355356
// down to, but not including, [Object].
356357
inheritanceChainElements ??=
357-
inheritanceChain.map((c) => c!.element).toList();
358+
inheritanceChain.map((c) => c!.element).toList(growable: false);
358359
// [packageGraph.specialClasses] is not available yet.
359360
bool isDartCoreObject(ClassElement e) =>
360361
e.name == 'Object' && e.library.name == 'dart.core';
@@ -379,7 +380,7 @@ abstract class InheritingContainer extends Container
379380
}
380381
}
381382

382-
__inheritedElements = combinedMap.values.toList();
383+
__inheritedElements = combinedMap.values.toList(growable: false);
383384
}
384385
return __inheritedElements;
385386
}
@@ -419,7 +420,7 @@ abstract class InheritingContainer extends Container
419420
// Now we only have inherited accessors who aren't associated with
420421
// anything in cls._fields.
421422
for (var fieldName in accessorMap.keys) {
422-
var elements = accessorMap[fieldName]!.toList();
423+
var elements = accessorMap[fieldName]!.toList(growable: false);
423424
var getterElement = elements.firstWhereOrNull((e) => e.isGetter);
424425
var setterElement = elements.firstWhereOrNull((e) => e.isSetter);
425426
fields.add(_createSingleField(
@@ -519,7 +520,7 @@ abstract class InheritingContainer extends Container
519520
_typeParameters ??= element.typeParameters.map((f) {
520521
var lib = modelBuilder.fromElement(f.enclosingElement3!.library!);
521522
return modelBuilder.from(f, lib as Library) as TypeParameter;
522-
}).toList();
523+
}).toList(growable: false);
523524
return _typeParameters!;
524525
}
525526

lib/src/model/method.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Method extends ModelElement
3434
void _calcTypeParameters() {
3535
typeParameters = element.typeParameters.map((f) {
3636
return modelBuilder.from(f, library) as TypeParameter;
37-
}).toList();
37+
}).toList(growable: false);
3838
}
3939

4040
@override

lib/src/model/model_element.dart

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ abstract class ModelElement extends Canonicalization
570570
topLevelElement = topLevelElement.enclosingElement3!;
571571
}
572572

573-
var candidateLibraries = thisAndExported
573+
final candidateLibraries = thisAndExported
574574
.where((l) =>
575575
l.isPublic && l.package.documentedWhere != DocumentLocation.missing)
576576
.where((l) {
@@ -580,7 +580,7 @@ abstract class ModelElement extends Canonicalization
580580
lookup = lookup.variable;
581581
}
582582
return topLevelElement == lookup;
583-
}).toList();
583+
}).toList(growable: true);
584584

585585
// Avoid claiming canonicalization for elements outside of this element's
586586
// defining package.
@@ -609,21 +609,22 @@ abstract class ModelElement extends Canonicalization
609609
// canonical. Still warn if the heuristic isn't that confident.
610610
var scoredCandidates =
611611
warnable.scoreCanonicalCandidates(candidateLibraries);
612-
candidateLibraries = scoredCandidates.map((s) => s.library).toList();
612+
final librariesByScore = scoredCandidates.map((s) => s.library).toList();
613613
var secondHighestScore =
614614
scoredCandidates[scoredCandidates.length - 2].score;
615615
var highestScore = scoredCandidates.last.score;
616616
var confidence = highestScore - secondHighestScore;
617+
final canonicalLibrary = librariesByScore.last;
617618

618619
if (confidence < config.ambiguousReexportScorerMinConfidence) {
619-
var libraryNames = candidateLibraries.map((l) => l.name);
620-
var message = '$libraryNames -> ${candidateLibraries.last.name} '
620+
var libraryNames = librariesByScore.map((l) => l.name);
621+
var message = '$libraryNames -> ${canonicalLibrary.name} '
621622
'(confidence ${confidence.toStringAsPrecision(4)})';
622623
warnable.warn(PackageWarning.ambiguousReexport,
623624
message: message, extendedDebug: scoredCandidates.map((s) => '$s'));
624625
}
625626

626-
return candidateLibraries.last;
627+
return canonicalLibrary;
627628
}
628629

629630
@override
@@ -737,7 +738,7 @@ abstract class ModelElement extends Canonicalization
737738
var setterDeprecated = pie.setter?.metadata.any((a) => a.isDeprecated);
738739

739740
var deprecatedValues =
740-
[getterDeprecated, setterDeprecated].whereNotNull().toList();
741+
[getterDeprecated, setterDeprecated].whereNotNull();
741742

742743
// At least one of these should be non-null. Otherwise things are weird
743744
assert(deprecatedValues.isNotEmpty);
@@ -854,7 +855,7 @@ abstract class ModelElement extends Canonicalization
854855
}
855856
}
856857
}
857-
return recursedParameters.toList();
858+
return recursedParameters.toList(growable: false);
858859
}();
859860

860861
@override

lib/src/model/package.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ class Package extends LibraryContainer
329329
late final List<Category> categories = [
330330
defaultCategory,
331331
...nameToCategory.values,
332-
].where((c) => c.name.isNotEmpty).toList()
332+
].where((c) => c.name.isNotEmpty).toList(growable: false)
333333
..sort();
334334

335335
Iterable<Category> get categoriesWithPublicLibraries =>
@@ -346,7 +346,8 @@ class Package extends LibraryContainer
346346
/// ordered by name.
347347
Iterable<Category> get documentedCategoriesSorted {
348348
if (config.categoryOrder.isNotEmpty) {
349-
final documentedCategories = this.documentedCategories.toList();
349+
final documentedCategories =
350+
this.documentedCategories.toList(growable: false);
350351
return documentedCategories
351352
..sort((a, b) {
352353
var aIndex = config.categoryOrder.indexOf(a.name);

0 commit comments

Comments
 (0)