From 9bcb4a024ba74055e468860881cb44a8c556e2c7 Mon Sep 17 00:00:00 2001 From: Parker Lougheed Date: Tue, 16 Jan 2024 15:29:49 +0000 Subject: [PATCH] [linter] Don't trigger unnecessary_lambdas on deferred constructors 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: https://github.com/dart-lang/sdk/issues/54611 Change-Id: I977be945208aee285f7bb44f73f7953b0646dc14 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/346100 Commit-Queue: Phil Quitslund Reviewed-by: Brian Wilkerson Reviewed-by: Phil Quitslund Auto-Submit: Parker Lougheed --- .../lib/src/rules/unnecessary_lambdas.dart | 4 +++- .../test/rules/unnecessary_lambdas_test.dart | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/pkg/linter/lib/src/rules/unnecessary_lambdas.dart b/pkg/linter/lib/src/rules/unnecessary_lambdas.dart index d3c36667a8d3..9a71fe2eaedf 100644 --- a/pkg/linter/lib/src/rules/unnecessary_lambdas.dart +++ b/pkg/linter/lib/src/rules/unnecessary_lambdas.dart @@ -167,7 +167,9 @@ class _Visitor extends SimpleAstVisitor { 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 ?? []; diff --git a/pkg/linter/test/rules/unnecessary_lambdas_test.dart b/pkg/linter/test/rules/unnecessary_lambdas_test.dart index 276c69d47bcb..fe83553237cf 100644 --- a/pkg/linter/test/rules/unnecessary_lambdas_test.dart +++ b/pkg/linter/test/rules/unnecessary_lambdas_test.dart @@ -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 {