Skip to content

Commit d5981ad

Browse files
kallentuCommit Queue
authored andcommitted
[linter] Issue 60136: Update strict_top_level_inference to ignore wildcard variables.
Avoid linting on wildcard variables. Wildcard variables won't be used, so it's okay that the type will be `dynamic`. Fixes: #60136 Bug: #60136 Change-Id: I6bc0543bfbd3b8eac09ca934fc5098b6c825e1d6 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/410940 Reviewed-by: Phil Quitslund <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Kallen Tu <[email protected]>
1 parent 24f1352 commit d5981ad

File tree

2 files changed

+75
-4
lines changed

2 files changed

+75
-4
lines changed

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'package:analyzer/dart/analysis/features.dart';
56
import 'package:analyzer/dart/ast/ast.dart';
67
import 'package:analyzer/dart/ast/token.dart';
78
import 'package:analyzer/dart/ast/visitor.dart';
@@ -40,11 +41,17 @@ class StrictTopLevelInference extends LintRule {
4041
}
4142

4243
class _Visitor extends SimpleAstVisitor<void> {
44+
final bool _wildCardVariablesEnabled;
45+
4346
final LintRule rule;
4447

4548
final LinterContext context;
4649

47-
_Visitor(this.rule, this.context);
50+
_Visitor(this.rule, this.context)
51+
: _wildCardVariablesEnabled = context.isEnabled(Feature.wildcard_variables);
52+
53+
bool isWildcardIdentifier(String lexeme) =>
54+
_wildCardVariablesEnabled && lexeme == '_';
4855

4956
@override
5057
void visitConstructorDeclaration(ConstructorDeclaration node) {
@@ -125,6 +132,11 @@ class _Visitor extends SimpleAstVisitor<void> {
125132
}) {
126133
for (var i = 0; i < parameters.length; i++) {
127134
var parameter = parameters[i];
135+
var parameterName = parameter.name;
136+
if (parameterName != null && isWildcardIdentifier(parameterName.lexeme)) {
137+
continue;
138+
}
139+
128140
if (parameter is DefaultFormalParameter) {
129141
parameter = parameter.parameter;
130142
}
@@ -142,20 +154,20 @@ class _Visitor extends SimpleAstVisitor<void> {
142154

143155
if (parameter.type != null) return;
144156
if (overriddenMember == null) {
145-
_report(parameter.name, keyword: parameter.keyword);
157+
_report(parameterName, keyword: parameter.keyword);
146158
} else {
147159
if (parameter.isPositional) {
148160
if (overriddenMember.formalParameters.length <= i ||
149161
overriddenMember.formalParameters[i].isNamed) {
150162
// The overridden member does not have a corresponding parameter.
151-
_report(parameter.name, keyword: parameter.keyword);
163+
_report(parameterName, keyword: parameter.keyword);
152164
}
153165
} else {
154166
var overriddenParameter = overriddenMember.formalParameters
155167
.firstWhereOrNull((p) => p.isNamed);
156168
if (overriddenParameter == null) {
157169
// The overridden member does not have a corresponding parameter.
158-
_report(parameter.name, keyword: parameter.keyword);
170+
_report(parameterName, keyword: parameter.keyword);
159171
}
160172
}
161173
}

pkg/linter/test/rules/strict_top_level_inference_test.dart

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,4 +1014,63 @@ var f;
10141014
],
10151015
);
10161016
}
1017+
1018+
test_wildcardVariable_constructorParameter() async {
1019+
await assertNoDiagnostics(r'''
1020+
class C {
1021+
C(_) {}
1022+
}
1023+
''');
1024+
}
1025+
1026+
test_wildcardVariable_constructorParameter_preWildcards() async {
1027+
await assertDiagnostics(
1028+
r'''
1029+
// @dart = 3.4
1030+
// (pre wildcard-variables)
1031+
class C {
1032+
C(_) {}
1033+
}
1034+
''',
1035+
[lint(57, 1)],
1036+
);
1037+
}
1038+
1039+
test_wildcardVariable_function() async {
1040+
await assertNoDiagnostics(r'''
1041+
void m(_) {}
1042+
''');
1043+
}
1044+
1045+
test_wildcardVariable_function_preWildcards() async {
1046+
await assertDiagnostics(
1047+
r'''
1048+
// @dart = 3.4
1049+
// (pre wildcard-variables)
1050+
void m(_) {}
1051+
''',
1052+
[lint(50, 1)],
1053+
);
1054+
}
1055+
1056+
test_wildcardVariable_method() async {
1057+
await assertNoDiagnostics(r'''
1058+
class C {
1059+
void m(_) {}
1060+
}
1061+
''');
1062+
}
1063+
1064+
test_wildcardVariable_method_preWilcards() async {
1065+
await assertDiagnostics(
1066+
r'''
1067+
// @dart = 3.4
1068+
// (pre wildcard-variables)
1069+
class C {
1070+
void m(_) {}
1071+
}
1072+
''',
1073+
[lint(62, 1)],
1074+
);
1075+
}
10171076
}

0 commit comments

Comments
 (0)