-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
UNREACHABLE_SWITCH_DEFAULT
warning to the analyzer.
This warning is similar to the existing `UNREACHABLE_SWITCH_CASE` warning, except that it warns if the `default` clause of a switch statement is unreachable due to all the `case` clasuses fully exhausting the switched type. To make the implementation easier, I changed the API for the `reportExhaustiveness` method in `_fe_analyzer_shared` (which is the primary entry point to the shared exhaustiveness checker). Previously, this method returned a list of `ExhaustivenessError`, where each list element was either an `UnreachableCaseError` (indicating that a certain case was unreachable) or a `NonExhaustiveError` (indicating that the entire switch statement was not exhaustive). If the caller passed in `false` for `computeUnreachable`, `UnreachableCaseError`s would not be returned, so the returned list would either be empty or contain a single `NonExhaustiveError`. The new API renames the types for clarity: - `NonExhaustiveError` becomes `NonExhaustiveness`, to highlight the fact that it's not necessarily an error for the switch's cases to be non-exhaustive; it's only an error if the scrutinee's static type is an "always exhaustive" type and there is no `default` clause. - `UnreachableCaseError` becomes `CaseUnreachability`, to highlight the fact that it's not an error for a case to be unreachable; it's a warning. Also, the new API adds instances of `CaseUnreachability` to an optional user-provided list instead of returning a newly created list; this allows callers to communicate that they don't need to see `CaseUnreachability` information by passing `null`. This frees up the return type to simply be an instance of `NonExhaustiveness` (if the cases are not exhaustive) or `null` (if they are exhaustive). This makes it easier for the analyzer to decide whether to issue the new warning, because it doesn't have to dig around the list looking for an instance of `NonExhaustiveness`. The new warning has an associated quick fix (remove the unreachable `default` clause). This quick fix uses the same `RemoveDeadCode` logic in the analysis server that the existing `UNREACHABLE_SWITCH_CASE` warning uses. Fixes #54575. Bug: #54575 Change-Id: I18b6b7c5249d77d28ead7488b4aae4ea65c4b664 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/378960 Reviewed-by: Johnni Winther <[email protected]> Commit-Queue: Paul Berry <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Reviewed-by: Erik Ernst <[email protected]>
- Loading branch information
1 parent
e06d0f3
commit cda2815
Showing
33 changed files
with
660 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.