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

Missing dead code warning #60342

Open
eernstg opened this issue Mar 18, 2025 · 1 comment
Open

Missing dead code warning #60342

eernstg opened this issue Mar 18, 2025 · 1 comment
Labels
area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. model-exhaustiveness Implementation of exhaustiveness checking P2 A bug or feature request we're likely to work on

Comments

@eernstg
Copy link
Member

eernstg commented Mar 18, 2025

Consider the following program:

sealed class A {}
class B1 extends A {}
class B2 extends A {}
class C {}

int f(A a) => switch (a) {
  B1() => 1,
  B2() => 2,
  C() => 3,
};

I just learned that the above switch expression does not give rise to a dead code warning. It is guaranteed that when the scrutinee a has static type A, the run-time type of a will match B1() or B2(). This implies that the C() case is dead code, in spite of the fact that there could be an object whose run-time type is a subtype of A and also a subtype of C (class D implements B1, C {} could be declared in any library).

A small experiment shows that the exhaustiveness is recognized already: Comment out C() => 3: No exhaustiveness error is reported for the switch. This implies that all additional cases will be dead.

Could we emit a dead code warning in this case? It seems sound and useful.

Note, though, that the ordering is significant: If the C() => 3 case is moved up to an earlier position then it is not impossible that it could be matched, and hence it isn't dead code.

@eernstg eernstg added area-dart-model For issues related to conformance to the language spec in the parser, compilers or the CLI analyzer. model-exhaustiveness Implementation of exhaustiveness checking improve-diagnostics Related to the quality of diagnostic messages labels Mar 18, 2025
@johnniwinther johnniwinther added area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. and removed area-dart-model For issues related to conformance to the language spec in the parser, compilers or the CLI analyzer. labels Mar 18, 2025
@bwilkerson bwilkerson added area-dart-model For issues related to conformance to the language spec in the parser, compilers or the CLI analyzer. and removed improve-diagnostics Related to the quality of diagnostic messages area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. labels Mar 18, 2025
@bwilkerson
Copy link
Member

@johnniwinther

Sorry, I didn't see that you'd changed the area until after I'd changed it back. My understanding is that the dead code warning in this case would come from the exhaustiveness checker.

I agree that if we can determine that the code can't be reached that we should flag it, so I'm thinking that this is an implementation request.

@johnniwinther johnniwinther added area-devexp For issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages. and removed area-dart-model For issues related to conformance to the language spec in the parser, compilers or the CLI analyzer. labels Mar 19, 2025
@pq pq added the P2 A bug or feature request we're likely to work on label Mar 19, 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. model-exhaustiveness Implementation of exhaustiveness checking P2 A bug or feature request we're likely to work on
Projects
None yet
Development

No branches or pull requests

4 participants