Skip to content

unnecessary_lambdas false positive when affecting inference #58986

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
FMorschel opened this issue Dec 21, 2022 · 7 comments
Open

unnecessary_lambdas false positive when affecting inference #58986

FMorschel opened this issue Dec 21, 2022 · 7 comments
Labels
area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. devexp-linter Issues with the analyzer's support for the linter package linter-false-positive P3 A lower priority bug or feature request type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@FMorschel
Copy link
Contributor

FMorschel commented Dec 21, 2022

Describe the issue
I had a tear-off on my parameter, but I had to change it with a ternary operator. That triggered this false positive.

To Reproduce
argument_type_not_assignable triggering on:

void fn({
required void Function(String) parameter,
}) {...}

void function2([String? text]) {...}

void callback() {...}

// Example:
...
  fn(
    parameter: (test) ? function2 : (_) => callback(),
  );
...

When I "solved" that with:

  fn(
    parameter: (test) ? (str) => function2(str) : (_) => callback(), // triggering `unnecessary_lambdas`
  );

Previously I had (and it was working):

  fn(
    parameter: function2,
  );

Expected behavior
argument_type_not_assignable not triggering when that solves unnecessary_lambdas or stopping unnecessary_lambdasfrom triggering.

@srawlins srawlins transferred this issue from dart-archive/linter Dec 21, 2022
@bwilkerson bwilkerson transferred this issue from dart-lang/sdk Dec 21, 2022
@bwilkerson
Copy link
Member

The argument_type_not_assignable diagnostic isn't a lint, it's a compilation error that means that your code won't compile or run.

The problem is that the conditional expression produces a value whose static type is the least upper bound of the types of the then and else expressions (void Function([String?]) and void Function() respectively. Because those signatures don't match, the least upper bound is Function, which really isn't assignable to void Function(String). You could correct the issue by writing

fn(
  parameter: test ? function2 : ([String? _]) => callback(),
);

But there is a bug here, in that unnecessary_lambdas shouldn't have beed produced in this case because the type of the closure isn't the same as the type of the tear-off.

@FMorschel FMorschel changed the title argument_type_not_assignable false positive unnecessary_lambdas false positive Dec 21, 2022
@FMorschel FMorschel changed the title unnecessary_lambdas false positive unnecessary_lambdas false positive Dec 21, 2022
@FMorschel
Copy link
Contributor Author

Understood.

Though I still don't follow why the (_) on (_) => callback() is not parsed as String when it is placed as a function parameter.

@bwilkerson
Copy link
Member

You're right, the type of the parameter is inferred to be String, but the least upper bound of void Function([String?]) and void Function(String) is still be Function because of the difference in nullability.

@FMorschel
Copy link
Contributor Author

Thank you for the explanation!

@pq pq added the P3 A lower priority bug or feature request label Dec 22, 2022
@pq
Copy link
Member

pq commented Dec 22, 2022

Yes. Thank you both!

@srawlins srawlins changed the title unnecessary_lambdas false positive unnecessary_lambdas false positive when affecting inference Feb 24, 2023
@FMorschel
Copy link
Contributor Author

You're right, the type of the parameter is inferred to be String, but the least upper bound of void Function([String?]) and void Function(String) is still be Function because of the difference in nullability.

@bwilkerson, is there any documentation on this? I want to understand better, why it couldn't interfere void Function(String) since both could be used as such.

@srawlins
Copy link
Member

This comment in the language tracker may be your best source of documentation, and the current state of things.

@srawlins srawlins added the type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) label Mar 29, 2024
@devoncarew devoncarew added devexp-linter Issues with the analyzer's support for the linter package legacy-area-analyzer Use area-devexp instead. labels Nov 19, 2024
@devoncarew devoncarew transferred this issue from dart-archive/linter Nov 19, 2024
@bwilkerson bwilkerson added area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. and removed legacy-area-analyzer Use area-devexp instead. labels Feb 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. devexp-linter Issues with the analyzer's support for the linter package linter-false-positive P3 A lower priority bug or feature request type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

5 participants