Skip to content

Commit d517e7b

Browse files
author
Dart CI
committed
Version 2.12.0-110.0.dev
Merge commit '923facf2262114994992f01d82fb8287372fb7f5' into 'dev'
2 parents 2cc0275 + 923facf commit d517e7b

25 files changed

+1353
-131
lines changed

pkg/analysis_server/test/src/services/correction/fix/change_to_test.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analysis_server/src/services/correction/fix.dart';
6+
import 'package:analyzer/src/error/codes.dart';
67
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
78
import 'package:test_reflective_loader/test_reflective_loader.dart';
89

@@ -126,7 +127,9 @@ main() {
126127
c.Future v = null;
127128
print(v);
128129
}
129-
''');
130+
''', errorFilter: (error) {
131+
return error.errorCode == CompileTimeErrorCode.UNDEFINED_CLASS;
132+
});
130133
}
131134

132135
Future<void> test_class_with() async {
@@ -177,7 +180,9 @@ main() {
177180
c.main();
178181
}
179182
''');
180-
await assertNoFix();
183+
await assertNoFix(errorFilter: (error) {
184+
return error.errorCode == CompileTimeErrorCode.UNDEFINED_FUNCTION;
185+
});
181186
}
182187

183188
Future<void> test_function_thisLibrary() async {

pkg/analyzer/lib/src/error/imports_verifier.dart

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,6 @@ class ImportsVerifier {
217217
/// element, the import directive can be marked as used (removed from the
218218
/// unusedImports) by looking at the resolved `lib` in `lib.X`, instead of
219219
/// looking at which library the `lib.X` resolves.
220-
///
221-
/// TODO (jwren) Since multiple [ImportDirective]s can share the same
222-
/// [PrefixElement], it is possible to have an unreported unused import in
223-
/// situations where two imports use the same prefix and at least one import
224-
/// directive is used.
225220
final HashMap<PrefixElement, List<ImportDirective>> _prefixElementMap =
226221
HashMap<PrefixElement, List<ImportDirective>>();
227222

@@ -410,54 +405,49 @@ class ImportsVerifier {
410405

411406
/// Remove elements from [_unusedImports] using the given [usedElements].
412407
void removeUsedElements(UsedImportedElements usedElements) {
413-
// Stop if all the imports and shown names are known to be used.
414-
if (_unusedImports.isEmpty && _unusedShownNamesMap.isEmpty) {
415-
return;
416-
}
408+
bool everythingIsKnownToBeUsed() =>
409+
_unusedImports.isEmpty && _unusedShownNamesMap.isEmpty;
410+
417411
// Process import prefixes.
418412
usedElements.prefixMap
419413
.forEach((PrefixElement prefix, List<Element> elements) {
420-
List<ImportDirective> importDirectives = _prefixElementMap[prefix];
421-
if (importDirectives != null) {
422-
int importLength = importDirectives.length;
423-
for (int i = 0; i < importLength; i++) {
424-
ImportDirective importDirective = importDirectives[i];
425-
_unusedImports.remove(importDirective);
426-
int elementLength = elements.length;
427-
for (int j = 0; j < elementLength; j++) {
428-
Element element = elements[j];
414+
if (everythingIsKnownToBeUsed()) {
415+
return;
416+
}
417+
// Find import directives using namespaces.
418+
for (var importDirective in _prefixElementMap[prefix] ?? []) {
419+
Namespace namespace = _computeNamespace(importDirective);
420+
for (var element in elements) {
421+
if (namespace?.getPrefixed(prefix.name, element.name) != null) {
422+
_unusedImports.remove(importDirective);
429423
_removeFromUnusedShownNamesMap(element, importDirective);
430424
}
431425
}
432426
}
433427
});
434428
// Process top-level elements.
435429
for (Element element in usedElements.elements) {
436-
// Stop if all the imports and shown names are known to be used.
437-
if (_unusedImports.isEmpty && _unusedShownNamesMap.isEmpty) {
430+
if (everythingIsKnownToBeUsed()) {
438431
return;
439432
}
440433
// Find import directives using namespaces.
441-
String name = element.name;
442434
for (ImportDirective importDirective in _allImports) {
443435
Namespace namespace = _computeNamespace(importDirective);
444-
if (namespace?.get(name) != null) {
436+
if (namespace?.get(element.name) != null) {
445437
_unusedImports.remove(importDirective);
446438
_removeFromUnusedShownNamesMap(element, importDirective);
447439
}
448440
}
449441
}
450442
// Process extension elements.
451443
for (ExtensionElement extensionElement in usedElements.usedExtensions) {
452-
// Stop if all the imports and shown names are known to be used.
453-
if (_unusedImports.isEmpty && _unusedShownNamesMap.isEmpty) {
444+
if (everythingIsKnownToBeUsed()) {
454445
return;
455446
}
456447
// Find import directives using namespaces.
457-
String name = extensionElement.name;
458448
for (ImportDirective importDirective in _allImports) {
459449
Namespace namespace = _computeNamespace(importDirective);
460-
if (namespace?.get(name) == extensionElement) {
450+
if (namespace?.get(extensionElement.name) == extensionElement) {
461451
_unusedImports.remove(importDirective);
462452
_removeFromUnusedShownNamesMap(extensionElement, importDirective);
463453
}

pkg/analyzer/test/generated/error_suppression_test.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,9 @@ import 'dart:collection' as c;
271271
// ignore: undefined_prefixed_name
272272
f() => c.g;
273273
''',
274-
[],
274+
[
275+
error(HintCode.UNUSED_IMPORT, 7, 17),
276+
],
275277
);
276278
}
277279
}

