Skip to content

Analyzer says unnecessary_cast, but removing cast is a compile-time error. #34150

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

Closed
lrhn opened this issue Aug 15, 2018 · 2 comments
Closed
Labels
devexp-warning Issues with the analyzer's Warning codes devexp-wolf-analysis Static analysis that requires the proposed Wolf analysis framework legacy-area-analyzer Use area-devexp instead. P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Milestone

Comments

@lrhn
Copy link
Member

lrhn commented Aug 15, 2018

Example:

const int minValue = (1 << 63);
T abs<T extends num>(T value) {
  if (value is int && value == minValue) return minValue as num;
  return -value;
}
main() {
  print(abs(42));
}

Here the analyzer (according to dartpad) complains that as num is unnecessary.
However, without the cast, minValue is not assignable to T.

@lrhn lrhn added legacy-area-analyzer Use area-devexp instead. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Aug 15, 2018
@bwilkerson bwilkerson added this to the Dart2.1 milestone Aug 28, 2018
@scheglov scheglov added the P2 A bug or feature request we're likely to work on label Sep 19, 2018
@scheglov scheglov modified the milestones: Dart2.1, PostDart2.1 Sep 19, 2018
@aadilmaan aadilmaan modified the milestones: Future, D25 Release Jun 4, 2019
@srawlins srawlins changed the title Anaylzer says cast is unnecessary, but removing cast is a compile-time error. Analyzer says cast is unnecessary, but removing cast is a compile-time error. Jun 9, 2020
@srawlins srawlins added the devexp-warning Issues with the analyzer's Warning codes label Jun 17, 2020
@srawlins srawlins changed the title Analyzer says cast is unnecessary, but removing cast is a compile-time error. Analyzer says unnecessary_cast, but removing cast is a compile-time error. Apr 16, 2023
@srawlins srawlins added the devexp-wolf-analysis Static analysis that requires the proposed Wolf analysis framework label Jul 26, 2024
@srawlins
Copy link
Member

srawlins commented Sep 3, 2024

The unnecessary_cast warning now only reports when the cast type is the exact same as the expression's static type.

@srawlins srawlins closed this as completed Sep 3, 2024
@lrhn
Copy link
Member Author

lrhn commented Sep 4, 2024

(And eerily the following should therefore still be reported:

  List<num> nums = []; 
  if (nums is! List<int>) nums = [1] as List<int>;
  print([nums].runtimeType);

but removing the cast will change the static type of nums after the if.
You just can't be sure without knowing what the type inference would do without the cast, and the cast changes the context type of the expression it casts. This is probably good enough, the cases where an as is used for its ability to block a context type are likley few far between in any code that has to get through code review.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
devexp-warning Issues with the analyzer's Warning codes devexp-wolf-analysis Static analysis that requires the proposed Wolf analysis framework legacy-area-analyzer Use area-devexp instead. P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

6 participants