Skip to content

Commit a0bc891

Browse files
authored
fix super.key false positive for use_key_in_widget_constructors (dart-archive/linter#3295)
* fix super.key false positive for `use_key_in_widget_constructors` * - type test
1 parent 5408090 commit a0bc891

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

lib/src/rules/use_key_in_widget_constructors.dart

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class _Visitor extends SimpleAstVisitor<void> {
7979
classElement.isPublic &&
8080
hasWidgetAsAscendant(classElement) &&
8181
!isExactWidget(classElement) &&
82+
!_hasKeySuperParameterInitializerArg(node) &&
8283
!node.initializers.any((initializer) {
8384
if (initializer is SuperConstructorInvocation) {
8485
var staticElement = initializer.staticElement;
@@ -98,12 +99,23 @@ class _Visitor extends SimpleAstVisitor<void> {
9899
super.visitConstructorDeclaration(node);
99100
}
100101

101-
bool _defineKeyParameter(ConstructorElement element) =>
102-
element.parameters.any((e) => e.name == 'key' && _isKeyType(e.type));
103-
104102
bool _defineKeyArgument(ArgumentList argumentList) => argumentList.arguments
105103
.any((a) => a.staticParameterElement?.name == 'key');
106104

105+
bool _defineKeyParameter(ConstructorElement element) =>
106+
element.parameters.any((e) => e.name == 'key' && _isKeyType(e.type));
107+
107108
bool _isKeyType(DartType type) =>
108109
DartTypeUtilities.implementsInterface(type, 'Key', '');
110+
111+
bool _hasKeySuperParameterInitializerArg(ConstructorDeclaration node) {
112+
for (var parameter in node.parameters.parameters) {
113+
var element = parameter.declaredElement;
114+
if (element is SuperFormalParameterElement && element.name == 'key') {
115+
return true;
116+
}
117+
}
118+
119+
return false;
120+
}
109121
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
analyzer:
2+
enable-experiment:
3+
- super-parameters
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
flutter:../../../../mock_packages/flutter/lib/
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// test w/ `dart test -N use_key_in_widget_constructors`
6+
7+
import 'package:flutter/foundation.dart';
8+
import 'package:flutter/widgets.dart';
9+
10+
/// https://github.com/flutter/flutter/issues/100297
11+
class OtherWidget extends StatelessWidget {
12+
const OtherWidget({required super.key}); //OK
13+
14+
@override
15+
Widget build(BuildContext context) {
16+
return Container();
17+
}
18+
}
19+

0 commit comments

Comments
 (0)