Skip to content

Commit 85b6b1a

Browse files
Nishthajain7Commit Queue
authored andcommitted
lint: include anonymous function expressions in parameter_assignments check
[email protected] Bug: #58659 Change-Id: If954cd3dfbed648490901e40319b73dc86ca0a54 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/460340 Auto-Submit: Nishtha Jain <[email protected]> Reviewed-by: Keerti Parthasarathy <[email protected]> Commit-Queue: Keerti Parthasarathy <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]>
1 parent 0b0a637 commit 85b6b1a

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class ParameterAssignments extends AnalysisRule {
4242
var visitor = _Visitor(this);
4343
registry.addFunctionDeclaration(this, visitor);
4444
registry.addMethodDeclaration(this, visitor);
45+
registry.addFunctionExpression(this, visitor);
46+
registry.addConstructorDeclaration(this, visitor);
4547
}
4648
}
4749

@@ -151,6 +153,11 @@ class _Visitor extends SimpleAstVisitor<void> {
151153

152154
_Visitor(this.rule);
153155

156+
@override
157+
void visitConstructorDeclaration(ConstructorDeclaration node) {
158+
_checkParameters(node.parameters, node.body);
159+
}
160+
154161
@override
155162
void visitFunctionDeclaration(FunctionDeclaration node) {
156163
_checkParameters(
@@ -159,6 +166,13 @@ class _Visitor extends SimpleAstVisitor<void> {
159166
);
160167
}
161168

169+
@override
170+
void visitFunctionExpression(FunctionExpression node) {
171+
if (node.parent is! FunctionDeclaration) {
172+
_checkParameters(node.parameters, node.body);
173+
}
174+
}
175+
162176
@override
163177
void visitMethodDeclaration(MethodDeclaration node) {
164178
_checkParameters(node.parameters, node.body);

pkg/linter/test/rules/parameter_assignments_test.dart

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,43 @@ class ParameterAssignmentsTest extends LintRuleTest {
1717
@override
1818
String get lintRule => LintNames.parameter_assignments;
1919

20+
test_anonymousFunction_assignment() async {
21+
await assertDiagnostics(
22+
r'''
23+
void main() {
24+
(int i) {
25+
i = 42;
26+
}(0);
27+
}
28+
''',
29+
[lint(30, 6)],
30+
);
31+
}
32+
33+
test_anonymousFunction_assignment_arrowBody() async {
34+
await assertDiagnostics(
35+
r'''
36+
void main() {
37+
(int i) => i = 42;
38+
}
39+
''',
40+
[lint(27, 6)],
41+
);
42+
}
43+
44+
test_anonymousFunction_assignment_notInvoked() async {
45+
await assertDiagnostics(
46+
r'''
47+
void main() {
48+
(int i) {
49+
i = 42;
50+
};
51+
}
52+
''',
53+
[lint(30, 6)],
54+
);
55+
}
56+
2057
test_assignment_inIfElseBranches() async {
2158
await assertDiagnostics(
2259
r'''
@@ -70,7 +107,6 @@ void f([int? _]) {
70107
);
71108
}
72109

73-
@FailingTest(reason: 'Closures not implemented')
74110
test_closure_assignment() async {
75111
await assertDiagnostics(
76112
r'''
@@ -95,6 +131,19 @@ void f(int p) {
95131
);
96132
}
97133

134+
test_constructor_assignment() async {
135+
await assertDiagnostics(
136+
r'''
137+
class Foo {
138+
Foo(int x) {
139+
x = 4;
140+
}
141+
}
142+
''',
143+
[lint(31, 5)],
144+
);
145+
}
146+
98147
test_function_assignment() async {
99148
await assertDiagnostics(
100149
r'''

0 commit comments

Comments
 (0)