@@ -82,19 +82,19 @@ class _Visitor extends SimpleAstVisitor<void> {
82
82
@override
83
83
void visitConstructorDeclaration (ConstructorDeclaration node) {
84
84
var element = node.declaredElement;
85
- if (element == null ) {
86
- return ;
87
- }
85
+ if (element == null ) return ;
86
+ if (element.isConst) return ;
87
+ if (node.body is ! EmptyFunctionBody ) return ;
88
+ if (element.enclosingElement.mixins.isNotEmpty) return ;
89
+ if (! _hasImmutableAnnotation (element.enclosingElement)) return ;
88
90
var isRedirected =
89
91
element.isFactory && element.redirectedConstructor != null ;
90
- if (node.body is EmptyFunctionBody &&
91
- ! element.isConst &&
92
- ! _hasMixin (element.enclosingElement) &&
93
- _hasImmutableAnnotation (element.enclosingElement) &&
94
- (isRedirected && (element.redirectedConstructor? .isConst ?? false ) ||
95
- (! isRedirected &&
96
- _hasConstConstructorInvocation (node) &&
97
- context.canBeConstConstructor (node)))) {
92
+ if (isRedirected && (element.redirectedConstructor? .isConst ?? false )) {
93
+ rule.reportLintForToken (node.firstTokenAfterCommentAndMetadata);
94
+ }
95
+ if (! isRedirected &&
96
+ _hasConstConstructorInvocation (node) &&
97
+ context.canBeConstConstructor (node)) {
98
98
rule.reportLintForToken (node.firstTokenAfterCommentAndMetadata);
99
99
}
100
100
}
@@ -105,26 +105,27 @@ class _Visitor extends SimpleAstVisitor<void> {
105
105
return false ;
106
106
}
107
107
var clazz = declaredElement.enclosingElement;
108
- // construct with super
109
- var superInvocation = node.initializers
110
- .firstWhereOrNull ((e) => e is SuperConstructorInvocation )
111
- as SuperConstructorInvocation ? ;
108
+ // Constructor with super-initializer.
109
+ var superInvocation =
110
+ node.initializers.whereType <SuperConstructorInvocation >().firstOrNull;
112
111
if (superInvocation != null ) {
113
112
return superInvocation.staticElement? .isConst ?? false ;
114
113
}
115
- // construct with this
114
+ // Constructor with ' this' redirecting initializer.
116
115
var redirectInvocation = node.initializers
117
- . firstWhereOrNull ((e) => e is RedirectingConstructorInvocation )
118
- as RedirectingConstructorInvocation ? ;
116
+ . whereType < RedirectingConstructorInvocation >( )
117
+ .firstOrNull ;
119
118
if (redirectInvocation != null ) {
120
119
return redirectInvocation.staticElement? .isConst ?? false ;
121
120
}
122
- // construct with implicit super()
121
+ // Constructor with implicit ` super()` call.
123
122
var supertype = clazz.supertype;
124
123
return supertype != null &&
125
124
supertype.constructors.firstWhere ((e) => e.name.isEmpty).isConst;
126
125
}
127
126
127
+ /// Whether [clazz] or any of it's super-types are annotated with
128
+ /// `@immutable` .
128
129
bool _hasImmutableAnnotation (InterfaceElement clazz) {
129
130
var selfAndInheritedClasses = _getSelfAndSuperClasses (clazz);
130
131
for (var cls in selfAndInheritedClasses) {
@@ -133,8 +134,6 @@ class _Visitor extends SimpleAstVisitor<void> {
133
134
return false ;
134
135
}
135
136
136
- bool _hasMixin (InterfaceElement clazz) => clazz.mixins.isNotEmpty;
137
-
138
137
static List <InterfaceElement > _getSelfAndSuperClasses (InterfaceElement self) {
139
138
InterfaceElement ? current = self;
140
139
var seenElements = < InterfaceElement > {};
0 commit comments