pkg/analyzer/test/src/dart/resolution/assignment_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,6 +1533,7 @@ main() {
15331533
x = 2;
15341534
}
15351535
''', [
1536+
error(HintCode.UNUSED_IMPORT, 7, 11),
15361537
error(CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, 37, 1),
15371538
]);
15381539

pkg/analyzer/test/src/dart/resolution/import_prefix_test.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ main() {
2424
p; // use
2525
}
2626
''', [
27+
error(HintCode.UNUSED_IMPORT, 7, 12),
2728
error(CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, 38, 1),
2829
]);
2930

@@ -40,6 +41,7 @@ main() {
4041
for (var x in p) {}
4142
}
4243
''', [
44+
error(HintCode.UNUSED_IMPORT, 7, 12),
4345
error(HintCode.UNUSED_LOCAL_VARIABLE, 47, 1),
4446
error(CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, 52, 1),
4547
]);
@@ -64,6 +66,7 @@ main() {
6466
var x = new C(p);
6567
}
6668
''', [
69+
error(HintCode.UNUSED_IMPORT, 7, 12),
6770
error(HintCode.UNUSED_LOCAL_VARIABLE, 66, 1),
6871
error(CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, 76, 1),
6972
]);

pkg/analyzer/test/src/dart/resolution/method_invocation_test.dart

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,7 @@ main() {
755755
math?.loadLibrary();
756756
}
757757
''', [
758+
error(HintCode.UNUSED_IMPORT, 7, 11),
758759
error(CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, 49, 4),
759760
]);
760761

@@ -778,6 +779,7 @@ main() {
778779
foo();
779780
}
780781
''', [
782+
error(HintCode.UNUSED_IMPORT, 7, 11),
781783
error(CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, 39, 3),
782784
]);
783785
_assertInvalidInvocation(
@@ -806,6 +808,7 @@ main() {
806808
math.foo(0);
807809
}
808810
''', [
811+
error(HintCode.UNUSED_IMPORT, 7, 11),
809812
error(CompileTimeErrorCode.UNDEFINED_FUNCTION, 45, 3),
810813
]);
811814
_assertUnresolvedMethodInvocation('foo(0);');
@@ -1309,13 +1312,15 @@ main() {
13091312
}
13101313

13111314
test_hasReceiver_deferredImportPrefix_loadLibrary() async {
1312-
await assertNoErrorsInCode(r'''
1315+
await assertErrorsInCode(r'''
13131316
import 'dart:math' deferred as math;
13141317
13151318
main() {
13161319
math.loadLibrary();
13171320
}
1318-
''');
1321+
''', [
1322+
error(HintCode.UNUSED_IMPORT, 7, 11),
1323+
]);
13191324

13201325
var import = findElement.importFind('dart:math');
13211326

@@ -1337,6 +1342,7 @@ main() {
13371342
math.loadLibrary(1 + 2);
13381343
}
13391344
''', [
1345+
error(HintCode.UNUSED_IMPORT, 7, 11),
13401346
error(CompileTimeErrorCode.EXTRA_POSITIONAL_ARGUMENTS, 65, 7),
13411347
]);
13421348

@@ -1769,6 +1775,7 @@ main() {
17691775
math();
17701776
}
17711777
''', [
1778+
error(HintCode.UNUSED_IMPORT, 7, 11),
17721779
error(CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, 40, 4),
17731780
]);
17741781
assertElement(findNode.simple('math()'), findElement.prefix('math'));
@@ -2332,14 +2339,16 @@ class MethodInvocationResolutionWithNullSafetyTest
23322339
class A {}
23332340
''');
23342341

2335-
await assertNoErrorsInCode(r'''
2342+
await assertErrorsInCode(r'''
23362343
// @dart = 2.7
23372344
import 'a.dart' deferred as a;
23382345
23392346
main() {
23402347
a.loadLibrary();
23412348
}
2342-
''');
2349+
''', [
2350+
error(HintCode.UNUSED_IMPORT, 22, 8),
2351+
]);
23432352

23442353
var import = findElement.importFind('package:test/a.dart');
23452354

pkg/analyzer/test/src/dart/resolution/prefixed_identifier_test.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,14 +278,16 @@ class PrefixedIdentifierResolutionWithNullSafetyTest
278278
class A {}
279279
''');
280280

281-
await assertNoErrorsInCode(r'''
281+
await assertErrorsInCode(r'''
282282
// @dart = 2.7
283283
import 'a.dart' deferred as a;
284284
285285
main() {
286286
a.loadLibrary;
287287
}
288-
''');
288+
''', [
289+
error(HintCode.UNUSED_IMPORT, 22, 8),
290+
]);
289291

290292
var import = findElement.importFind('package:test/a.dart');
291293

pkg/analyzer/test/src/dart/resolution/type_name_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ main() {
197197
new math.A();
198198
}
199199
''', [
200+
error(HintCode.UNUSED_IMPORT, 7, 11),
200201
error(CompileTimeErrorCode.NEW_WITH_NON_TYPE, 49, 1),
201202
]);
202203

pkg/analyzer/test/src/diagnostics/const_with_non_type_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ void f() {
2323
const lib.A();
2424
}
2525
''', [
26+
error(HintCode.UNUSED_IMPORT, 7, 11),
2627
error(CompileTimeErrorCode.CONST_WITH_NON_TYPE, 50, 1),
2728
]);
2829
}

pkg/analyzer/test/src/diagnostics/extends_non_class_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ import 'dart:math' as p;
118118
119119
class C extends p.A {}
120120
''', [
121+
error(HintCode.UNUSED_IMPORT, 7, 11),
121122
error(CompileTimeErrorCode.EXTENDS_NON_CLASS, 42, 3),
122123
]);
123124
}

0 commit comments

Comments
 (0)