Skip to content

Commit b675b9f

Browse files
pqCommit Queue
authored andcommitted
[element model] migrate unnecessary_lambdas
Bug: https://github.com/dart-lang/linter/issues/5099 Change-Id: I2d1d8da8b5adb588e52c01ffdd214b3184171af5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/393141 Commit-Queue: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]> Auto-Submit: Phil Quitslund <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent d2d4325 commit b675b9f

File tree

2 files changed

+35
-33
lines changed

2 files changed

+35
-33
lines changed

pkg/linter/analyzer_use_new_elements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ lib/src/rules/unnecessary_const.dart
187187
lib/src/rules/unnecessary_constructor_name.dart
188188
lib/src/rules/unnecessary_final.dart
189189
lib/src/rules/unnecessary_getters_setters.dart
190+
lib/src/rules/unnecessary_lambdas.dart
190191
lib/src/rules/unnecessary_late.dart
191192
lib/src/rules/unnecessary_library_directive.dart
192193
lib/src/rules/unnecessary_library_name.dart

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

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
import 'package:analyzer/dart/analysis/features.dart';
66
import 'package:analyzer/dart/ast/ast.dart';
77
import 'package:analyzer/dart/ast/visitor.dart';
8-
import 'package:analyzer/dart/element/element.dart';
8+
import 'package:analyzer/dart/element/element2.dart';
99

1010
import '../analyzer.dart';
1111
import '../extensions.dart';
1212
import '../util/dart_type_utilities.dart';
1313

1414
const _desc = r"Don't create a lambda when a tear-off will do.";
1515

16-
Set<Element?> _extractElementsOfSimpleIdentifiers(AstNode node) =>
16+
Set<Element2?> _extractElementsOfSimpleIdentifiers(AstNode node) =>
1717
_IdentifierVisitor().extractElements(node);
1818

1919
class UnnecessaryLambdas extends LintRule {
@@ -35,7 +35,7 @@ class UnnecessaryLambdas extends LintRule {
3535
}
3636

3737
class _FinalExpressionChecker {
38-
final Set<ParameterElement?> parameters;
38+
final Set<FormalParameterElement?> parameters;
3939

4040
_FinalExpressionChecker(this.parameters);
4141

@@ -60,7 +60,7 @@ class _FinalExpressionChecker {
6060
}
6161

6262
if (node is SimpleIdentifier) {
63-
var element = node.staticElement;
63+
var element = node.element;
6464
if (parameters.contains(element)) {
6565
return false;
6666
}
@@ -72,18 +72,18 @@ class _FinalExpressionChecker {
7272
}
7373

7474
class _IdentifierVisitor extends RecursiveAstVisitor<void> {
75-
final _elements = <Element?>{};
75+
final _elements = <Element2?>{};
7676

7777
_IdentifierVisitor();
7878

79-
Set<Element?> extractElements(AstNode node) {
79+
Set<Element2?> extractElements(AstNode node) {
8080
node.accept(this);
8181
return _elements;
8282
}
8383

8484
@override
8585
visitSimpleIdentifier(SimpleIdentifier node) {
86-
_elements.add(node.staticElement);
86+
_elements.add(node.element);
8787
super.visitSimpleIdentifier(node);
8888
}
8989
}
@@ -100,7 +100,8 @@ class _Visitor extends SimpleAstVisitor<void> {
100100

101101
@override
102102
void visitFunctionExpression(FunctionExpression node) {
103-
if (node.declaredElement?.name != '' || node.body.keyword != null) {
103+
var element = node.declaredElement2 ?? node.declaredFragment?.element;
104+
if (element?.name3 != '' || node.body.keyword != null) {
104105
return;
105106
}
106107
var body = node.body;
@@ -139,8 +140,11 @@ class _Visitor extends SimpleAstVisitor<void> {
139140
return;
140141
}
141142

142-
var nodeType = node.declaredElement?.type;
143-
var invocationType = expression.constructorName.staticElement?.type;
143+
var functionElement =
144+
node.declaredElement2 ?? node.declaredFragment?.element;
145+
146+
var nodeType = functionElement?.type;
147+
var invocationType = expression.constructorName.element?.type;
144148
if (nodeType == null) return;
145149
if (invocationType == null) return;
146150
// It is possible that the invocation function type is a valid replacement
@@ -175,7 +179,8 @@ class _Visitor extends SimpleAstVisitor<void> {
175179
return;
176180
}
177181

178-
var parameters = nodeToLintParams.map((e) => e.declaredElement).toSet();
182+
var parameters =
183+
nodeToLintParams.map((e) => e.declaredFragment?.element).toSet();
179184
if (node is FunctionExpressionInvocation) {
180185
if (node.function.mightBeDeferred) return;
181186

@@ -197,15 +202,17 @@ class _Visitor extends SimpleAstVisitor<void> {
197202
if (argType == null) return;
198203
if (!typeSystem.isSubtypeOf(tearoffType, argType)) return;
199204
} else if (parent is VariableDeclaration) {
200-
var variableType = parent.declaredElement?.type;
205+
var variableElement =
206+
parent.declaredElement2 ?? parent.declaredFragment?.element;
207+
var variableType = variableElement?.type;
201208
if (variableType == null) return;
202209
if (!typeSystem.isSubtypeOf(tearoffType, variableType)) return;
203210
}
204211

205212
var checker = _FinalExpressionChecker(parameters);
206213
if (!node.containsNullAwareInvocationInChain &&
207214
checker.isFinalNode(node.target) &&
208-
node.methodName.staticElement.isFinal &&
215+
node.methodName.element.isFinal &&
209216
node.typeArguments == null) {
210217
rule.reportLint(nodeToLint);
211218
}
@@ -215,30 +222,24 @@ class _Visitor extends SimpleAstVisitor<void> {
215222

216223
extension on Expression? {
217224
bool get mightBeDeferred {
218-
var self = this;
219-
var element = switch (self) {
220-
PrefixedIdentifier() => self.prefix.staticElement,
221-
SimpleIdentifier() => self.staticElement,
225+
var element = switch (this) {
226+
PrefixedIdentifier(:var prefix) => prefix.element,
227+
SimpleIdentifier(:var element) => element,
222228
_ => null,
223229
};
224-
return element is PrefixElement &&
225-
element.imports.any((e) => e.prefix is DeferredImportElementPrefix);
230+
return element is PrefixElement2 &&
231+
element.imports.any((e) => e.prefix2?.isDeferred ?? false);
226232
}
227233
}
228234

229-
extension on Element? {
235+
extension on Element2? {
230236
/// Returns whether this is a `final` variable or property and not `late`.
231-
bool get isFinal {
232-
var self = this;
233-
if (self is PropertyAccessorElement) {
234-
var variable = self.variable2;
235-
return self.isSynthetic &&
236-
variable != null &&
237-
variable.isFinal &&
238-
!variable.isLate;
239-
} else if (self is VariableElement) {
240-
return self.isFinal && !self.isLate;
241-
}
242-
return true;
243-
}
237+
bool get isFinal => switch (this) {
238+
GetterElement(:var isSynthetic, :var variable3?) ||
239+
SetterElement(:var isSynthetic, :var variable3?) =>
240+
isSynthetic && variable3.isFinal && !variable3.isLate,
241+
VariableElement2(:var isLate, :var isFinal) => isFinal && !isLate,
242+
// TODO(pq): [element model] this preserves existing v1 semantics but looks fishy
243+
_ => true,
244+
};
244245
}

0 commit comments

Comments
 (0)