Skip to content

Commit a505205

Browse files
pqCommit Queue
authored andcommitted
[element model] migrate type_init_formals
See: https://github.com/dart-lang/linter/issues/5099 Change-Id: I97af483b5e0020974ad4803c0a9c0efbc6aee63f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/389402 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 4cb5274 commit a505205

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

pkg/analyzer/lib/src/dart/element/element.dart

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4088,10 +4088,10 @@ class FieldFormalParameterElementImpl2 extends FormalParameterElementImpl
40884088
FieldFormalParameterElementImpl2(super.firstFragment);
40894089

40904090
@override
4091-
FieldElement2? get field2 =>
4092-
((firstFragment as FieldFormalParameterElementImpl).field
4093-
as FieldFragment)
4094-
.element;
4091+
FieldElement2? get field2 => switch (firstFragment) {
4092+
FieldFormalParameterElementImpl(:FieldFragment field) => field.element,
4093+
_ => null,
4094+
};
40954095
}
40964096

40974097
class FormalParameterElementImpl extends PromotableElementImpl2
@@ -9724,10 +9724,17 @@ class SuperFormalParameterElementImpl2 extends FormalParameterElementImpl
97249724
SuperFormalParameterElementImpl2(super.firstFragment);
97259725

97269726
@override
9727-
FormalParameterElement? get superConstructorParameter2 =>
9728-
((firstFragment as SuperFormalParameterElementImpl)
9729-
.superConstructorParameter as FormalParameterFragment?)
9730-
?.element;
9727+
FormalParameterElement? get superConstructorParameter2 {
9728+
var superConstructorParameter =
9729+
(firstFragment as SuperFormalParameterElementImpl)
9730+
.superConstructorParameter;
9731+
return switch (superConstructorParameter) {
9732+
ParameterMember() =>
9733+
superConstructorParameter.asElement2 as FormalParameterElement?,
9734+
FormalParameterFragment()? => superConstructorParameter.element,
9735+
_ => null,
9736+
};
9737+
}
97319738
}
97329739

97339740
class TopLevelFunctionElementImpl extends ExecutableElementImpl2

pkg/linter/analyzer_use_new_elements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ lib/src/rules/test_types_in_equals.dart
167167
lib/src/rules/throw_in_finally.dart
168168
lib/src/rules/tighten_type_of_initializing_formals.dart
169169
lib/src/rules/type_annotate_public_apis.dart
170+
lib/src/rules/type_init_formals.dart
170171
lib/src/rules/type_literal_in_constant_pattern.dart
171172
lib/src/rules/unintended_html_in_doc_comment.dart
172173
lib/src/rules/unnecessary_await_in_return.dart

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

Lines changed: 7 additions & 7 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

@@ -39,10 +39,10 @@ class _Visitor extends SimpleAstVisitor<void> {
3939
var nodeType = node.type;
4040
if (nodeType == null) return;
4141

42-
var paramElement = node.declaredElement;
43-
if (paramElement is! FieldFormalParameterElement) return;
42+
var paramElement = node.declaredFragment?.element;
43+
if (paramElement is! FieldFormalParameterElement2) return;
4444

45-
var field = paramElement.field;
45+
var field = paramElement.field2;
4646
// If no such field exists, the code is invalid; do not report lint.
4747
if (field != null && nodeType.type == field.type) {
4848
rule.reportLint(nodeType);
@@ -54,10 +54,10 @@ class _Visitor extends SimpleAstVisitor<void> {
5454
var nodeType = node.type;
5555
if (nodeType == null) return;
5656

57-
var paramElement = node.declaredElement;
58-
if (paramElement is! SuperFormalParameterElement) return;
57+
var paramElement = node.declaredFragment?.element;
58+
if (paramElement is! SuperFormalParameterElement2) return;
5959

60-
var superConstructorParameter = paramElement.superConstructorParameter;
60+
var superConstructorParameter = paramElement.superConstructorParameter2;
6161
if (superConstructorParameter == null) return;
6262

6363
if (superConstructorParameter.type == nodeType.type) {

0 commit comments

Comments
 (0)