Skip to content

Commit 56cf168

Browse files
srawlinscommit-bot@chromium.org
authored andcommitted
Analyzer: Don't report on prefixed import with illegal references
Bug: TODO from #38784 Change-Id: I4ecdaf333c2613eb5fa0c57e4a8f8be56c35bb21 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/175184 Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent f04757f commit 56cf168

16 files changed

+10
-40
lines changed

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
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';
76
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
87
import 'package:test_reflective_loader/test_reflective_loader.dart';
98

@@ -127,9 +126,7 @@ main() {
127126
c.Future v = null;
128127
print(v);
129128
}
130-
''', errorFilter: (error) {
131-
return error.errorCode == CompileTimeErrorCode.UNDEFINED_CLASS;
132-
});
129+
''');
133130
}
134131

135132
Future<void> test_class_with() async {
@@ -180,9 +177,7 @@ main() {
180177
c.main();
181178
}
182179
''');
183-
await assertNoFix(errorFilter: (error) {
184-
return error.errorCode == CompileTimeErrorCode.UNDEFINED_FUNCTION;
185-
});
180+
await assertNoFix();
186181
}
187182

188183
Future<void> test_function_thisLibrary() async {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,13 @@ class ImportsVerifier {
417417
// Find import directives using namespaces.
418418
for (var importDirective in _prefixElementMap[prefix] ?? []) {
419419
Namespace namespace = _computeNamespace(importDirective);
420+
if (elements.isEmpty) {
421+
// [prefix] and [elements] were added to [usedElements.prefixMap] but
422+
// [elements] is empty, so the prefix was referenced incorrectly.
423+
// Another diagnostic about the prefix reference is reported, and we
424+
// shouldn't confuse by also reporting an unused prefix.
425+
_unusedImports.remove(importDirective);
426+
}
420427
for (var element in elements) {
421428
if (namespace?.getPrefixed(prefix.name, element.name) != null) {
422429
_unusedImports.remove(importDirective);

pkg/analyzer/test/generated/error_suppression_test.dart

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

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

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

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

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

@@ -41,7 +40,6 @@ main() {
4140
for (var x in p) {}
4241
}
4342
''', [
44-
error(HintCode.UNUSED_IMPORT, 7, 12),
4543
error(HintCode.UNUSED_LOCAL_VARIABLE, 47, 1),
4644
error(CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, 52, 1),
4745
]);
@@ -66,7 +64,6 @@ main() {
6664
var x = new C(p);
6765
}
6866
''', [
69-
error(HintCode.UNUSED_IMPORT, 7, 12),
7067
error(HintCode.UNUSED_LOCAL_VARIABLE, 66, 1),
7168
error(CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, 76, 1),
7269
]);

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,6 @@ main() {
779779
foo();
780780
}
781781
''', [
782-
error(HintCode.UNUSED_IMPORT, 7, 11),
783782
error(CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, 39, 3),
784783
]);
785784
_assertInvalidInvocation(
@@ -808,7 +807,6 @@ main() {
808807
math.foo(0);
809808
}
810809
''', [
811-
error(HintCode.UNUSED_IMPORT, 7, 11),
812810
error(CompileTimeErrorCode.UNDEFINED_FUNCTION, 45, 3),
813811
]);
814812
_assertUnresolvedMethodInvocation('foo(0);');
@@ -1789,7 +1787,6 @@ main() {
17891787
math();
17901788
}
17911789
''', [
1792-
error(HintCode.UNUSED_IMPORT, 7, 11),
17931790
error(CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, 40, 4),
17941791
]);
17951792
assertElement(findNode.simple('math()'), findElement.prefix('math'));

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ main() {
198198
new math.A();
199199
}
200200
''', [
201-
error(HintCode.UNUSED_IMPORT, 7, 11),
202201
error(CompileTimeErrorCode.NEW_WITH_NON_TYPE, 49, 1),
203202
]);
204203

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

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

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

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

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,6 @@ f() {
260260
p?.x;
261261
}
262262
''', [
263-
error(HintCode.UNUSED_IMPORT, 7, 8),
264263
error(CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, 31, 1),
265264
]);
266265
}
@@ -472,7 +471,6 @@ f() {
472471
p?.x = 0;
473472
}
474473
''', [
475-
error(HintCode.UNUSED_IMPORT, 7, 8),
476474
error(CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, 31, 1),
477475
]);
478476
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ import 'dart:math' as p;
158158
159159
class C with p.M {}
160160
''', [
161-
error(HintCode.UNUSED_IMPORT, 7, 11),
162161
error(CompileTimeErrorCode.MIXIN_OF_NON_CLASS, 39, 3),
163162
]);
164163
}

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class C {
2727
}
2828
}
2929
''', [
30-
error(HintCode.UNUSED_IMPORT, 7, 10),
3130
error(CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, 46, 1),
3231
]);
3332
}
@@ -42,7 +41,6 @@ f() {
4241
p += 1;
4342
}
4443
''', [
45-
error(HintCode.UNUSED_IMPORT, 7, 10),
4644
error(CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, 32, 1),
4745
]);
4846
}
@@ -59,7 +57,6 @@ class C {
5957
}
6058
}
6159
''', [
62-
error(HintCode.UNUSED_IMPORT, 7, 10),
6360
error(CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, 46, 1),
6461
]);
6562
}
@@ -93,7 +90,6 @@ f() {
9390
p = 1;
9491
}
9592
''', [
96-
error(HintCode.UNUSED_IMPORT, 7, 10),
9793
error(CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, 32, 1),
9894
]);
9995
}
@@ -108,7 +104,6 @@ f() {
108104
p += 1;
109105
}
110106
''', [
111-
error(HintCode.UNUSED_IMPORT, 7, 10),
112107
error(CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, 32, 1),
113108
]);
114109
}
@@ -154,7 +149,6 @@ f() {
154149
return p?.x;
155150
}
156151
''', [
157-
error(HintCode.UNUSED_IMPORT, 7, 10),
158152
error(CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, 39, 1),
159153
]);
160154
}
@@ -169,7 +163,6 @@ f() {
169163
return p?.loadLibrary;
170164
}
171165
''', [
172-
error(HintCode.UNUSED_IMPORT, 7, 10),
173166
error(CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, 48, 1),
174167
]);
175168
}
@@ -185,7 +178,6 @@ f() {
185178
p?.x = null;
186179
}
187180
''', [
188-
error(HintCode.UNUSED_IMPORT, 7, 10),
189181
error(CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, 32, 1),
190182
]);
191183
}
@@ -200,7 +192,6 @@ f() {
200192
p?.loadLibrary = null;
201193
}
202194
''', [
203-
error(HintCode.UNUSED_IMPORT, 7, 10),
204195
error(CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, 41, 1),
205196
]);
206197
}
@@ -215,7 +206,6 @@ f() {
215206
return p;
216207
}
217208
''', [
218-
error(HintCode.UNUSED_IMPORT, 7, 10),
219209
error(CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT, 39, 1),
220210
]);
221211
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ import 'dart:math' as p;
5252
main() {
5353
}
5454
''', [
55-
error(HintCode.UNUSED_IMPORT, 7, 11),
5655
error(CompileTimeErrorCode.UNDEFINED_ANNOTATION, 25, 13),
5756
]);
5857
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ import 'dart:math' as p;
140140
141141
p.A a;
142142
''', [
143-
error(HintCode.UNUSED_IMPORT, 7, 11),
144143
error(CompileTimeErrorCode.UNDEFINED_CLASS, 26, 3),
145144
]);
146145
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class UndefinedPrefixedNameTest extends PubPackageResolutionTest {
2121
import 'lib.dart' as p;
2222
f() => p.c;
2323
''', [
24-
error(HintCode.UNUSED_IMPORT, 7, 10),
2524
error(CompileTimeErrorCode.UNDEFINED_PREFIXED_NAME, 33, 1),
2625
]);
2726
}
@@ -34,7 +33,6 @@ f() {
3433
p.c = 0;
3534
}
3635
''', [
37-
error(HintCode.UNUSED_IMPORT, 7, 10),
3836
error(CompileTimeErrorCode.UNDEFINED_PREFIXED_NAME, 34, 1),
3937
]);
4038
}

pkg/analyzer/test/verify_diagnostics_test.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ class DocumentationValidator {
4949
// Need a way to make auxiliary files that (a) are not included in the
5050
// generated docs or (b) can be made persistent for fixes.
5151
'CompileTimeErrorCode.PART_OF_NON_PART',
52-
// Need to avoid reporting an unused import with a prefix, when the prefix
53-
// is only referenced in an invalid way.
54-
'CompileTimeErrorCode.PREFIX_IDENTIFIER_NOT_FOLLOWED_BY_DOT',
5552
// Produces the diagnostic HintCode.UNUSED_LOCAL_VARIABLE when it shouldn't.
5653
'CompileTimeErrorCode.UNDEFINED_IDENTIFIER_AWAIT',
5754
// The code has been replaced but is not yet removed.

0 commit comments

Comments
 (0)