Skip to content

"Add missing switch cases" fix doesn't account for shadowed names #59701

@DanTup

Description

@DanTup

This was originally raised at Dart-Code/Dart-Code#5366 by @klukshu.

Given:

a.dart

sealed class Attribute {}
class Attribute1 extends Attribute {}
class Attribute2 extends Attribute {}

switch.dart

import 'a.dart';

Object? f(Attribute attribute) {
  return switch (attribute) {
    Attribute1() => throw UnimplementedError(),
    Attribute2() => throw UnimplementedError(),
  };
}

class Attribute1 {}

The Attribute1 in the switch case is not the correct one (this produces no warning - which is #56338), but using the quick fix to "Add missing switch cases" adds code that does the same:

  return switch (attribute) {
    Attribute1() => throw UnimplementedError(),
    Attribute2() => throw UnimplementedError(),
    // TODO: Handle this case.
    Attribute1() => throw UnimplementedError(),
  };

This results in the original diagnostic (not exhaustively matched) remaining, but an additional one for "this case is covered by previous cases").

I'm not sure what the right fix is though, probably it would require adding an import with a prefix, but I'm not sure if that's the fix the user would want?

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-serverIssues related to some aspect of the analysis servertype-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions