Skip to content

False positive for "avoid_types_on_closure_parameters" when contravariance applies #58183

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
vsevolod19860507 opened this issue Jun 8, 2020 · 4 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

@vsevolod19860507
Copy link

When contravariance applies

class Unused {
  const Unused();
}

const unused = Unused();

@immutable
class User {
  final String name;

  const User({this.name});

  User Function({String name}) get copyWith => ({
        Object name = unused, // False positive
      }) =>
          User(
            name: name == unused ? this.name : name as String,
          );
}
@davidmorgan
Copy link
Contributor

+1

Along with #58183 this would make the lint match omit_local_variable_types, which seems desirable.

@srawlins
Copy link
Member

I find the motivating example to be extremely non-idiomatic. But I don't want to discount the idea.

In the example, we create a copyWith function, which is a User Function({String name}), but secretly under the covers, it is a User Function({Object name}), with a default value for name, which is not a String, but is a sentinel value. It's very clever, but I've never used or seen this design before.

The fix, to account for this example, would be to never complain about a parameter type if it is not exactly equal to the type that would be inferred, were the type omitted. I don't know that we have the tooling to answer hypothetical inference questions like this.

@pq pq added the P3 A lower priority bug or feature request label Nov 14, 2022
@srawlins srawlins changed the title False positive for "avoid_types_on_closure_parameters" False positive for "avoid_types_on_closure_parameters" when contravariance applies Jul 25, 2023
@rrousselGit
Copy link

I encountered this earlier.

The fix, to account for this example, would be to never complain about a parameter type if it is not exactly equal to the type that would be inferred, were the type omitted. I don't know that we have the tooling to answer hypothetical inference questions like this.

An alternative is to silence the lint if it is a downcast and is not required.

So:

Function(int) cb = (num a) {}; // KO

Function({int}) cb = ({int? a}) {}; // OK
Function({int}) cb = ({Object a = _sentinel}) {}; // OK

@devoncarew devoncarew added devexp-linter Issues with the analyzer's support for the linter package legacy-area-analyzer Use area-devexp instead. labels Nov 18, 2024
@devoncarew devoncarew transferred this issue from dart-archive/linter Nov 18, 2024
@eernstg
Copy link
Member

eernstg commented Nov 19, 2024

The linter could certainly be adjusted as mentioned, but it is also possible to work around the issue today:

class Unused {
  const Unused();
}

const unused = Unused();

@immutable
class User {
  final String name;

  const User({required this.name});

  User _copyWith({Object name = unused}) =>
      User(name: name == unused ? this.name : name as String);

  User Function({String name}) get copyWith => _copyWith;
}

@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

Successfully merging a pull request may close this issue.

8 participants