Skip to content

Commit

Permalink
[linter] Don't trigger unnecessary_lambdas on deferred constructors
Browse files Browse the repository at this point in the history
I didn't add this exception to the documentation as it's pretty niche and I'm afraid it might detract from the goal of the lint. Happy to add something if you think it's worth while though :)

Bug reference: #54611

Change-Id: I977be945208aee285f7bb44f73f7953b0646dc14
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/346100
Commit-Queue: Phil Quitslund <[email protected]>
Reviewed-by: Brian Wilkerson <[email protected]>
Reviewed-by: Phil Quitslund <[email protected]>
Auto-Submit: Parker Lougheed <[email protected]>
  • Loading branch information
parlough authored and Commit Queue committed Jan 16, 2024
1 parent 0fabbeb commit 9bcb4a0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pkg/linter/lib/src/rules/unnecessary_lambdas.dart
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ class _Visitor extends SimpleAstVisitor<void> {

void _visitInstanceCreation(
InstanceCreationExpression expression, FunctionExpression node) {
if (expression.isConst) return;
if (expression.isConst || expression.constructorName.type.isDeferred) {
return;
}

var arguments = expression.argumentList.arguments;
var parameters = node.parameters?.parameters ?? <FormalParameter>[];
Expand Down
24 changes: 24 additions & 0 deletions pkg/linter/test/rules/unnecessary_lambdas_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,30 @@ var x = [() => const C()];
''');
}

test_constructorCall_imported() async {
newFile('$testPackageLibPath/b.dart', r'''
class C {}
''');
await assertDiagnostics(r'''
import 'b.dart' as b;
var x = [() => b.C()];
''', [
lint(32, 11),
]);
}

test_constructorCall_importedDeferred() async {
newFile('$testPackageLibPath/b.dart', r'''
class C {}
''');
await assertNoDiagnostics(r'''
import 'b.dart' deferred as b;
var x = [() => b.C()];
''');
}

test_constructorCall_matchingArg() async {
await assertDiagnostics(r'''
class C {
Expand Down

0 comments on commit 9bcb4a0

Please sign in to comment.