Skip to content

Commit 2fe6ffe

Browse files
pqCommit Queue
authored and
Commit Queue
committed
[wildcards] prefer_final_parameters support
Fixes: https://github.com/dart-lang/linter/issues/5044 Change-Id: Ibbf24df34133cfa75deed357733c2ae0637251f5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/378902 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent 964017a commit 2fe6ffe

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'package:analyzer/dart/ast/ast.dart';
66
import 'package:analyzer/dart/ast/visitor.dart';
7+
import 'package:analyzer/src/dart/element/extensions.dart'; //ignore: implementation_imports
78

89
import '../analyzer.dart';
910

@@ -130,6 +131,7 @@ class _Visitor extends SimpleAstVisitor<void> {
130131
var declaredElement = param.declaredElement;
131132
if (declaredElement != null &&
132133
!declaredElement.isInitializingFormal &&
134+
!declaredElement.isWildcardVariable &&
133135
!body.isPotentiallyMutatedInScope(declaredElement)) {
134136
rule.reportLint(param, arguments: [param.name!.lexeme]);
135137
}

pkg/linter/test/rules/prefer_final_parameters_test.dart

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,20 @@ void f(final List<int> x) {
6262
]);
6363
}
6464

65+
test_closure_wildcard() async {
66+
await assertNoDiagnostics(r'''
67+
var f = (Object _) { };
68+
''');
69+
}
70+
71+
test_constructor_unused_wildcard() async {
72+
await assertNoDiagnostics(r'''
73+
class C {
74+
C(String _);
75+
}
76+
''');
77+
}
78+
6579
test_constructor_usedInBody() async {
6680
await assertDiagnostics(r'''
6781
class C {
@@ -158,6 +172,22 @@ class C {
158172
''');
159173
}
160174

175+
test_method_wildcard() async {
176+
await assertNoDiagnostics(r'''
177+
class C {
178+
void m(String _) { }
179+
}
180+
''');
181+
}
182+
183+
test_method_wildcard_final() async {
184+
await assertNoDiagnostics(r'''
185+
class C {
186+
void m(final String _) { }
187+
}
188+
''');
189+
}
190+
161191
test_operator() async {
162192
await assertDiagnostics(r'''
163193
class C {
@@ -180,6 +210,16 @@ class C {
180210
''');
181211
}
182212

213+
test_operator_wildcard() async {
214+
await assertNoDiagnostics(r'''
215+
class C {
216+
C operator +(C _) {
217+
return this;
218+
}
219+
}
220+
''');
221+
}
222+
183223
test_recordPattern_destructured() async {
184224
await assertNoDiagnostics(r'''
185225
void f(int a, int b) {
@@ -208,6 +248,14 @@ class C {
208248
''');
209249
}
210250

251+
test_setter_wildcard() async {
252+
await assertNoDiagnostics(r'''
253+
class C {
254+
void set f(int _) { }
255+
}
256+
''');
257+
}
258+
211259
test_superParameter() async {
212260
await assertNoDiagnostics('''
213261
class D {
@@ -274,6 +322,16 @@ void f({final String? p}) {
274322
''');
275323
}
276324

325+
test_topLevelFunction_named_wildcard() async {
326+
await assertDiagnostics(r'''
327+
void f({final String? _}) { }
328+
''', [
329+
// No lint.
330+
// https://github.com/dart-lang/language/blob/main/working/wildcards/feature-specification.md#declarations-that-are-capable-of-declaring-a-wildcard
331+
error(CompileTimeErrorCode.PRIVATE_OPTIONAL_PARAMETER, 22, 1),
332+
]);
333+
}
334+
277335
test_topLevelFunction_namedRequired() async {
278336
await assertDiagnostics(r'''
279337
void f({required String p}) {
@@ -292,6 +350,16 @@ void f({required final String p}) {
292350
''');
293351
}
294352

353+
test_topLevelFunction_namedRequired_wildcard() async {
354+
await assertDiagnostics(r'''
355+
void f({required String _}) { }
356+
''', [
357+
// No lint.
358+
// https://github.com/dart-lang/language/blob/main/working/wildcards/feature-specification.md#declarations-that-are-capable-of-declaring-a-wildcard
359+
error(CompileTimeErrorCode.PRIVATE_OPTIONAL_PARAMETER, 24, 1),
360+
]);
361+
}
362+
295363
test_topLevelFunction_optional() async {
296364
await assertDiagnostics(r'''
297365
void f([String? p]) {
@@ -307,6 +375,18 @@ void f([String? p]) {
307375
void f([final String? p]) {
308376
print(p);
309377
}
378+
''');
379+
}
380+
381+
test_topLevelFunction_optional_wildcard() async {
382+
await assertNoDiagnostics(r'''
383+
void f([String? _]) { }
384+
''');
385+
}
386+
387+
test_topLevelFunction_wildcard() async {
388+
await assertNoDiagnostics(r'''
389+
void f(int _){ }
310390
''');
311391
}
312392
}

0 commit comments

Comments
 (0)