Skip to content

Commit fed66f6

Browse files
author
Dart CI
committed
Version 2.12.0-3.0.dev
Merge commit '2d4a3bd913c1f61da00ee96cfef2f90361f15271' into 'dev'
2 parents f01bcdc + 2d4a3bd commit fed66f6

File tree

4 files changed

+117
-9
lines changed

4 files changed

+117
-9
lines changed

pkg/analyzer/lib/src/dart/ast/extensions.dart

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,29 @@ import 'package:analyzer/dart/element/element.dart';
77
import 'package:analyzer/dart/element/type.dart';
88
import 'package:analyzer/src/dart/ast/ast.dart';
99

10+
/// TODO(scheglov) https://github.com/dart-lang/sdk/issues/43608
11+
Element _readElement(AstNode node) {
12+
var parent = node.parent;
13+
14+
if (parent is AssignmentExpression && parent.leftHandSide == node) {
15+
return parent.readElement;
16+
}
17+
if (parent is PostfixExpression && parent.operand == node) {
18+
return parent.readElement;
19+
}
20+
if (parent is PrefixExpression && parent.operand == node) {
21+
return parent.readElement;
22+
}
23+
24+
if (parent is PrefixedIdentifier && parent.identifier == node) {
25+
return _readElement(parent);
26+
}
27+
if (parent is PropertyAccess && parent.propertyName == node) {
28+
return _readElement(parent);
29+
}
30+
return null;
31+
}
32+
1033
/// TODO(scheglov) https://github.com/dart-lang/sdk/issues/43608
1134
Element _writeElement(AstNode node) {
1235
var parent = node.parent;
@@ -73,6 +96,14 @@ extension FormalParameterExtension on FormalParameter {
7396

7497
/// TODO(scheglov) https://github.com/dart-lang/sdk/issues/43608
7598
extension IdentifierExtension on Identifier {
99+
Element get writeElement {
100+
return _writeElement(this);
101+
}
102+
103+
Element get readElement {
104+
return _readElement(this);
105+
}
106+
76107
Element get writeOrReadElement {
77108
return _writeElement(this) ?? staticElement;
78109
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ class GatherUsedLocalElementsVisitor extends RecursiveAstVisitor<void> {
161161
usedElements.addElement(element);
162162
}
163163
} else {
164-
_useIdentifierElement(node);
164+
_useIdentifierElement(node, node.readElement);
165+
_useIdentifierElement(node, node.writeElement);
166+
_useIdentifierElement(node, node.staticElement);
165167
var parent = node.parent;
166168
// If [node] is a method tear-off, assume all parameters are used.
167169
var functionReferenceIsCall =
@@ -216,9 +218,8 @@ class GatherUsedLocalElementsVisitor extends RecursiveAstVisitor<void> {
216218
}
217219
}
218220

219-
/// Marks an [Element] of [node] as used in the library.
220-
void _useIdentifierElement(Identifier node) {
221-
Element element = node.staticElement;
221+
/// Marks the [element] of [node] as used in the library.
222+
void _useIdentifierElement(Identifier node, Element element) {
222223
if (element == null) {
223224
return;
224225
}

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

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,31 @@ print(x) {}
155155
]);
156156
}
157157

158+
test_classGetterSetter_isUsed_assignmentExpression_compound() async {
159+
await assertNoErrorsInCode(r'''
160+
class A {
161+
int get _foo => 0;
162+
set _foo(int _) {}
163+
164+
void f() {
165+
_foo += 2;
166+
}
167+
}
168+
''');
169+
}
170+
171+
test_classSetter_isUsed_assignmentExpression_simple() async {
172+
await assertNoErrorsInCode(r'''
173+
class A {
174+
set _foo(int _) {}
175+
176+
void f() {
177+
_foo = 0;
178+
}
179+
}
180+
''');
181+
}
182+
158183
test_constructor_isUsed_asRedirectee() async {
159184
await assertNoErrorsInCode(r'''
160185
class A {
@@ -818,8 +843,8 @@ extension _A on String {
818843
]);
819844
}
820845

821-
// Postfix operators can only be called, not defined. The "notUsed" sibling to
822-
// this test is the test on a binary operator.
846+
/// Postfix operators can only be called, not defined. The "notUsed" sibling to
847+
/// this test is the test on a binary operator.
823848
test_method_notUsed_privateExtension_indexEqOperator() async {
824849
await assertErrorsInCode(r'''
825850
extension _A on bool {
@@ -840,8 +865,8 @@ extension _A on bool {
840865
]);
841866
}
842867

843-
// Assignment operators can only be called, not defined. The "notUsed" sibling
844-
// to this test is the test on a binary operator.
868+
/// Assignment operators can only be called, not defined. The "notUsed" sibling
869+
/// to this test is the test on a binary operator.
845870
test_method_notUsed_privateExtension_operator() async {
846871
await assertErrorsInCode(r'''
847872
extension _A on String {
@@ -1383,6 +1408,57 @@ _f(int p) => 7;
13831408
]);
13841409
}
13851410

1411+
test_topLevelGetterSetter_isUsed_assignmentExpression_compound() async {
1412+
await assertNoErrorsInCode(r'''
1413+
int get _foo => 0;
1414+
set _foo(int _) {}
1415+
1416+
void f() {
1417+
_foo += 2;
1418+
}
1419+
''');
1420+
}
1421+
1422+
test_topLevelGetterSetter_isUsed_postfixExpression_increment() async {
1423+
await assertNoErrorsInCode(r'''
1424+
int get _foo => 0;
1425+
set _foo(int _) {}
1426+
1427+
void f() {
1428+
_foo++;
1429+
}
1430+
''');
1431+
}
1432+
1433+
test_topLevelGetterSetter_isUsed_prefixExpression_increment() async {
1434+
await assertNoErrorsInCode(r'''
1435+
int get _foo => 0;
1436+
set _foo(int _) {}
1437+
1438+
void f() {
1439+
++_foo;
1440+
}
1441+
''');
1442+
}
1443+
1444+
test_topLevelSetter_isUsed_assignmentExpression_simple() async {
1445+
await assertNoErrorsInCode(r'''
1446+
set _foo(int _) {}
1447+
1448+
void f() {
1449+
_foo = 0;
1450+
}
1451+
''');
1452+
}
1453+
1454+
test_topLevelSetter_notUsed() async {
1455+
await assertErrorsInCode(r'''
1456+
set _foo(int _) {}
1457+
''', [
1458+
error(HintCode.UNUSED_ELEMENT, 4, 4),
1459+
]);
1460+
}
1461+
13861462
test_topLevelVariable_isUsed() async {
13871463
await assertNoErrorsInCode(r'''
13881464
int _a = 1;

tools/VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ CHANNEL dev
2727
MAJOR 2
2828
MINOR 12
2929
PATCH 0
30-
PRERELEASE 2
30+
PRERELEASE 3
3131
PRERELEASE_PATCH 0

0 commit comments

Comments
 (0)