Skip to content

Commit

Permalink
Version 3.5.0-83.0.dev
Browse files Browse the repository at this point in the history
Merge 553df4b into dev
  • Loading branch information
Dart CI committed Apr 20, 2024
2 parents 95e0894 + 553df4b commit d4c5d9c
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,7 @@ void f(Object? x) {
Future<void> test_objectPattern_declarationStatement_multi_first() async {
await resolveTestCode(r'''
void f(A a) {
var A(:foo, :bar) = a;
bar;
var A(:foo, bar: int()) = a;
}
class A {
Expand All @@ -411,8 +410,7 @@ class A {
''');
await assertHasFix(r'''
void f(A a) {
var A(:bar) = a;
bar;
var A(bar: int()) = a;
}
class A {
Expand All @@ -425,8 +423,7 @@ class A {
Future<void> test_objectPattern_declarationStatement_multi_last() async {
await resolveTestCode(r'''
void f(A a) {
var A(:foo, :bar) = a;
foo;
var A(foo: int(), :bar) = a;
}
class A {
Expand All @@ -436,8 +433,7 @@ class A {
''');
await assertHasFix(r'''
void f(A a) {
var A(:foo) = a;
foo;
var A(foo: int()) = a;
}
class A {
Expand Down Expand Up @@ -572,14 +568,12 @@ void f(Object? x) {
Future<void> test_recordPattern_named_declaration() async {
await resolveTestCode(r'''
void f(({int foo, int bar}) x) {
var (:foo, :bar) = x;
bar;
var (:foo, bar: int()) = x;
}
''');
await assertHasFix(r'''
void f(({int foo, int bar}) x) {
var (foo: _, :bar) = x;
bar;
var (foo: _, bar: int()) = x;
}
''');
}
Expand All @@ -600,14 +594,12 @@ void f(Object? x) {
Future<void> test_recordPattern_positional_declaration() async {
await resolveTestCode(r'''
void f(Object? x) {
var (foo, bar) = (0, 1);
bar;
var (foo, int()) = (0, 1);
}
''');
await assertHasFix(r'''
void f(Object? x) {
var (_, bar) = (0, 1);
bar;
var (_, int()) = (0, 1);
}
''');
}
Expand Down
40 changes: 39 additions & 1 deletion pkg/analyzer/lib/src/error/unused_local_elements_verifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,10 @@ class UnusedLocalElementsVerifier extends RecursiveAstVisitor<void> {
/// The URI of the library being verified.
final Uri _libraryUri;

/// The current set of pattern variable elements, used to track whether _all_
/// within a [PatternVariableDeclaration] are used.
List<BindPatternVariableElement>? _patternVariableElements;

/// Create a new instance of the [UnusedLocalElementsVerifier].
UnusedLocalElementsVerifier(this._errorListener, this._usedElements,
this._inheritanceManager, LibraryElement library)
Expand Down Expand Up @@ -526,7 +530,12 @@ class UnusedLocalElementsVerifier extends RecursiveAstVisitor<void> {
) {
var declaredElement = node.declaredElement!;
if (!declaredElement.isDuplicate) {
_visitLocalVariableElement(declaredElement);
var patternVariableElements = _patternVariableElements;
if (patternVariableElements != null) {
patternVariableElements.add(declaredElement);
} else {
_visitLocalVariableElement(declaredElement);
}
}

super.visitDeclaredVariablePattern(node);
Expand Down Expand Up @@ -637,6 +646,35 @@ class UnusedLocalElementsVerifier extends RecursiveAstVisitor<void> {
super.visitMixinDeclaration(node);
}

@override
void visitPatternVariableDeclaration(PatternVariableDeclaration node) {
var outerPatternVariableElements = _patternVariableElements;
var patternVariableElements = _patternVariableElements = [];
try {
super.visitPatternVariableDeclaration(node);
var elementsToReport = <BindPatternVariableElement>[];
for (var element in patternVariableElements) {
var isUsed = _usedElements.elements.contains(element);
// Don't report any of the declared variables as unused, if any of them
// are used. This allows for a consistent set of patterns to be used,
// in a case where some declared variables are used, and some are just
// present to help match, for example, a record shape, or a list, etc.
if (isUsed) {
return;
}
if (!_isNamedUnderscore(element)) {
elementsToReport.add(element);
}
}
for (var element in elementsToReport) {
_reportErrorForElement(
WarningCode.UNUSED_LOCAL_VARIABLE, element, [element.displayName]);
}
} finally {
_patternVariableElements = outerPatternVariableElements;
}
}

@override
void visitSimpleIdentifier(SimpleIdentifier node) {
if (node.inDeclarationContext()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import '../dart/resolution/context_collection_resolution.dart';

main() {
defineReflectiveSuite(() {
defineReflectiveTests(SuperInExtensionTest);
defineReflectiveTests(SuperInExtensionTypeTest);
});
}

@reflectiveTest
class SuperInExtensionTest extends PubPackageResolutionTest {
class SuperInExtensionTypeTest extends PubPackageResolutionTest {
test_binaryOperator() async {
await assertErrorsInCode('''
extension type A(int it) {
Expand Down
54 changes: 54 additions & 0 deletions pkg/analyzer/test/src/diagnostics/unused_local_variable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,42 @@ main() {
''');
}

test_patternVariableDeclarationStatement_noneUsed() async {
await assertErrorsInCode(r'''
void f() {
var (a, b) = (0, 1);
}
''', [
error(WarningCode.UNUSED_LOCAL_VARIABLE, 18, 1),
error(WarningCode.UNUSED_LOCAL_VARIABLE, 21, 1),
]);
}

test_patternVariableDeclarationStatement_noneUsed_nested() async {
await assertErrorsInCode(r'''
void f() {
var (a, [b, _]) = (0, []);
}
''', [
error(WarningCode.UNUSED_LOCAL_VARIABLE, 18, 1),
error(WarningCode.UNUSED_LOCAL_VARIABLE, 22, 1),
]);
}

test_patternVariableDeclarationStatement_noneUsed_withChildStatements() async {
await assertErrorsInCode(r'''
void f() {
var (a, b) = () {
var (c, d) = (0, 1);
return (c, d);
}();
}
''', [
error(WarningCode.UNUSED_LOCAL_VARIABLE, 18, 1),
error(WarningCode.UNUSED_LOCAL_VARIABLE, 21, 1),
]);
}

test_patternVariableDeclarationStatement_notUsed() async {
await assertErrorsInCode(r'''
void f() {
Expand All @@ -150,6 +186,24 @@ void f() {
]);
}

test_patternVariableDeclarationStatement_someUsed() async {
await assertNoErrorsInCode(r'''
void f() {
var (a, b) = (0, 1);
a;
}
''');
}

test_patternVariableDeclarationStatement_someUsed_nested() async {
await assertNoErrorsInCode(r'''
void f() {
var (a, [b, c]) = (0, []);
c;
}
''');
}

test_patternVariableDeclarationStatement_used() async {
await assertNoErrorsInCode(r'''
void f() {
Expand Down
2 changes: 1 addition & 1 deletion tools/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ CHANNEL dev
MAJOR 3
MINOR 5
PATCH 0
PRERELEASE 82
PRERELEASE 83
PRERELEASE_PATCH 0

0 comments on commit d4c5d9c

Please sign in to comment.