Skip to content

Commit f517dd6

Browse files
pqCommit Queue
authored and
Commit Queue
committed
[wildcards] update UNUSED_ELEMENT reporting for local functions
Change-Id: Id15e72ae91ee46273a80bec7af447e5a0fa41cca Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/368880 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent d36a892 commit f517dd6

File tree

3 files changed

+107
-4
lines changed

3 files changed

+107
-4
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -756,8 +756,11 @@ class UnusedLocalElementsVerifier extends RecursiveAstVisitor<void> {
756756
}
757757

758758
/// Returns whether the name of [element] should be treated as a wildcard.
759-
bool _isNamedWildcard(LocalVariableElement element) {
760-
String name = element.name;
759+
bool _isNamedWildcard(LocalElement element) {
760+
// TODO(pq): ask the element once implemented.
761+
var name = element.name;
762+
if (name == null) return false;
763+
761764
var length = name.length;
762765
if (length > 1 && _wildCardVariablesEnabled) return false;
763766

@@ -1013,6 +1016,10 @@ class UnusedLocalElementsVerifier extends RecursiveAstVisitor<void> {
10131016

10141017
void _visitFunctionElement(FunctionElement element) {
10151018
if (!_isUsedElement(element)) {
1019+
if (_wildCardVariablesEnabled) {
1020+
if (element.isLocal && _isNamedWildcard(element)) return;
1021+
}
1022+
10161023
_reportErrorForElement(
10171024
WarningCode.UNUSED_ELEMENT, element, [element.displayName]);
10181025
}
@@ -1156,3 +1163,8 @@ class UsedLocalElements {
11561163
return catchStackTraceElements.contains(element);
11571164
}
11581165
}
1166+
1167+
extension on FunctionElement {
1168+
bool get isLocal =>
1169+
enclosingElement is FunctionElement || enclosingElement is MethodElement;
1170+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,3 +633,10 @@ mixin WithStrictCastsMixin on PubPackageResolutionTest {
633633
Future<void> assertNoErrorsWithStrictCasts(String code) async =>
634634
assertErrorsWithStrictCasts(code, []);
635635
}
636+
637+
// TODO(pq): revisit, https://dart-review.googlesource.com/c/sdk/+/368880
638+
mixin WithWildCardVariablesMixin on PubPackageResolutionTest {
639+
@override
640+
List<String> get experiments =>
641+
[...super.experiments, Feature.wildcard_variables.enableString];
642+
}

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

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:analyzer/src/dart/error/hint_codes.dart';
6-
import 'package:analyzer/src/error/codes.g.dart';
5+
import 'package:analyzer/src/error/codes.dart';
76
import 'package:test/expect.dart';
87
import 'package:test_reflective_loader/test_reflective_loader.dart';
98

@@ -12,6 +11,7 @@ import '../dart/resolution/context_collection_resolution.dart';
1211
main() {
1312
defineReflectiveSuite(() {
1413
defineReflectiveTests(UnusedElementTest);
14+
defineReflectiveTests(UnusedElementWildCardVariablesTest);
1515
});
1616
}
1717

@@ -571,6 +571,22 @@ class A {
571571
''');
572572
}
573573

574+
test_function_underscore() async {
575+
await assertErrorsInCode(r'''
576+
_(){}
577+
''', [
578+
error(WarningCode.UNUSED_ELEMENT, 0, 1),
579+
]);
580+
}
581+
582+
test_function_underscores() async {
583+
await assertErrorsInCode(r'''
584+
__(){}
585+
''', [
586+
error(WarningCode.UNUSED_ELEMENT, 0, 2),
587+
]);
588+
}
589+
574590
test_functionLocal_isUsed_closure() async {
575591
await assertNoErrorsInCode(r'''
576592
main() {
@@ -824,6 +840,50 @@ class A {
824840
]);
825841
}
826842

843+
test_localFunction_inFunction_wildcard() async {
844+
await assertErrorsInCode(r'''
845+
main() {
846+
_(){}
847+
}
848+
''', [
849+
error(WarningCode.UNUSED_ELEMENT, 11, 1),
850+
]);
851+
}
852+
853+
test_localFunction_inMethod_underscores() async {
854+
await assertErrorsInCode(r'''
855+
class C {
856+
m() {
857+
__(){}
858+
}
859+
}
860+
''', [
861+
error(WarningCode.UNUSED_ELEMENT, 22, 2),
862+
]);
863+
}
864+
865+
test_localFunction_inMethod_wildcard() async {
866+
await assertErrorsInCode(r'''
867+
class C {
868+
m() {
869+
_(){}
870+
}
871+
}
872+
''', [
873+
error(WarningCode.UNUSED_ELEMENT, 22, 1),
874+
]);
875+
}
876+
877+
test_localFunction_underscores() async {
878+
await assertErrorsInCode(r'''
879+
main() {
880+
__(){}
881+
}
882+
''', [
883+
error(WarningCode.UNUSED_ELEMENT, 11, 2),
884+
]);
885+
}
886+
827887
test_method_isUsed_hasPragma_vmEntryPoint() async {
828888
pragma;
829889
await assertNoErrorsInCode(r'''
@@ -2863,3 +2923,27 @@ typedef _A = List<int>;
28632923
]);
28642924
}
28652925
}
2926+
2927+
@reflectiveTest
2928+
class UnusedElementWildCardVariablesTest extends UnusedElementTest
2929+
with WithWildCardVariablesMixin {
2930+
@override
2931+
test_localFunction_inFunction_wildcard() async {
2932+
await assertNoErrorsInCode(r'''
2933+
m() {
2934+
_(){}
2935+
}
2936+
''');
2937+
}
2938+
2939+
@override
2940+
test_localFunction_inMethod_wildcard() async {
2941+
await assertNoErrorsInCode(r'''
2942+
class C {
2943+
m() {
2944+
_(){}
2945+
}
2946+
}
2947+
''');
2948+
}
2949+
}

0 commit comments

Comments
 (0)