Skip to content

Commit 98ec623

Browse files
pqCommit Queue
authored andcommitted
[element model] migrate parameter_assignments
Bug: https://github.com/dart-lang/linter/issues/5099 Change-Id: I8b674c1860e4c1fc492948670698a6ca9ee99ce0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/391567 Reviewed-by: Brian Wilkerson <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent 97ab888 commit 98ec623

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8166,13 +8166,15 @@ sealed class FunctionBodyImpl extends AstNodeImpl implements FunctionBody {
81668166
@experimental
81678167
@override
81688168
bool isPotentiallyMutatedInScope2(VariableElement2 variable) {
8169-
if (variable is LocalVariableElementImpl2) {
8170-
return isPotentiallyMutatedInScope(variable.wrappedElement);
8171-
}
8172-
if (variable case VariableElement variable) {
8173-
return isPotentiallyMutatedInScope(variable);
8174-
}
8175-
return false;
8169+
return switch (variable) {
8170+
LocalVariableElementImpl2() =>
8171+
isPotentiallyMutatedInScope(variable.wrappedElement),
8172+
VariableElement() =>
8173+
isPotentiallyMutatedInScope(variable as VariableElement),
8174+
FormalParameterElement(:VariableElement firstFragment) =>
8175+
isPotentiallyMutatedInScope(firstFragment),
8176+
_ => false,
8177+
};
81768178
}
81778179

81788180
/// Dispatch this function body to the resolver, imposing [imposedType] as the

pkg/linter/analyzer_use_new_elements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ lib/src/rules/one_member_abstracts.dart
110110
lib/src/rules/only_throw_errors.dart
111111
lib/src/rules/package_api_docs.dart
112112
lib/src/rules/package_prefixed_library_names.dart
113+
lib/src/rules/parameter_assignments.dart
113114
lib/src/rules/prefer_adjacent_string_concatenation.dart
114115
lib/src/rules/prefer_asserts_with_message.dart
115116
lib/src/rules/prefer_bool_in_asserts.dart

pkg/linter/lib/src/rules/parameter_assignments.dart

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import 'package:analyzer/dart/ast/ast.dart';
66
import 'package:analyzer/dart/ast/visitor.dart';
7-
import 'package:analyzer/dart/element/element.dart';
7+
import 'package:analyzer/dart/element/element2.dart';
88

99
import '../analyzer.dart';
1010

@@ -18,7 +18,7 @@ bool _isFormalParameterReassigned(
1818
FormalParameter parameter, AssignmentExpression assignment) {
1919
var leftHandSide = assignment.leftHandSide;
2020
return leftHandSide is SimpleIdentifier &&
21-
leftHandSide.staticElement == parameter.declaredElement;
21+
leftHandSide.element == parameter.declaredFragment?.element;
2222
}
2323

2424
class ParameterAssignments extends LintRule {
@@ -51,7 +51,7 @@ class _DeclarationVisitor extends RecursiveAstVisitor<void> {
5151
{required this.paramIsNotNullByDefault,
5252
required this.paramDefaultsToNull});
5353

54-
Element? get parameterElement => parameter.declaredElement;
54+
Element2? get parameterElement => parameter.declaredFragment?.element;
5555

5656
void checkPatternElements(DartPattern node) {
5757
NodeList<PatternField>? fields;
@@ -112,8 +112,7 @@ class _DeclarationVisitor extends RecursiveAstVisitor<void> {
112112
visitPostfixExpression(PostfixExpression node) {
113113
if (paramIsNotNullByDefault) {
114114
var operand = node.operand;
115-
if (operand is SimpleIdentifier &&
116-
operand.staticElement == parameterElement) {
115+
if (operand is SimpleIdentifier && operand.element == parameterElement) {
117116
reportLint(node);
118117
}
119118
}
@@ -125,8 +124,7 @@ class _DeclarationVisitor extends RecursiveAstVisitor<void> {
125124
visitPrefixExpression(PrefixExpression node) {
126125
if (paramIsNotNullByDefault) {
127126
var operand = node.operand;
128-
if (operand is SimpleIdentifier &&
129-
operand.staticElement == parameterElement) {
127+
if (operand is SimpleIdentifier && operand.element == parameterElement) {
130128
reportLint(node);
131129
}
132130
}
@@ -155,9 +153,9 @@ class _Visitor extends SimpleAstVisitor<void> {
155153
if (parameterList == null) return;
156154

157155
for (var parameter in parameterList.parameters) {
158-
var declaredElement = parameter.declaredElement;
156+
var declaredElement = parameter.declaredFragment?.element;
159157
if (declaredElement != null &&
160-
body.isPotentiallyMutatedInScope(declaredElement)) {
158+
body.isPotentiallyMutatedInScope2(declaredElement)) {
161159
var paramIsNotNullByDefault = parameter is SimpleFormalParameter ||
162160
_isDefaultFormalParameterWithDefaultValue(parameter);
163161
var paramDefaultsToNull = parameter is DefaultFormalParameter &&
@@ -173,9 +171,8 @@ class _Visitor extends SimpleAstVisitor<void> {
173171
}
174172

175173
extension on AstNode {
176-
Element? get element {
177-
var self = this;
178-
if (self is AssignedVariablePattern) return self.element;
179-
return null;
180-
}
174+
Element2? get element => switch (this) {
175+
AssignedVariablePattern(:var element2) => element2,
176+
_ => null,
177+
};
181178
}

0 commit comments

Comments
 (0)