55import 'package:analyzer/dart/analysis/features.dart' ;
66import 'package:analyzer/dart/ast/ast.dart' ;
77import 'package:analyzer/dart/ast/visitor.dart' ;
8- import 'package:analyzer/dart/element/element .dart' ;
8+ import 'package:analyzer/dart/element/element2 .dart' ;
99
1010import '../analyzer.dart' ;
1111import '../extensions.dart' ;
1212import '../util/dart_type_utilities.dart' ;
1313
1414const _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
1919class UnnecessaryLambdas extends LintRule {
@@ -35,7 +35,7 @@ class UnnecessaryLambdas extends LintRule {
3535}
3636
3737class _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
7474class _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
216223extension 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