Files
sdk/tests/language/patterns/switch_case_scope_test.dart
T
Paul Berry cda2815bb1 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 https://github.com/dart-lang/sdk/issues/54575.

Bug: https://github.com/dart-lang/sdk/issues/54575
Change-Id: I18b6b7c5249d77d28ead7488b4aae4ea65c4b664
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/378960
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Paul Berry <paulberry@google.com>
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Erik Ernst <eernst@google.com>
2024-09-10 19:13:12 +00:00

74 lines
1.9 KiB
Dart

// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
main() {
switchStatementBodyScope();
ifCaseThenScope();
unsharedPatternVariableShadows();
sharedPatternVariableShadows(0);
sharedPatternVariableShadows(1);
}
var topLevel = 'top-level';
/// Pattern variables are in a scope surrounding the body scope.
void switchStatementBodyScope() {
switch ('pattern') {
case var x:
// Not a collision:
var x = 'local';
Expect.equals('local', x);
default: // ignore: unreachable_switch_default
Expect.fail('Should not reach this.');
}
}
/// Pattern variables are in a scope surrounding the if-case then scope.
void ifCaseThenScope() {
// Not an error: Then statement is in separate scope.
if ('pattern' case var x) var x = 'local';
}
/// Unshared variables shadow variables in outer scopes.
void unsharedPatternVariableShadows() {
var local = 'local';
switch (('pat', 'tern')) {
case (String topLevel, String local):
Expect.equals('pat tern', '$topLevel $local');
// Assign to pattern variable.
local = 'assigned';
default: // ignore: unreachable_switch_default
Expect.fail('Should not reach this.');
}
// Outer local is not assigned.
Expect.equals('local', local);
}
/// Shared variables shadow variables in outer scopes.
void sharedPatternVariableShadows(Object value) {
var local = 'local';
switch (('pat', 'tern')) {
case (String topLevel, String local) when value == 0:
case (String topLevel, String local) when value == 1:
Expect.equals('pat tern', '$topLevel $local');
// Assign to pattern variable.
local = 'assigned';
default:
Expect.fail('Should not reach this.');
}
// Outer local is not assigned.
Expect.equals('local', local);
}