Skip to content

Commit

Permalink
[element model] migrate avoid_redundant_argument_values
Browse files Browse the repository at this point in the history
Bug: https://github.com/dart-lang/linter/issues/5099
Change-Id: Ia4ac6f2c0684f0f1f22a36cab4625bbaad38c5ef
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/390822
Auto-Submit: Phil Quitslund <[email protected]>
Commit-Queue: Brian Wilkerson <[email protected]>
Reviewed-by: Brian Wilkerson <[email protected]>
  • Loading branch information
pq authored and Commit Queue committed Oct 17, 2024
1 parent f78b304 commit 8f40f77
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
9 changes: 7 additions & 2 deletions pkg/analyzer/lib/src/dart/element/member.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,13 @@ class ConstructorMember extends ExecutableMember
}

@override
ConstructorElement2? get redirectedConstructor2 =>
redirectedConstructor.asElement2 as ConstructorElement2;
ConstructorElement2? get redirectedConstructor2 {
var element = redirectedConstructor.asElement2;
return switch (element) {
ConstructorElement2() => element,
_ => null,
};
}

@override
InterfaceType get returnType => type.returnType as InterfaceType;
Expand Down
20 changes: 10 additions & 10 deletions pkg/linter/lib/src/rules/avoid_redundant_argument_values.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/src/lint/linter.dart'; // ignore: implementation_imports
import 'package:collection/collection.dart';

Expand Down Expand Up @@ -46,7 +46,7 @@ class _Visitor extends SimpleAstVisitor<void> {

for (var i = arguments.length - 1; i >= 0; --i) {
var arg = arguments[i];
var param = arg.staticParameterElement;
var param = arg.correspondingParameter;
if (arg is NamedExpression) {
arg = arg.expression;
}
Expand All @@ -59,10 +59,10 @@ class _Visitor extends SimpleAstVisitor<void> {
}
}

void checkArgument(Expression arg, ParameterElement? param) {
void checkArgument(Expression arg, FormalParameterElement? param) {
if (param == null ||
param.declaration.isRequired ||
param.hasRequired ||
param.isRequired ||
param.metadata2.hasRequired ||
!param.isOptional) {
return;
}
Expand Down Expand Up @@ -93,13 +93,13 @@ class _Visitor extends SimpleAstVisitor<void> {

@override
void visitInstanceCreationExpression(InstanceCreationExpression node) {
var constructor = node.constructorName.staticElement;
var constructor = node.constructorName.element;
if (constructor != null && !constructor.isFactory) {
check(node.argumentList);
return;
}

var redirectedConstructor = constructor?.redirectedConstructor;
var redirectedConstructor = constructor?.redirectedConstructor2;
if (constructor == null || redirectedConstructor == null) {
check(node.argumentList);
return;
Expand All @@ -113,10 +113,10 @@ class _Visitor extends SimpleAstVisitor<void> {
}
visitedConstructors.add(redirectedConstructor);
constructor = redirectedConstructor;
redirectedConstructor = redirectedConstructor.redirectedConstructor;
redirectedConstructor = redirectedConstructor.redirectedConstructor2;
}

var parameters = constructor!.parameters;
var parameters = constructor!.formalParameters;

// If the constructor being called is a redirecting factory constructor, an
// argument is redundant if it is equal to the default value of the
Expand All @@ -130,7 +130,7 @@ class _Visitor extends SimpleAstVisitor<void> {

for (var i = arguments.length - 1; i >= 0; --i) {
var arg = arguments[i];
ParameterElement? param;
FormalParameterElement? param;
if (arg is NamedExpression) {
param = parameters.firstWhereOrNull(
(p) => p.isNamed && p.name == arg.name.label.name);
Expand Down

0 comments on commit 8f40f77

Please sign in to comment.