linter: Avoid infinite loop in avoid_redundant_argument_values

Fixes https://github.com/dart-lang/linter/issues/4970

Change-Id: I75e6fd554f12038acbd7604f52b2fe50d11a682d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/366500
Auto-Submit: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Phil Quitslund <pquitslund@google.com>
Reviewed-by: Phil Quitslund <pquitslund@google.com>
This commit is contained in:
Sam Rawlins 2024-05-14 20:59:48 +00:00 committed by Commit Queue
parent 480e631727
commit 7c5437b8bf
2 changed files with 29 additions and 5 deletions

View file

@ -135,15 +135,23 @@ class _Visitor extends SimpleAstVisitor {
}
var redirectedConstructor = constructor?.redirectedConstructor;
while (redirectedConstructor?.redirectedConstructor != null) {
redirectedConstructor = redirectedConstructor?.redirectedConstructor;
}
if (redirectedConstructor == null) {
if (constructor == null || redirectedConstructor == null) {
check(node.argumentList);
return;
}
var parameters = redirectedConstructor.parameters;
var visitedConstructors = {constructor};
while (redirectedConstructor != null) {
if (visitedConstructors.contains(redirectedConstructor)) {
// Cycle. Compile-time error.
return;
}
visitedConstructors.add(redirectedConstructor);
constructor = redirectedConstructor;
redirectedConstructor = redirectedConstructor.redirectedConstructor;
}
var parameters = constructor!.parameters;
// If the constructor being called is a redirecting factory constructor, an
// argument is redundant if it is equal to the default value of the

View file

@ -83,6 +83,22 @@ void f() {
''');
}
test_redirectingFactoryConstructor_cyclic() async {
await assertDiagnostics(r'''
class A {
factory A.foo() = A.bar;
factory A.bar() = A.foo;
}
void f() {
A.foo();
}
''', [
// No lint.
error(CompileTimeErrorCode.RECURSIVE_FACTORY_REDIRECT, 30, 5),
error(CompileTimeErrorCode.RECURSIVE_FACTORY_REDIRECT, 57, 5),
]);
}
test_redirectingFactoryConstructor_multipleOptional() async {
await assertNoDiagnostics(r'''
class A {