Skip to content

"Add missing case clauses" only inserts first case for switch pattern statements #52180

Closed

Description

@DanTup

With language version 2.19 this works correctly:

Apr-26-2023 14-31-49

But for 3.0 it only inserts one case:

Apr-26-2023 14-32-23

This missing test might be related, though it seems to be looking for a different fix to the one being produced (perhaps it's intended for pre-3.0 code, but probably it shouldn't be failing if that's the case).

@FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/49759')

Activity

  1. added
    devexp-serverIssues related to some aspect of the analysis server
    devexp-quick-fixIssues with analysis server (quick) fixes
    on Apr 26, 2023
  2. changed the title [-]"Add missing case clauses" does not work correctly for switch pattern statements[/-] [+]"Add missing case clauses" only inserts first case for switch pattern statements[/+] on Apr 26, 2023
  3. bwilkerson commented on Apr 26, 2023

    @bwilkerson
    Member

    Unfortunately, that behavior is the best we can do at the moment. With the switch to patterns we had to write a much more general algorithm to compute exhaustiveness, and that algorithm currently only finds the first case that isn't covered. There have been discussions about extending the algorithm to find all of the missing cases so that we can do a better job, but I don't know what the state of that work is.

    @scheglov

  4. added
    P3A lower priority bug or feature request
    on May 5, 2023
  5. deimantasa commented on May 21, 2023

    @deimantasa

    Any updates to this? It feels like a serious bummer, especially when having several (5+) enum cases to deal with. Auto-fill used to save tons of time.

    P.S. Android Studio (Flamingo) does not even generate one case. Only by hovering, first case is hinted.
    Screenshot 2023-05-21 at 7 25 19 AM
    Screenshot 2023-05-21 at 7 25 50 AM

  6. pq commented on Jul 6, 2023

    @pq
    Member

    There have been discussions about extending the algorithm to find all of the missing cases so that we can do a better job, but I don't know what the state of that work is.

    @scheglov?

  7. lhengl commented on Jul 12, 2023

    @lhengl

    Just upgraded to dart 3 and stumbled into this problem. There used to be fixes available for exhaustiveness, now there isn't. With the prevalent use of enum and switch statements, I'm surprised this issue doesn't have more upvotes.

  8. AbdulazizRasulbek commented on Jul 12, 2023

    @AbdulazizRasulbek

    same issue for sealed classes

  9. added
    type-bugIncorrect behavior (everything from a crash to more subtle misbehavior)
    on Mar 15, 2024
  10. bwilkerson commented on Jun 13, 2024

    @bwilkerson
    Member

    This has been fixed by https://dart-review.googlesource.com/c/sdk/+/371062 and should be available in the next stable release.

  11. DanTup commented on Jun 13, 2024

    @DanTup
    CollaboratorAuthor

    @bwilkerson I tested this and it seems to work great 🥳 but I noticed the test I linked up top is still marked @FailingTest:

    @FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/49759')

    It fails because of "Unnecessary error filter" (a test bug), but if I remove the WithFilter, it fails with:

    Expected to find fix dart.fix.add.missingEnumCaseClauses in
    Fix(kind=dart.fix.add.missingSwitchCases, change={"message":"Add missing switch cases","edits":[{"file":"C:\home\test\lib\test.dart","fileStamp":0,"edits":[{"offset":49,"length":0,"replacement":" case E.a:\r\n // TODO: Handle this case.\r\n case E.b:\r\n // TODO: Handle this case.\r\n case E.c:\r\n // TODO: Handle this case.\r\n"}]}],"linkedEditGroups":[],"id":"dart.fix.add.missingSwitchCases"})

    It looks like the fix provided is dart.fix.add.missingSwitchCases but this test is for dart.fix.add.missingEnumCaseClauses. It's not clear to me what the difference is here (I thought maybe one is for the original switch statement parsing and the other is since switch expressions were added, but I don't think that explains how any of other other tests in this class would be passing).

    Oh, as I was about to send this I noticed all the non-failing tests in that class are marked // @dart=2.19.

    So.. should all of the @FailingTests in the class AddMissingEnumCaseClausesTest now be moved into AddMissingSwitchCasesTest_SwitchStatement since it seems for >2.19 they'll all be handled by that fix instead?

  12. bwilkerson commented on Jun 13, 2024

    @bwilkerson
    Member

    The fix kind DartFixKind.ADD_MISSING_ENUM_CASE_CLAUSES is associated with AddMissingEnumCaseClauses, which is associated with StaticWarningCode.MISSING_ENUM_CONSTANT_IN_SWITCH. That warning code is produced only in pre-pattern-enabled code.

    The fix kind DartFixKind.ADD_MISSING_SWITCH_CASES is associated with AddMissingSwitchCases, which is associated with both CompileTimeErrorCode.NON_EXHAUSTIVE_SWITCH_EXPRESSION and CompileTimeErrorCode.NON_EXHAUSTIVE_SWITCH_STATEMENT, both of which are only produced in pattern-enabled code.

    ... should all of the @FailingTests in the class AddMissingEnumCaseClausesTest now be moved into AddMissingSwitchCasesTest since it seems for >2.19 they'll all be handled by that fix instead?

    If the tests are intended to be run with patterns enabled, then yes, they should be moved because they will produce the CompileTimeErrorCode.NON_EXHAUSTIVE_SWITCH_EXPRESSION and CompileTimeErrorCode.NON_EXHAUSTIVE_SWITCH_STATEMENT diagnostics, and AddMissingSwitchCasesTest is where those tests should live.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestdevexp-quick-fixIssues with analysis server (quick) fixesdevexp-serverIssues related to some aspect of the analysis serverlegacy-area-analyzerUse area-devexp instead.type-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