Skip to content
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

False-positive unnecessary_cast when making a type nullable. #53828

Closed
naipofo opened this issue Oct 23, 2023 · 2 comments
Closed

False-positive unnecessary_cast when making a type nullable. #53828

naipofo opened this issue Oct 23, 2023 · 2 comments
Labels
analyzer-warning Issues with the analyzer's Warning codes area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P1 A high priority bug; for example, a single project is unusable or has many test failures

Comments

@naipofo
Copy link

naipofo commented Oct 23, 2023

Describe the issue
Linter gives unnecessary_cast despite the cast fixing a type error.

To Reproduce

void main() {
  final list = [1,2,3,4];

  // type error: The return type 'Null' isn't a 'int', as required by the closure's context.
  final maybeFound1 = list.firstWhere((e) => e == 5, orElse: () => null);

  // error fixed but lint says that there is an unnecessary cast
  final maybeFound2 = (list as List<int?>).firstWhere((e) => e == 5, orElse: () => null);
}

Expected behavior
No lint message, as the cast is not unnecessary.

Additional context
Applying the lint fix as suggested causes a type error and dart refuses to compile.

@bwilkerson bwilkerson changed the title False-negative unnecessary_cast when making a type nullable. False-positive unnecessary_cast when making a type nullable. Oct 23, 2023
@bwilkerson bwilkerson transferred this issue from dart-lang/linter Oct 23, 2023
@bwilkerson bwilkerson added P1 A high priority bug; for example, a single project is unusable or has many test failures area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. analyzer-warning Issues with the analyzer's Warning codes labels Oct 23, 2023
@srawlins
Copy link
Member

More or less a duplicate of #49550 (P2), and broadly #52086 (P3). See also #52117, #49269, #48984, #34150. This can be fixed by https://dart-review.googlesource.com/c/sdk/+/265420.

@srawlins
Copy link
Member

Fixed with the fix to those issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-warning Issues with the analyzer's Warning codes area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P1 A high priority bug; for example, a single project is unusable or has many test failures
Projects
None yet
Development

No branches or pull requests

3 participants